python解析Markdown內容,
# 1.標題
## 1.1標題
### 1.1.1 標題
內容內容內容內容內容內容內容內
### 1.1.2 標題
內容內容內容內容內容內容內容內
### 1.1.3 標題
內容內容內容內容內容內容內容內
# 2.標題
## 2.1標題
內容內容內容內容內容內容內容內
該文章主要功能是解析markdown標題,構建父類標題與子類標題之間的對應關係。
原理概述
- “#”個數比上一層多一個或多個,默認爲上級目錄的子目錄。
- 分別記住1~6級目錄的狀態(父節點是否結束)。
- 遇到“#”沒有按每次加一個的方式遞進,當父節點沒有結束的話,就是子節點;當父節點結束,則跳過節點查詢節點目前應該到的父節點。
結果形式
根據的格式將標題解析成如下類型,
方便發到前臺生成樹狀可伸縮的菜單
[
{ “titleID”: 1, “parentID”: 0, “titleName”: “1.標題” },
{ “titleID”: 2, “parentID”: 1, “titleName”: “1.1標題” },
{ “titleID”: 3, “parentID”: 2, “titleName”: “1.1.1 標題” },
{ “titleID”: 4, “parentID”: 2, “titleName”: “1.1.2 標題” },
{ “titleID”: 5, “parentID”: 2, “titleName”: “1.1.3 標題” },
{ “titleID”: 6, “parentID”: 0, “titleName”: “2. 標題” },
{ “titleID”: 7, “parentID”: 6, “titleName”: “2.1 標題” },
……
]
# -*- coding: utf-8 -*-
import re
import os
import json
pattern = '#+\s'
heading = {
'heading1': 0,
'heading2': -1,
'heading3': -1,
'heading4': -1,
'heading5': -1,
'heading6': -1
}
def formatHeading():
heading['heading1'] = 0
heading['heading2'] = -1
heading['heading3'] = -1
heading['heading4'] = -1
heading['heading5'] = -1
heading['heading6'] = -1
def updateHeading(current, headId):
for i in range(1, 6):
if len(current) == i:
heading['heading%r' % i] = headId
def getMenu():
filename = os.getcwd()+'/title.md'
titles = []
global heading
global newHeading
headId = 1
current = None
preCurrent = '$'
parentID = 0
with open(filename, 'r') as f:
for i in f.readlines():
title = {}
if not re.match(pattern, i.strip(' \t\n')):
continue
i = i.strip(' \t\n')
current = i.split(' ')[0]
# 當前標題級別比前一個小,則當前標題的父類標題是上一個的headId
# 註釋:#越多級別越小
# 不論大多少個級別,只要父類級別大就是它的父類
if len(current) > len(preCurrent):
parentID = headId - 1
# 更新當前級別父類
updateHeading(current, parentID)
# 當前級別比父類級別大,則去heading中尋找記錄過的父類級別
# 註釋:#越少級別越大
elif len(current) < len(preCurrent):
length = len(current)
# 當在文中出現一級標題的時候還原所有父類級別到初始值
if length == 1:
formatHeading()
# 給當父類結果類賦值
parentID = 0
else:
getVal = heading['heading%r' % length]
# 如果有記錄過該級別的父類項
if getVal != -1:
parentID = getVal
# 改級別項沒有記錄則依次向上找父類,指導找到一級標題
else:
for j in range(length, 1, -1):
tempVal = heading['heading%r' % j]
if tempVal != -1:
parentID = tempVal
break
titleName = i[len(current):].strip(' \t\n')
title['titleName'] = titleName
title['titleID'] = headId
title['parentID'] = parentID
titles.append(title)
# print(headId, current, parentID)
preCurrent = current
headId += 1
# print(titles)
return titles
def writeFile(datas):
jsObj = json.dumps(datas)
fileObject = open('output/jsonFile.json', 'w')
fileObject.write(jsObj)
fileObject.close()
# def addAnchorMark(titles, planId):
# filename = os.path.join(os.getcwd(), "html", "planReport_" + str(planId) + ".html")
# anchorHtml = u''
# with open(filename, 'r') as f:
# for i in f.readlines():
# for title in titles:
# old = '>' + title['FunName'] + '<'
# new = " id='a_" + str(
# title['FunID']) + "'>" + title['FunName'] + "<"
# old = old.replace("\r", "")
# i = i.replace(old, new)
# anchorHtml += i.decode('utf8')
# return anchorHtml
if __name__ == "__main__":
writeFile(getMenu())