Ver Fonte

마크다운 문법 구현 시작

Surplus_Up (2DU) há 6 anos atrás
pai
commit
e9229fb228
3 ficheiros alterados com 52 adições e 2 exclusões
  1. 4 1
      route/tool/mark.py
  2. 47 0
      route/tool/set_mark/markdown.py
  3. 1 1
      route/tool/set_mark/namumark.py

+ 4 - 1
route/tool/mark.py

@@ -1,4 +1,5 @@
-from .set_mark.namumark import namumark, link_fix
+from .set_mark.namumark import namumark
+from .set_mark.markdown import markdown
 
 from .set_mark.tool import *
 
@@ -49,6 +50,8 @@ def render_do(title, data, num, include):
     rep_data = curs.fetchall()
     if rep_data[0][0] == 'namumark':
         data = namumark(conn, data, title, include)
+    elif rep_data[0][0] == 'markdown':
+        data = markdown(conn, data, title, include)
     elif rep_data[0][0] == 'custom':
         data = custom_mark(conn, data, title, include)
     elif rep_data[0][0] == 'raw':

+ 47 - 0
route/tool/set_mark/markdown.py

@@ -0,0 +1,47 @@
+from . import tool
+
+import datetime
+import html
+import re
+
+def markdown(conn, data, title, include_num):
+    backlink = []
+    include_num = include_num + '_' if include_num else ''
+    plus_data = '' + \
+        'get_link_state("' + include_num + '");\n' + \
+        'get_file_state("' + include_num + '");\n' + \
+    ''
+
+    data = html.escape(data)
+    data = data.replace('\r\n', '\n')
+    data = '\n' + data
+
+    class head_render:
+        def __init__(self):
+            pass
+
+        def __call__(self, match):
+            head_len = str(len(match[1]))
+            head_data = match[2]
+
+            return '<h' + head_len + '>' + head_data + '</h' + head_len + '>'
+
+    head_r = r'\n(#{1,6}) ?([^\n]+)'
+    head_do = head_render()
+    data = re.sub(head_r, head_do, data)
+
+    class link_render:
+        def __init__(self):
+            pass
+
+        def __call__(self, match):
+            if re.search(r'^http(s)?:\/\/', match[2], flags = re.I):
+                return '<a id="out_link" href="' + match[2] + '">' + match[1] + '</a>'
+            else:
+                return '<a class="' + include_num + 'link_finder" href="/w/' + match[2] + '">' + match[1] + '</a>'
+
+    link_r = r'\[((?:(?!\]\().)+)\]\(([^\]]+)\)'
+    link_do = link_render()
+    data = re.sub(link_r, link_do, data)
+    
+    return [data, plus_data, backlink]

+ 1 - 1
route/tool/set_mark/namumark.py

@@ -506,7 +506,7 @@ def namumark(conn, data, title, include_num):
     data = re.sub('<math>(?P<in>(?:(?!<\/math>).)+)<\/math>', '[math(\g<in>)]', data)
 
     data = html.escape(data)
-    data = re.sub('\r\n', '\n', data)
+    data = data.replace('\r\n', '\n')
 
     math_re = re.compile('\[math\(((?:(?!\)\]).)+)\)\]', re.I)
     while 1: