Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

v0.1はこちら

v0.6の問題

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

#ifndef Unit1H
#define Unit1H
```

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

#ifndef Unit1H
#define Unit1H
```

code v0.7

https://ideone.com/OWXJqB

```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
v0.4 Apr. 25, 2018
v0.3 Apr. 25, 2018
- conv_super_pre_notation() takes cpp notation
v0.2 Apr. 25, 2018
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

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として利用する

未対応の問題

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

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

セブンオブナインです。Unimatrix 01の第三付属物 9の7という識別番号です。Star trek Voyagerの好きなキャラクターです。まとめ記事は後日タイトルから内容がわからなくなるため、title検索で見つかるよう個々の記事にしてます。いわゆるBorg集合体の有名なセリフから「お前たち（の知識）を吸収する。抵抗は無意味だ」。Thanks in advance.
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away