1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

はてなダイアリー to Markdown > v0.7 > fix bug: C++のコメント行の誤変換を回避

Last updated at Posted at 2018-08-02

はてなダイアリー to Markdown > v0.6 > Verilog-HDLコードとVHDLコード表記対応
の続き

v0.1はこちら

v0.6の問題

>|cpp|
//---------------------------------------------------------------------------

# ifndef Unit1H
# define Unit1H

上記のようなコードがあった時にリストとして変換される (下記)。

```cpp
//        -        -        -        -        -        -        -        -        -        -        -        -        -        -        -        -        -        -        -        -        -        -        -        -        -

# ifndef Unit1H
# define Unit1H

code v0.7

上記の誤変換に関して対処した。

import sys
import re

# on Python 3.5

'''
v0.7 Aug. 02, 2018
   - conv_sublist() avoids converting C++ comment (e.g. //---)
v0.6 Jun. 22, 2018
   - conv_super_pre_notation() handles [vhdl]
   - conv_super_pre_notation() handles [verilog]
v0.5 May, 29, 2018
   - add conv_tex()
v0.4 Apr. 25, 2018
   - add conv_tableFormat()
v0.3 Apr. 25, 2018
   - conv_super_pre_notation() takes cpp notation
v0.2 Apr. 25, 2018
   - add conv_super_pre_notation()
   - add conv_sublist()
v0.1 Apr. 24, 2018
   - sub list is converted to Markdown style
'''


def conv_tex(intxt):
    if r"[Tex:" in intxt or r"[tex:" in intxt:
        wrk = re.sub('\[Tex:', r'```math\r\n', intxt)
        wrk = re.sub('\[tex:', r'```math\r\n', wrk)
        wrk = re.sub(r']', r'\r\n```', wrk)
       	return wrk
    return intxt


def conv_sublist(astr):
    if "//-" in astr:  # C++ comment
        return astr
    if "---" in astr:
        astr = astr.replace("---", SPACE4 + SPACE4 + "-")
    if "--" in astr:
        astr = astr.replace("--", SPACE4 + "-")
    return astr


def conv_super_pre_notation(astr):
    astr = astr.replace(">||", "```")
    astr = astr.replace(">|cpp|", "```cpp")
    astr = astr.replace(">|c|", "```c")
    astr = astr.replace("||<", "```")
    astr = astr.replace(">|verilog|", "```verilog")
    astr = astr.replace(">|vhdl|", "```vhdl")
    return astr


def getTableFormat(numVertical):
    wrk = "|"
    for loop in range(numVertical - 1):   # -1: except for first
       wrk += ":-:|"
    return wrk


def conv_tableFormat(astr):
    global countTbl
    if "|" in astr:
        countTbl += 1
        if countTbl == 2:
            num = astr.count('|')
            fmt = getTableFormat(num)
            return fmt + "\r\n" + astr
    else:
        countTbl = 0
    return astr
    
# read from stdin
lines = sys.stdin.readlines()

SPACE4 = "    "

countTbl = 0  # Table format counter
for elem in lines:
    wrk = conv_sublist(elem)
    wrk = conv_super_pre_notation(wrk)
    wrk = conv_tableFormat(wrk)
    wrk = conv_tex(wrk)
    print(wrk, end='')

使用方法

  • 変換したいはてなダイアリーの内容をstdinに貼る
  • 実行する
  • 出力されるstdoutをMarkdownとして利用する

再実行する場合はstdoutの「Clear the output」で出力をクリアしてから実行する。

未対応の問題

VHDLのコードのコメント-- 2to4デコーダー--は、はてなのリスト表記--と被る。

v0.7までは--はリストとして変換されてしまうので、あとで手直しが必要となる。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?