LoginSignup
0

More than 5 years have passed since last update.

はてなダイアリー to Markdown > v0.5 > Tex:表記対応

Last updated at Posted at 2018-05-28

はてなダイアリー to Markdown > v0.4 > テーブル表記対応
の続き

v0.1はこちら

関連

code v0.5

[Tex:Z_c = \frac{1}{2 \pi f C}]

を下記のように置換する。


```math
Tex:Z_c = \frac{1}{2 \pi f C}
```

import sys
import re

# on Python 3.5

'''
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:
        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("||<", "```")
    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='')

下記のようになる。


|A|B|C|
|:-:|:-:|:-:|
|A|B|C|
|A|B|C|
テスト
|A|B|C|
|:-:|:-:|:-:|
|A|B|C|
|A|B|C|

- A
    - B
        - C

```cpp
// some cpp program
```

```math
Z_c = \frac{1}{2 \pi f C}
```

```math
Z_c = \frac{1}{\omega C}
```

[[[AAA]]]

code v0.5のバグ

  • //-----というコメントは// - - - - -に変換される

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
0