namumark.py 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  1. from . import tool
  2. import datetime
  3. import html
  4. import re
  5. def nowiki_js(data):
  6. data = data.replace('\\', '\\\\')
  7. data = data.replace('"', '\\"')
  8. data = data.replace('\r', '')
  9. data = re.sub(r'^\n', '', data)
  10. data = data.replace('\n', '<br>')
  11. return data
  12. def link_fix(main_link, no_change = 0):
  13. global end_data
  14. main_link = main_link.replace('&#x27;', "<link_comma>")
  15. if re.search(r'^:', main_link):
  16. main_link = re.sub(r'^:', '', main_link)
  17. if no_change == 0:
  18. main_link = re.sub(r'^사용자:', 'user:', main_link)
  19. main_link = re.sub(r'^파일:', 'file:', main_link)
  20. main_link = re.sub(r'^분류:', 'category:', main_link)
  21. other_link = re.search(r'[^\\]?(#[^#]+)$', main_link)
  22. if other_link:
  23. other_link = other_link.group(1)
  24. main_link = re.sub(r'(#[^#]+)$', '', main_link)
  25. else:
  26. other_link = ''
  27. main_link = main_link.replace("<link_comma>", "&#x27;")
  28. main_link = re.sub(r'\\#', '%23', main_link)
  29. find_data = re.findall(r'<span id="(nowiki_[0-9]+)">', main_link)
  30. for i in find_data:
  31. main_link = main_link.replace('<span id="' + i + '"></span>', end_data[i])
  32. find_data = re.findall(r'<span id="(nowiki_[0-9]+)">', other_link)
  33. for i in find_data:
  34. other_link = other_link.replace('<span id="' + i + '"></span>', end_data[i])
  35. return [main_link, other_link]
  36. def table_parser(data, cel_data, cel_num, start_data, num = 0, cel_color = {}):
  37. table_class = 'class="'
  38. div_style = 'style="'
  39. all_table = 'style="'
  40. cel_style = 'style="'
  41. row_style = 'style="'
  42. row = ''
  43. cel = 'colspan="' + str(round(len(start_data) / 2)) + '"'
  44. if not cel_num in cel_color:
  45. cel_color[cel_num] = ''
  46. cel_style += cel_color[cel_num]
  47. if num == 0:
  48. if re.search(r'^ ', cel_data) and re.search(r' $', cel_data):
  49. cel_style += 'text-align: center;'
  50. elif re.search(r'^ ', cel_data):
  51. cel_style += 'text-align: right;'
  52. elif re.search(r' $', cel_data):
  53. cel_style += 'text-align: left;'
  54. table_state = re.findall(r'&lt;((?:(?!&gt;).)+)&gt;', data)
  55. for in_state in table_state:
  56. if re.search(r"^table ?width=([^=]+)$", in_state):
  57. table_data = re.sub(r'^table ?width=', '', in_state)
  58. div_style += 'width: ' + ((table_data + 'px') if re.search(r'^[0-9]+$', table_data) else table_data) + ';'
  59. all_table += 'width: 100%;'
  60. elif re.search(r"^table ?height=([^=]+)$", in_state):
  61. table_data = re.sub(r'^table ?height=', '', in_state)
  62. div_style += 'height: ' + ((table_data + 'px') if re.search(r'^[0-9]+$', table_data) else table_data) + ';'
  63. elif re.search(r"^table ?align=([^=]+)$", in_state):
  64. table_data = re.sub(r'^table ?align=', '', in_state)
  65. if table_data == 'right':
  66. div_style += 'float: right;'
  67. elif table_data == 'center':
  68. div_style += 'margin: auto;'
  69. all_table += 'margin: auto;'
  70. elif re.search(r"^table ?textalign=([^=]+)$", in_state):
  71. num = 1
  72. table_data = re.sub(r'^table ?textalign=', '', in_state)
  73. if table_data == 'right':
  74. all_table += 'text-align: right;'
  75. elif table_data == 'center':
  76. all_table += 'text-align: center;'
  77. elif re.search(r"^row ?textalign=([^=]+)$", in_state):
  78. table_data = re.sub(r'^row ?textalign=', '', in_state)
  79. if table_data == 'right':
  80. row_style += 'text-align: right;'
  81. elif table_data == 'center':
  82. row_style += 'text-align: center;'
  83. else:
  84. row_style += 'text-align: left;'
  85. elif re.search(r'^-([0-9]+)$', in_state):
  86. cel = 'colspan="' + re.sub(r'^-', '', in_state) + '"'
  87. elif re.search(r"^(\^|v)?\|([^|]+)$", in_state):
  88. if re.search(r'^\^', in_state):
  89. cel_style += 'vertical-align: top;'
  90. elif re.search(r'^v', in_state):
  91. cel_style += 'vertical-align: bottom;'
  92. row = 'rowspan="' + re.sub(r'^(\^|v)?\|', '', in_state) + '"'
  93. elif re.search(r"^row ?bgcolor=([^=]+)$", in_state):
  94. table_data = re.sub(r'^row ?bgcolor=', '', in_state)
  95. row_style += 'background: ' + (re.sub(',([^,]*)', '', table_data) if re.search(r',', table_data) else table_data) + ';'
  96. elif re.search(r"^row ?color=([^=]+)$", in_state):
  97. table_data = re.sub(r'^row ?color=', '', in_state)
  98. row_style += 'color: ' + (re.sub(',([^,]*)', '', table_data) if re.search(r',', table_data) else table_data) + ';'
  99. elif re.search(r"^table ?bordercolor=([^=]+)$", in_state):
  100. table_data = re.sub(r'^table ?bordercolor=', '', in_state)
  101. all_table += 'border: ' + (re.sub(',([^,]*)', '', table_data) if re.search(r',', table_data) else table_data) + ' 2px solid;'
  102. elif re.search(r"^table ?bgcolor=([^=]+)$", in_state):
  103. table_data = re.sub(r'^table ?bgcolor=', '', in_state)
  104. all_table += 'background: ' + (re.sub(',([^,]*)', '', table_data) if re.search(r',', table_data) else table_data) + ';'
  105. elif re.search(r"^table ?color=([^=]+)$", in_state):
  106. table_data = re.sub(r'^table ?color=', '', in_state)
  107. all_table += 'color: ' + (re.sub(',([^,]*)', '', table_data) if re.search(r',', table_data) else table_data) + ';'
  108. elif re.search(r"^col ?bgcolor=([^=]+)$", in_state):
  109. table_data = re.sub(r'^col ?bgcolor=', '', in_state)
  110. table_data = (re.sub(',([^,]*)', '', table_data) if re.search(r',', table_data) else table_data)
  111. cel_color[cel_num] += 'background: ' + table_data + ';'
  112. cel_style += 'background: ' + table_data + ';'
  113. elif re.search(r"^col ?color=([^=]+)$", in_state):
  114. table_data = re.sub(r'^col ?color=', '', in_state)
  115. table_data = (re.sub(',([^,]*)', '', table_data) if re.search(r',', table_data) else table_data)
  116. cel_color[cel_num] += 'color: ' + table_data + ';'
  117. cel_style += 'color: ' + table_data + ';'
  118. elif re.search(r"^(bgcolor=([^=]+)|#(?:[0-9a-f-A-F]{3}){1,2}|\w+)$", in_state):
  119. table_data = re.sub(r'^bgcolor=', '', in_state)
  120. cel_style += 'background: ' + (re.sub(',([^,]*)', '', table_data) if re.search(r',', table_data) else table_data) + ';'
  121. elif re.search(r"^color=([^=]+)$", in_state):
  122. table_data = re.sub(r'^color=', '', in_state)
  123. cel_style += 'color: ' + (re.sub(',([^,]*)', '', table_data) if re.search(r',', table_data) else table_data) + ';'
  124. elif re.search(r"^width=([^=]+)$", in_state):
  125. table_data = re.sub(r'^width=', '', in_state)
  126. cel_style += 'width: ' + ((table_data + 'px') if re.search(r'^[0-9]+$', table_data) else table_data) + ';'
  127. elif re.search(r"^height=([^=]+)$", in_state):
  128. table_data = re.sub(r'^height=', '', in_state)
  129. cel_style += 'height: ' + ((table_data + 'px') if re.search(r'^[0-9]+$', table_data) else table_data) + ';'
  130. elif re.search(r'^\(|:|\)$', in_state):
  131. if in_state == '(':
  132. cel_style += 'text-align: right;'
  133. elif in_state == ':':
  134. cel_style += 'text-align: center;'
  135. else:
  136. cel_style += 'text-align: left;'
  137. elif re.search(r"^table ?class=([^=]+)$", in_state):
  138. table_class += re.sub(r"^table ?class=", '', in_state)
  139. div_style += '"'
  140. all_table += '"'
  141. cel_style += '"'
  142. row_style += '"'
  143. table_class += '"'
  144. return [all_table, row_style, cel_style, row, cel, table_class, num, div_style, cel_color]
  145. def table_start(data):
  146. data = re.sub(r'\n( +)\|\|', '\n||', data)
  147. data = re.sub(r'\|\|( +)\n', '||\n', data)
  148. data = re.sub(r'(\|\|)+\n', '||\n', data)
  149. while 1:
  150. cel_num = 0
  151. table_num = 0
  152. table_end = ''
  153. cel_color = {}
  154. table = re.search(r'\n((?:(?:(?:(?:\|\||\|[^|]+\|)+(?:(?:(?!\|\|).\n*)*))+)\|\|(?:\n)?)+)', data)
  155. if table:
  156. table = re.sub(r'(\|\|)+\n', '||\n', table.group(1))
  157. table_caption = re.search(r'^\|([^|]+)\|', table)
  158. if table_caption:
  159. table_caption = '<caption>' + table_caption.group(1) + '</caption>'
  160. table = re.sub(r'^\|([^|]+)\|', '||', table)
  161. else:
  162. table_caption = ''
  163. table = '\n' + table
  164. table_cel = re.findall(r'(\n(?:(?:\|\|)+)|\|\|\n(?:(?:\|\|)+)|(?:(?:\|\|)+))((?:(?:(?!\n|\|\|).)+\n*)+)', table)
  165. for i in table_cel:
  166. cel_plus = re.search(r'^((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)', i[1])
  167. cel_plus = cel_plus.group(1) if cel_plus else ''
  168. cel_data = re.sub(r'^((?:&lt;(?:(?:(?!&gt;).)*)&gt;)+)', '', i[1])
  169. if re.search(r'^\n', i[0]):
  170. cel_num = 1
  171. cel_plus = table_parser(
  172. cel_plus,
  173. cel_data,
  174. cel_num,
  175. re.sub(r'^\n', '', i[0]),
  176. table_num,
  177. cel_color
  178. )
  179. cel_color = cel_plus[8]
  180. table_num = cel_plus[6]
  181. table_end += '' + \
  182. '<div class="table_safe" ' + cel_plus[7] + '>' + \
  183. '<table ' + cel_plus[5] + ' ' + cel_plus[0] + '>' + \
  184. table_caption + \
  185. '<tr ' + cel_plus[1] + '>' + \
  186. '<td ' + cel_plus[2] + ' ' + cel_plus[3] + ' ' + cel_plus[4] + '>' + \
  187. cel_data
  188. elif re.search(r'\n', i[0]):
  189. cel_num = 1
  190. cel_plus = table_parser(
  191. cel_plus,
  192. cel_data,
  193. cel_num,
  194. re.sub(r'^\|\|\n', '', i[0]),
  195. table_num,
  196. cel_color
  197. )
  198. cel_color = cel_plus[8]
  199. table_end += '' + \
  200. '</td>' + \
  201. '</tr>' + \
  202. '<tr ' + cel_plus[1] + '>' + \
  203. '<td ' + cel_plus[2] + ' ' + cel_plus[3] + ' ' + cel_plus[4] + '>' + \
  204. cel_data
  205. else:
  206. cel_num += 1
  207. cel_plus = table_parser(
  208. cel_plus,
  209. cel_data,
  210. cel_num,
  211. re.sub(r'^\|\|\n', '', i[0]),
  212. table_num,
  213. cel_color
  214. )
  215. cel_color = cel_plus[8]
  216. table_end += '' + \
  217. '</td>' + \
  218. '<td ' + cel_plus[2] + ' ' + cel_plus[3] + ' ' + cel_plus[4] + '>' + \
  219. cel_data
  220. table_end += '</td></tr></table></div>'
  221. data = re.sub(r'\n((?:(?:(?:(?:\|\||\|[^|]+\|)+(?:(?:(?!\|\|).\n*)*))+)\|\|(?:\n)?)+)', '\n' + table_end + '\n', data, 1)
  222. else:
  223. break
  224. return data.replace('||', '<no_table>')
  225. def middle_parser(data):
  226. global end_data
  227. global plus_data
  228. global nowiki_num
  229. global include_name
  230. middle_stack = 0
  231. middle_list = []
  232. middle_num = 0
  233. html_num = 0
  234. syntax_num = 0
  235. folding_num = 0
  236. middle_re = re.compile(r'(?:{{{((?:(?:(?! |{{{|}}}|&lt;).)*) ?)|(}}}))')
  237. middle_all_data = middle_re.findall(data)
  238. for middle_data in middle_all_data:
  239. if not middle_data[1]:
  240. if middle_stack > 0:
  241. middle_stack += 1
  242. data = re.sub(r'(?:{{{((?:(?! |{{{|}}}|&lt;).)*)(?P<in> ?)|(}}}))', '<middle_start>' + middle_data[0] + '\g<in>', data, 1)
  243. else:
  244. if re.search(r'^(#|@|\+|\-)', middle_data[0]) and not re.search(r'^(#|@|\+|\-){2}|(#|@|\+|\-)\\', middle_data[0]):
  245. if re.search(r'^(#(?:[0-9a-f-A-F]{3}){1,2})', middle_data[0]):
  246. middle_search = re.search(r'^(#(?:[0-9a-f-A-F]{3}){1,2})', middle_data[0])
  247. middle_list += ['span']
  248. data = middle_re.sub('<span style="color: ' + middle_search.group(1) + ';">', data, 1)
  249. elif re.search(r'^(?:#(\w+))', middle_data[0]):
  250. middle_search = re.search(r'^(?:#(\w+))', middle_data[0])
  251. middle_list += ['span']
  252. data = middle_re.sub('<span style="color: ' + middle_search.group(1) + ';">', data, 1)
  253. elif re.search(r'^(?:@((?:[0-9a-f-A-F]{3}){1,2}))', middle_data[0]):
  254. middle_search = re.search(r'^(?:@((?:[0-9a-f-A-F]{3}){1,2}))', middle_data[0])
  255. middle_list += ['span']
  256. data = middle_re.sub('<span style="background: #' + middle_search.group(1) + ';">', data, 1)
  257. elif re.search(r'^(?:@(\w+))', middle_data[0]):
  258. middle_search = re.search(r'^(?:@(\w+))', middle_data[0])
  259. middle_list += ['span']
  260. data = middle_re.sub('<span style="background: ' + middle_search.group(1) + ';">', data, 1)
  261. elif re.search(r'^(\+|-)([1-5])', middle_data[0]):
  262. middle_search = re.search(r'^(\+|-)([1-5])', middle_data[0])
  263. middle_search = middle_search.groups()
  264. if middle_search[0] == '+':
  265. font_size = str(int(middle_search[1]) * 20 + 100)
  266. else:
  267. font_size = str(100 - int(middle_search[1]) * 10)
  268. middle_list += ['span']
  269. data = middle_re.sub('<span style="font-size: ' + font_size + '%;">', data, 1)
  270. elif re.search(r'^#!wiki', middle_data[0]):
  271. middle_data_2 = re.search(r'{{{#!wiki(?: style=(?:&quot;|&#x27;)((?:(?!&quot;|&#x27;).)*)(?:&quot;|&#x27;))?(?: *)\n?', data)
  272. if middle_data_2:
  273. middle_data_2 = middle_data_2.groups()
  274. else:
  275. middle_data_2 = ['']
  276. middle_list += ['div_1']
  277. data = re.sub(
  278. r'{{{#!wiki(?: style=(?:&quot;|&#x27;)((?:(?!&quot;|&#x27;).)*)(?:&quot;|&#x27;))?(?: *)\n?',
  279. '<div id="wiki_div" style="' + str(middle_data_2[0] if middle_data_2[0] else '') + '">',
  280. data,
  281. 1
  282. )
  283. elif re.search(r'^#!syntax', middle_data[0]):
  284. middle_data_2 = re.search(r'{{{#!syntax ((?:(?!\n|{{{).)+)\n?', data)
  285. if middle_data_2:
  286. middle_data_2 = middle_data_2.groups()
  287. else:
  288. middle_data_2 = ['python']
  289. if syntax_num == 0:
  290. plus_data += 'hljs.initHighlightingOnLoad();\n'
  291. syntax_num = 1
  292. middle_list += ['pre']
  293. data = re.sub(
  294. r'{{{#!syntax ?((?:(?!\n|{{{).)*)\n?',
  295. '<pre id="syntax"><code class="' + middle_data_2[0] + '">',
  296. data,
  297. 1
  298. )
  299. elif re.search(r'^#!folding', middle_data[0]):
  300. middle_list += ['2div']
  301. folding_data = re.search(r'{{{#!folding ?((?:(?!\n).)*)\n?', data)
  302. if folding_data:
  303. folding_data = folding_data.groups()
  304. else:
  305. folding_data = ['Test']
  306. plus_data += '' + \
  307. 'if(document.getElementById("get_' + include_name + 'folding_' + str(folding_num) + '")) { ' + \
  308. 'document.getElementById("get_' + include_name + 'folding_' + str(folding_num) + '").innerHTML = ' + \
  309. '"' + nowiki_js(folding_data[0]) + '"; ' + \
  310. '' + \
  311. '}' + \
  312. '\n' + \
  313. ''
  314. data = re.sub(
  315. r'{{{#!folding ?((?:(?!\n).)*)\n?', '' + \
  316. '<div>' + \
  317. '<div style="display: inline-block;">' + \
  318. '<b>' + \
  319. '<a href="javascript:void(0);" ' + \
  320. 'onclick="do_open_folding(\'' + include_name + 'folding_' + str(folding_num) + '\', this);" ' + \
  321. 'id="get_' + include_name + 'folding_' + str(folding_num) + '">' + \
  322. '</a>' + \
  323. '</b>' + \
  324. '</div_2>' + \
  325. '<div id="' + include_name + 'folding_' + str(folding_num) + '" style="display: none;">' + \
  326. '<div id="wiki_div" style="">',
  327. data,
  328. 1
  329. )
  330. folding_num += 1
  331. elif re.search(r'^#!html', middle_data[0]):
  332. middle_list += ['span']
  333. html_num += 1
  334. data = middle_re.sub('<span id="' + include_name + 'render_contect_' + str(html_num) + '">', data, 1)
  335. else:
  336. middle_list += ['span']
  337. data = middle_re.sub('<span>', data, 1)
  338. else:
  339. middle_list += ['code']
  340. middle_stack += 1
  341. data = middle_re.sub('<code>' + middle_data[0].replace('\\', '\\\\'), data, 1)
  342. middle_num += 1
  343. else:
  344. if middle_list == []:
  345. data = middle_re.sub('<middle_end>', data, 1)
  346. else:
  347. if middle_stack > 0:
  348. middle_stack -= 1
  349. if middle_stack > 0:
  350. data = middle_re.sub('<middle_end>', data, 1)
  351. else:
  352. if middle_num > 0:
  353. middle_num -= 1
  354. if middle_list[middle_num] == '2div':
  355. data = middle_re.sub('</div_1></div_2></div_2>', data, 1)
  356. elif middle_list[middle_num] == 'pre':
  357. data = middle_re.sub('</code></pre>', data, 1)
  358. else:
  359. data = middle_re.sub('</' + middle_list[middle_num] + '>', data, 1)
  360. del middle_list[middle_num]
  361. while 1:
  362. if middle_list == []:
  363. break
  364. else:
  365. if middle_stack > 0:
  366. middle_stack -= 1
  367. if middle_stack > 0:
  368. data += '<middle_end>'
  369. else:
  370. if middle_num > 0:
  371. middle_num -= 1
  372. if middle_list[middle_num] == '2div':
  373. data += '</div_1></div_2></div_2>'
  374. elif middle_list[middle_num] == 'pre':
  375. data += '</code></pre>'
  376. else:
  377. data += '</' + middle_list[middle_num] + '>'
  378. del middle_list[middle_num]
  379. data = data.replace('<middle_start>', '{{{')
  380. data = data.replace('<middle_end>', '}}}')
  381. while 1:
  382. nowiki_data = re.search(r'<code>((?:(?:(?!<\/code>).)*\n*)*)<\/code>', data)
  383. if nowiki_data:
  384. nowiki_data = nowiki_data.groups()
  385. nowiki_num += 1
  386. end_data[include_name + 'nowiki_' + str(nowiki_num)] = nowiki_data[0]
  387. plus_data += '' + \
  388. 'if(document.getElementById("' + include_name + 'nowiki_' + str(nowiki_num) + '")) { ' + \
  389. 'document.getElementById("' + include_name + 'nowiki_' + str(nowiki_num) + '").innerHTML = "' + nowiki_js(nowiki_data[0]) + '"; ' + \
  390. '}' + \
  391. '\n' + \
  392. ''
  393. data = re.sub(
  394. r'<code>((?:(?:(?!<\/code>).)*\n*)*)<\/code>',
  395. '<span id="' + include_name + 'nowiki_' + str(nowiki_num) + '"></span>',
  396. data,
  397. 1
  398. )
  399. else:
  400. break
  401. while 1:
  402. syntax_data = re.search(
  403. r'<code class="([^"\'>]+)">((?:(?:(?:(?!<\/code>|<span ).)*)\n*)+)<\/code>',
  404. data
  405. )
  406. if syntax_data:
  407. syntax_data = syntax_data.groups()
  408. nowiki_num += 1
  409. end_data[include_name + 'nowiki_' + str(nowiki_num)] = syntax_data[1]
  410. plus_data += '' + \
  411. 'if(document.getElementById("' + include_name + 'nowiki_' + str(nowiki_num) + '")) { ' + \
  412. 'document.getElementById("' + include_name + 'nowiki_' + str(nowiki_num) + '").innerHTML = "' + nowiki_js(syntax_data[1]) + '"; ' + \
  413. '}' + \
  414. '\n' + \
  415. ''
  416. data = re.sub(
  417. r'<code class="([^"\'>]+)">((?:(?:(?:(?!<\/code>|<span ).)*)\n*)+)<\/code>',
  418. '<code class="' + syntax_data[0] + '"><span id="' + include_name + 'nowiki_' + str(nowiki_num) + '"></span></code>',
  419. data,
  420. 1
  421. )
  422. else:
  423. break
  424. return data
  425. def namumark(conn, data, title, include_num):
  426. curs = conn.cursor()
  427. global plus_data
  428. global end_data
  429. global nowiki_num
  430. global include_name
  431. nowiki_num = 0
  432. data = '\n' + data + '\n'
  433. include_name = include_num + '_' if include_num else ''
  434. plus_data = ''
  435. backlink = []
  436. end_data = {}
  437. data = re.sub(r'@([^=@]+)=(?P<in>[^=@]+)@', '\g<in>', data)
  438. data = re.sub(r'<math>(?P<in>(?:(?!<\/math>).)+)<\/math>', '[math(\g<in>)]', data)
  439. data = html.escape(data)
  440. data = data.replace('\r\n', '\n')
  441. math_re = re.compile(r'\[math\(((?:(?!\)\]).)+)\)\]', re.I)
  442. while 1:
  443. math = math_re.search(data)
  444. if math:
  445. math = math.group(1)
  446. math = math.replace('{', '<math_mid_1>')
  447. math = math.replace('}', '<math_mid_2>')
  448. math = math.replace('\\', '<math_slash>')
  449. data = math_re.sub('<math>' + math + '</math>', data, 1)
  450. else:
  451. break
  452. data = data.replace('\\{', '<break_middle>')
  453. data = middle_parser(data)
  454. data = data.replace('<break_middle>', '\\{')
  455. first = 0
  456. math_re = re.compile(r'<math>((?:(?!<\/math>).)+)<\/math>', re.I)
  457. while 1:
  458. math = math_re.search(data)
  459. if math:
  460. math = math.group(1)
  461. math = math.replace('<math_mid_1>', '{')
  462. math = math.replace('<math_mid_2>', '}')
  463. math = math.replace('<math_slash>', '\\')
  464. first += 1
  465. data = math_re.sub('<span id="math_' + str(first) + '"></span>', data, 1)
  466. plus_data += '' + \
  467. 'try {' + \
  468. 'katex.render(' + \
  469. '"' + nowiki_js(html.unescape(math)) + '",' + \
  470. 'document.getElementById(\"' + include_name + 'math_' + str(first) + '\")' + \
  471. ');' + \
  472. '} catch {' + \
  473. 'document.getElementById(\"' + include_name + 'math_' + str(first) + '\").innerHTML = "<span style=\'color: red;\'>' + nowiki_js(math) + '</span>";' + \
  474. '}\n' + \
  475. ''
  476. else:
  477. break
  478. num = 0
  479. while 1:
  480. one_nowiki = re.search(r'(?:\\)(.)', data)
  481. if one_nowiki:
  482. one_nowiki = one_nowiki.groups()
  483. nowiki_num += 1
  484. end_data[include_name + 'nowiki_' + str(nowiki_num)] = one_nowiki[0]
  485. plus_data += '' + \
  486. 'if(document.getElementById("' + include_name + 'nowiki_' + str(nowiki_num) + '")) { ' + \
  487. 'document.getElementById("' + include_name + 'nowiki_' + str(nowiki_num) + '").innerHTML = "' + nowiki_js(one_nowiki[0]) + '"; ' + \
  488. '}' + \
  489. '\n' + \
  490. ''
  491. data = re.sub(r'(?:\\)(.)', '<span id="' + include_name + 'nowiki_' + str(nowiki_num) + '"></span>', data, 1)
  492. else:
  493. break
  494. include_re = re.compile(r'\[include\(((?:(?!\)\]).)+)\)\]', re.I)
  495. i = 0
  496. while 1:
  497. i += 1
  498. include = include_re.search(data)
  499. if include:
  500. include = include.group(1)
  501. include_data = re.search(r'^((?:(?!,).)+)', include)
  502. if include_data:
  503. include_data = include_data.group(1)
  504. else:
  505. include_data = 'Test'
  506. include_link = include_data
  507. backlink += [[title, include_link, 'include']]
  508. data = include_re.sub('' + \
  509. '<a id="' + include_name + 'include_link" class="include_' + str(i) + '" href="/w/' + tool.url_pas(include_link) + '">(' + include_link + ')</a>' + \
  510. '<div id="' + include_name + 'include_' + str(i) + '"></div>' + \
  511. '', data, 1)
  512. include_plus_data = []
  513. while 1:
  514. include_plus = re.search(r', ?((?:(?!=).)+)=((?:(?!,).)+)', include)
  515. if include_plus:
  516. include_plus = include_plus.groups()
  517. include_data_set = include_plus[1]
  518. find_data = re.findall(r'<span id="(nowiki_[0-9]+)">', include_data_set)
  519. for j in find_data:
  520. include_data_set = include_data_set.replace('<span id="' + j + '"></span>', end_data[j])
  521. include_plus_data += [[include_plus[0], include_data_set]]
  522. include = re.sub(r', ?((?:(?!=).)+)=((?:(?!,).)+)', '', include, 1)
  523. else:
  524. break
  525. plus_data += 'load_include("' + include_link + '", "' + include_name + 'include_' + str(i) + '", ' + str(include_plus_data) + ');\n'
  526. else:
  527. break
  528. data = re.sub(r'\r\n', '\n', data)
  529. data = re.sub(r'&amp;', '&', data)
  530. data = re.sub(r'\n##(((?!\n).)+)', '', data)
  531. data = re.sub(r'<div id="wiki_div" style="">\n', '<div id="wiki_div" style="">', data)
  532. while 1:
  533. wiki_table_data = re.search(r'<div id="wiki_div" ((?:(?!>).)+)>((?:(?!<div id="wiki_div"|<\/div_1>).\n*)+)<\/div_1>', data)
  534. if wiki_table_data:
  535. wiki_table_data = wiki_table_data.groups()
  536. if re.search(r'\|\|', wiki_table_data[1]):
  537. end_parser = re.sub(r'\n$', '', re.sub(r'^\n', '', table_start('\n' + wiki_table_data[1] + '\n')))
  538. else:
  539. end_parser = wiki_table_data[1]
  540. data = re.sub(
  541. '<div id="wiki_div" ((?:(?!>).)+)>((?:(?!<div id="wiki_div"|<\/div_1>).\n*)+)<\/div_1>',
  542. '<div ' + wiki_table_data[0] + '>' + end_parser + '</div_2>',
  543. data,
  544. 1
  545. )
  546. else:
  547. break
  548. data = re.sub(r'<\/div_2>', '</div>', data)
  549. data = re.sub(r'<\/td>', '</td_1>', data)
  550. data += '\n'
  551. data = data.replace('\\', '&#92;')
  552. redirect_re = re.compile(r'\n#(?:redirect|넘겨주기) ((?:(?!\n).)+)\n', re.I)
  553. redirect = redirect_re.search(data)
  554. if redirect:
  555. redirect = redirect.group(1)
  556. return_link = link_fix(redirect)
  557. main_link = html.unescape(return_link[0])
  558. other_link = return_link[1]
  559. backlink += [[title, main_link, 'redirect']]
  560. data = redirect_re.sub(
  561. '\n' + \
  562. '<ul>' + \
  563. '<li>' + \
  564. '<a id="go_redirect_link" href="/w/' + tool.url_pas(main_link) + other_link + '">' + main_link + other_link + '</a>' + \
  565. '</li>' + \
  566. '</ul>' + \
  567. '\n',
  568. data,
  569. 1
  570. )
  571. no_toc_re = re.compile(r'\[(?:목차|toc)\((?:no)\)\]\n', re.I)
  572. toc_re = re.compile(r'\[(?:목차|toc)\]', re.I)
  573. if not no_toc_re.search(data):
  574. if not toc_re.search(data):
  575. data = re.sub(r'\n(?P<in>={1,6}) ?(?P<out>(?:(?!=).)+) ?={1,6}\n', '\n[toc]\n\g<in> \g<out> \g<in>\n', data, 1)
  576. else:
  577. data = no_toc_re.sub('', data)
  578. data = '<div class="all_in_data" id="in_data_0">' + data
  579. toc_full = 0
  580. toc_top_stack = 6
  581. toc_stack = [0, 0, 0, 0, 0, 0]
  582. edit_number = 0
  583. toc_data = '<div id="toc"><span id="toc_title">TOC</span>\n\n'
  584. while 1:
  585. toc = re.search(r'\n(={1,6}) ?((?:(?!\n).)+) ?(?:={1,6})\n', data)
  586. if toc:
  587. toc = toc.groups()
  588. toc_number = len(toc[0])
  589. edit_number += 1
  590. if toc_full > toc_number:
  591. for i in range(toc_number, 6):
  592. toc_stack[i] = 0
  593. if toc_top_stack > toc_number:
  594. toc_top_stack = toc_number
  595. toc_full = toc_number
  596. toc_stack[toc_number - 1] += 1
  597. toc_number = str(toc_number)
  598. all_stack = ''
  599. for i in range(0, 6):
  600. all_stack += str(toc_stack[i]) + '.'
  601. while 1:
  602. if re.search(r'[^0-9]0\.', all_stack):
  603. all_stack = re.sub(r'[^0-9]0\.', '.', all_stack)
  604. else:
  605. break
  606. all_stack = re.sub(r'^0\.', '', all_stack)
  607. all_stack = re.sub(r'\.$', '', all_stack)
  608. new_toc_data = re.sub(r'=*$', '', toc[1])
  609. new_toc_data = re.sub(r' +$', '', new_toc_data)
  610. if re.search(r'^# ?(?P<in>[^#]+) ?#$', new_toc_data):
  611. fol_head = '+'
  612. new_toc_data = re.sub(r'^# ?(?P<in>[^#]+) ?#$', '\g<in>', new_toc_data)
  613. else:
  614. fol_head = '-'
  615. data = re.sub(
  616. '\n(={1,6}) ?((?:(?!\n).)+) ?\n',
  617. '\n' + \
  618. '</div>'
  619. '<h' + toc_number + ' id="s-' + all_stack + '">' + \
  620. '<a href="#toc">' + all_stack + '.</a> ' + new_toc_data + ' ' + \
  621. '<span style="font-size: 12px">' + \
  622. '<a href="/edit/' + tool.url_pas(title) + '?section=' + str(edit_number) + '">(Edit)</a>' + \
  623. ' ' + \
  624. '<a href="javascript:void(0);" onclick="do_open_folding(\'in_data_' + all_stack + '\', this);">' + \
  625. '(' + fol_head + ')' + \
  626. '</a>' + \
  627. '</span>' + \
  628. '</h' + toc_number + '>' + \
  629. '<div class="all_in_data"' + (' style="display: none;"' if fol_head == '+' else '') + ' id="in_data_' + all_stack + '">' + \
  630. '\n',
  631. data,
  632. 1
  633. )
  634. toc_main_data = new_toc_data
  635. toc_main_data = re.sub(r'\[\*((?:(?! |\]).)*)(?: ((?:(?!(\[\*(?:(?:(?!\]).)+)\]|\])).)+))?\]', '', toc_main_data)
  636. toc_main_data = re.sub(r'<span id="math_[0-9]"><\/span>', '(Math)', toc_main_data)
  637. toc_data += '' + \
  638. '<span style="margin-left: ' + str((toc_full - toc_top_stack) * 10) + 'px;">' + \
  639. '<a href="#s-' + all_stack + '">' + all_stack + '.</a> ' + toc_main_data + \
  640. '</span>' + \
  641. '\n' + \
  642. ''
  643. else:
  644. break
  645. toc_data += '</div>'
  646. data = toc_re.sub(toc_data, data)
  647. now_time = tool.get_time()
  648. time_data = re.search(r'^([0-9]{4}-[0-9]{2}-[0-9]{2})', now_time)
  649. time = time_data.group(1)
  650. macro_re = re.compile(r'\[([^[(]+)\(((?:(?!\[|\)]).)+)\)\]')
  651. macro_data = macro_re.findall(data)
  652. for i in macro_data:
  653. macro_name = i[0].lower()
  654. if macro_name == 'youtube' or macro_name == 'kakaotv' or macro_name == 'nicovideo':
  655. width = re.search(r', ?width=((?:(?!,).)+)', i[1])
  656. if width:
  657. video_width = width.group(1)
  658. if re.search(r'^[0-9]+$', video_width):
  659. video_width += 'px'
  660. else:
  661. video_width = '560px'
  662. height = re.search(r', ?height=((?:(?!,).)+)', i[1])
  663. if height:
  664. video_height = height.group(1)
  665. if re.search(r'^[0-9]+$', video_height):
  666. video_height += 'px'
  667. else:
  668. video_height = '315px'
  669. code = re.search(r'^((?:(?!,).)+)', i[1])
  670. if code:
  671. video_code = code.group(1)
  672. else:
  673. video_code = ''
  674. video_start = ''
  675. if macro_name == 'youtube':
  676. start = re.search(r', ?(start=(?:(?!,).)+)', i[1])
  677. if start:
  678. video_start = '?' + start.group(1)
  679. video_code = re.sub(r'^https:\/\/www\.youtube\.com\/watch\?v=', '', video_code)
  680. video_code = re.sub(r'^https:\/\/youtu\.be\/', '', video_code)
  681. video_src = 'https://www.youtube.com/embed/' + video_code
  682. elif macro_name == 'kakaotv':
  683. video_code = re.sub(r'^https:\/\/tv\.kakao\.com\/channel\/9262\/cliplink\/', '', video_code)
  684. video_code = re.sub(r'^http:\/\/tv\.kakao\.com\/v\/', '', video_code)
  685. video_src = 'https://tv.kakao.com/embed/player/cliplink/' + video_code +'?service=kakao_tv'
  686. else:
  687. video_src = 'https://embed.nicovideo.jp/watch/' + video_code
  688. data = macro_re.sub(
  689. '<iframe style="width: ' + video_width + '; height: ' + video_height + ';" src="' + video_src + video_start + '" frameborder="0" allowfullscreen></iframe>',
  690. data,
  691. 1
  692. )
  693. elif macro_name == 'anchor':
  694. data = macro_re.sub('<span id="' + i[1] + '"></span>', data, 1)
  695. elif macro_name == 'ruby':
  696. ruby_code = re.search(r'^([^,]+)', i[1])
  697. if ruby_code:
  698. ruby_code = ruby_code.group(1)
  699. else:
  700. ruby_code = 'Test'
  701. ruby_top = re.search(r'ruby=([^,]+)', i[1], flags = re.I)
  702. if ruby_top:
  703. ruby_top = ruby_top.group(1)
  704. else:
  705. ruby_top = 'Test'
  706. ruby_color = re.search(r'color=([^,]+)', i[1], flags = re.I)
  707. if ruby_color:
  708. ruby_color = 'color: ' + ruby_color.group(1) + ';'
  709. else:
  710. ruby_color = ''
  711. ruby_data = '' + \
  712. '<ruby>' + \
  713. ruby_code \
  714. + '<rp>(</rp>' + \
  715. '<rt style="' + ruby_color + '">' + ruby_top + '</rt>' + \
  716. '<rp>)</rp>' + \
  717. '</ruby>' + \
  718. ''
  719. data = macro_re.sub(ruby_data, data, 1)
  720. elif macro_name == 'age' or macro_name == 'dday':
  721. try:
  722. old = datetime.datetime.strptime(time, '%Y-%m-%d')
  723. will = datetime.datetime.strptime(i[1], '%Y-%m-%d')
  724. e_data = old - will
  725. if macro_name == 'age':
  726. data = macro_re.sub(str(int(e_data.days / 365)), data, 1)
  727. else:
  728. if re.search(r'^-', str(e_data.days)):
  729. e_day = str(e_data.days)
  730. else:
  731. e_day = '+' + str(e_data.days)
  732. data = macro_re.sub(e_day, data, 1)
  733. except:
  734. data = macro_re.sub('age-dday-error', data, 1)
  735. else:
  736. data = macro_re.sub('<macro_start>' + i[0] + '<macro_middle>' + i[1] + '<macro_end>', data, 1)
  737. data = data.replace('<macro_start>', '[')
  738. data = data.replace('<macro_middle>', '(')
  739. data = data.replace('<macro_end>', ')]')
  740. while 1:
  741. block = re.search(r'(\n(?:&gt; ?(?:(?:(?!\n).)+)?\n)+)', data)
  742. if block:
  743. block = block.group(1)
  744. block = re.sub(r'^\n&gt; ?', '', block)
  745. block = re.sub(r'\n&gt; ?', '\n', block)
  746. block = re.sub(r'\n$', '', block)
  747. data = re.sub(r'(\n(?:&gt; ?(?:(?:(?!\n).)+)?\n)+)', '\n<blockquote>' + block + '</blockquote>\n', data, 1)
  748. else:
  749. break
  750. while 1:
  751. hr = re.search(r'\n-{4,9}\n', data)
  752. if hr:
  753. data = re.sub(r'\n-{4,9}\n', '\n<hr>\n', data, 1)
  754. else:
  755. break
  756. data = re.sub(r'(?P<in>\n +\* ?(?:(?:(?!\|\|).)+))\|\|', '\g<in>\n ||', data)
  757. data = re.sub(r'(?P<in><div id="folding_(?:[0-9]+)" style="display: none;"><div style="">|<blockquote>)(?P<out> )?\* ', '\g<in>\n\g<out>* ', data)
  758. while 1:
  759. li = re.search(r'(\n(?:(?: *)\* ?(?:(?:(?!\n).)+)\n)+)', data)
  760. if li:
  761. li = li.group(1)
  762. while 1:
  763. sub_li = re.search(r'\n(?:( *)\* ?((?:(?!\n).)+))', li)
  764. if sub_li:
  765. sub_li = sub_li.groups()
  766. if len(sub_li[0]) == 0:
  767. margin = 20
  768. else:
  769. margin = len(sub_li[0]) * 20
  770. li = re.sub(r'\n(?:( *)\* ?((?:(?!\n).)+))', '<li style="margin-left: ' + str(margin) + 'px;">' + sub_li[1] + '</li>', li, 1)
  771. else:
  772. break
  773. data = re.sub(r'(\n(?:(?: *)\* ?(?:(?:(?!\n).)+)\n)+)', '\n\n<ul>' + li + '</ul>\n', data, 1)
  774. else:
  775. break
  776. data = re.sub(r'<\/ul>\n \|\|', '</ul>||', data)
  777. data = re.sub(r'\|\|</blockquote>', '</blockquote>||', data)
  778. while 1:
  779. indent = re.search(r'\n( +)', data)
  780. if indent:
  781. indent = len(indent.group(1))
  782. margin = '<span style="margin-left: 20px;"></span>' * indent
  783. data = re.sub(r'\n( +)', '\n' + margin, data, 1)
  784. else:
  785. break
  786. data = table_start(data)
  787. category = ''
  788. link_re = re.compile('\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]')
  789. category_re = re.compile(r'^(?:category|분류):', re.I)
  790. e_link_id = 0
  791. while 1:
  792. link = link_re.search(data)
  793. if link:
  794. link = link.group(1)
  795. str_e_link_id = str(e_link_id)
  796. e_link_id += 1
  797. link_split = re.search(r'((?:(?!\|).)+)(?:\|((?:(?!\|).)+))', link)
  798. if link_split:
  799. link_split = link_split.groups()
  800. main_link = link_split[0]
  801. see_link = link_split[1]
  802. inter_same = 0
  803. else:
  804. main_link = link
  805. see_link = link
  806. inter_same = 1
  807. if re.search(r'^((?:file|파일)|(?:out|외부)):', main_link):
  808. file_style = ''
  809. file_width = re.search(r'width=((?:(?!&).)+)', see_link)
  810. if file_width:
  811. file_width = file_width.group(1)
  812. if re.search(r'px$', file_width):
  813. file_style += 'width: ' + file_width + ';'
  814. else:
  815. file_style += 'width: ' + file_width + 'px;'
  816. file_height = re.search(r'height=((?:(?!&).)+)', see_link)
  817. if file_height:
  818. file_height = file_height.group(1)
  819. if re.search(r'px$', file_height):
  820. file_style += 'height: ' + file_height + ';'
  821. else:
  822. file_style += 'height: ' + file_height + 'px;'
  823. file_align = re.search(r'align=((?:(?!&).)+)', see_link)
  824. if file_align:
  825. file_align = file_align.group(1)
  826. if file_align == 'center':
  827. file_align = 'display: block; text-align: center;'
  828. else:
  829. file_align = 'float: ' + file_align + ';'
  830. else:
  831. file_align = ''
  832. file_color = re.search(r'bgcolor=((?:(?!&).)+)', see_link)
  833. if file_color:
  834. file_color = 'background: ' + file_color.group(1) + '; display: inline-block;'
  835. else:
  836. file_color = ''
  837. if re.search(r'^(?:out|외부):', main_link):
  838. file_src = re.sub(r'^(?:out|외부):', '', main_link)
  839. file_alt = main_link
  840. exist = 'Yes'
  841. else:
  842. file_data = re.search(r'^(?:file|파일):((?:(?!\.).)+)\.(.+)$', main_link)
  843. if file_data:
  844. file_data = file_data.groups()
  845. file_name = file_data[0]
  846. file_end = file_data[1]
  847. if main_link != title:
  848. backlink += [[title, main_link, 'file']]
  849. else:
  850. file_name = 'TEST'
  851. file_end = 'jpg'
  852. file_src = '/image/' + tool.sha224_replace(file_name) + '.' + file_end
  853. file_alt = 'file:' + file_name + '.' + file_end
  854. exist = None
  855. data = link_re.sub(
  856. '<span style="' + file_align + '">' + \
  857. '<span style="' + file_color + '" ' + \
  858. 'class="' + include_name + 'file_finder" ' + \
  859. 'under_style="' + file_style + '" ' + \
  860. 'under_alt="' + file_alt + '" ' + \
  861. 'under_src="' + file_src + '" ' + \
  862. 'under_href="' + ("out_link" if exist else '/upload?name=' + tool.url_pas(file_name)) + '">' + \
  863. '</span>' + \
  864. '</span>',
  865. data,
  866. 1
  867. )
  868. elif category_re.search(main_link):
  869. if category == '':
  870. category += '<div id="cate_all"><hr><div id="cate">Category : '
  871. main_link = category_re.sub('category:', main_link)
  872. link_id = ''
  873. curs.execute(tool.db_change("select title from data where title = ?"), [main_link])
  874. if re.search(r'#blur', main_link):
  875. link_id = ' hidden_link'
  876. main_link = main_link.replace('#blur', '')
  877. see_link = see_link.replace('#blur', '')
  878. backlink += [[title, main_link, 'cat']]
  879. category += '' + \
  880. '<a class="' + include_name + 'link_finder' + link_id + '" ' + \
  881. 'href="/w/' + tool.url_pas(main_link) + '">' + \
  882. category_re.sub('', see_link) + \
  883. '</a> | ' + \
  884. ''
  885. data = link_re.sub('', data, 1)
  886. elif re.search(r'^inter:((?:(?!:).)+):', main_link):
  887. inter_data = re.search(r'^inter:((?:(?!:).)+):((?:(?!\]\]).)+)', main_link)
  888. inter_data = inter_data.groups()
  889. curs.execute(tool.db_change('select link, icon from inter where title = ?'), [inter_data[0]])
  890. inter = curs.fetchall()
  891. if inter:
  892. return_link = link_fix(inter_data[1], 1)
  893. main_link = html.unescape(return_link[0])
  894. other_link = return_link[1]
  895. inter_view = inter[0][1] if inter[0][1] != '' else (inter_data[0] + ':')
  896. data = link_re.sub('' + \
  897. '<a id="inside" ' + \
  898. 'name="' + include_name + 'set_link_' + str_e_link_id + '" ' + \
  899. 'href="">' + inter_view + see_link + '</a>' + \
  900. '',
  901. data,
  902. 1
  903. )
  904. plus_data += "" + \
  905. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].href = '" + \
  906. (inter[0][0] + tool.url_pas(main_link) + other_link).replace('\'', '\\\'') + "';" + \
  907. "\n" + \
  908. ""
  909. if inter_same == 1:
  910. plus_data += "" + \
  911. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].innerHTML = '" + \
  912. (inter_view + main_link + other_link).replace('\'', '\\\'') + "';" + \
  913. "\n" + \
  914. ""
  915. else:
  916. data = link_re.sub('', data, 1)
  917. elif re.search(r'^(\/(?:.+))$', main_link):
  918. under_title = re.search(r'^(\/(?:.+))$', main_link)
  919. under_title = under_title.group(1)
  920. if see_link != main_link:
  921. data = link_re.sub('[[' + title + under_title + '|' + see_link + ']]', data, 1)
  922. else:
  923. data = link_re.sub('[[' + title + under_title + ']]', data, 1)
  924. elif re.search(r'^http(s)?:\/\/', main_link):
  925. data = link_re.sub('' + \
  926. '<a id="out_link" ' + \
  927. 'name="' + include_name + 'set_link_' + str_e_link_id + '" ' + \
  928. 'rel="nofollow" ' + \
  929. 'href="">' + see_link + '</a>' + \
  930. '',
  931. data,
  932. 1
  933. )
  934. plus_data += "" + \
  935. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].href = '" + \
  936. main_link.replace('\'', '\\\'') + "';" + \
  937. "\n" + \
  938. ""
  939. if inter_same == 1:
  940. plus_data += "" + \
  941. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].innerHTML = '" + \
  942. main_link.replace('\'', '\\\'') + "';" + \
  943. "\n" + \
  944. ""
  945. else:
  946. return_link = link_fix(main_link)
  947. main_link = html.unescape(return_link[0])
  948. other_link = return_link[1]
  949. if re.search(r'^\/', main_link):
  950. main_link = re.sub(r'^\/', title + '/', main_link)
  951. elif re.search(r'\.\.\/\/', main_link):
  952. main_link = re.sub(r'\.\.\/\/', '/', main_link)
  953. elif re.search(r'^\.\.\/', main_link):
  954. main_link = re.sub(r'^\.\.\/', re.sub(r'(?P<in>.+)\/.*$', '\g<in>', title), main_link)
  955. if main_link != title and main_link != '':
  956. backlink += [[title, main_link, '']]
  957. curs.execute(tool.db_change("select title from data where title = ?"), [main_link])
  958. if not curs.fetchall():
  959. backlink += [[title, main_link, 'no']]
  960. data = link_re.sub('' + \
  961. '<a class="' + include_name + 'link_finder" ' + \
  962. 'name="' + include_name + 'set_link_' + str_e_link_id + '" ' + \
  963. 'title="" ' + \
  964. 'href="">' + see_link + '</a>' + \
  965. '',
  966. data,
  967. 1
  968. )
  969. plus_data += "" + \
  970. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].href = '" + \
  971. ('/w/' + tool.url_pas(main_link) + other_link).replace('\'', '\\\'') + "';" + \
  972. "\n" + \
  973. ""
  974. plus_data += "" + \
  975. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].title = '" + \
  976. (html.escape(main_link) + other_link).replace('\'', '\\\'') + "';" + \
  977. "\n" + \
  978. ""
  979. if inter_same == 1:
  980. plus_data += "" + \
  981. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].innerHTML = '" + \
  982. (html.escape(main_link) + other_link).replace('\'', '\\\'') + "';" + \
  983. "\n" + \
  984. ""
  985. else:
  986. if re.search(r'^#', other_link):
  987. data = link_re.sub(
  988. '<a title="' + other_link + '" href="' + other_link + '">' + other_link + '</a>',
  989. data,
  990. 1
  991. )
  992. else:
  993. data = link_re.sub('<b>' + see_link + '</b>', data, 1)
  994. else:
  995. break
  996. if re.search(r'\[pagecount\]', data, flags = re.I):
  997. plus_data += 'page_count();\n'
  998. data = re.sub(r'\[pagecount\]', '<span class="all_page_count"></span>', data, flags = re.I)
  999. data = re.sub(r'\[date\]', now_time, data, flags = re.I)
  1000. data = re.sub(r'\[clearfix\]', '<div style="clear:both"></div>', data, flags = re.I)
  1001. data = re.sub(r'\[br\]', '<br>', data, flags = re.I)
  1002. footnote_number = 0
  1003. footnote_all = []
  1004. footnote_dict = {}
  1005. footnote_re = {}
  1006. footdata_all = '<hr><ul id="footnote_data">'
  1007. re_footnote = re.compile(r'(?:\[\*((?:(?! |\]).)*)(?: ((?:(?!(?:\[\*|\])).)+))?\]|(\[(?:각주|footnote)\]))')
  1008. while 1:
  1009. footnote = re_footnote.search(data)
  1010. if footnote:
  1011. footnote_data = footnote.groups()
  1012. if footnote_data[2]:
  1013. footnote_all.sort()
  1014. for footdata in footnote_all:
  1015. if footdata[2] == 0:
  1016. footdata_in = ''
  1017. else:
  1018. footdata_in = footdata[2]
  1019. footdata_all += '' + \
  1020. '<li>' + \
  1021. '<a href="javascript:do_open_foot(\'' + include_name + 'fn-' + str(footdata[0]) + '\', 1);" ' + \
  1022. 'id="' + include_name + 'cfn-' + str(footdata[0]) + '">' + \
  1023. '(' + footdata[1] + ')' + \
  1024. '</a> <span id="' + include_name + 'fn-' + str(footdata[0]) + '">' + footdata_in + '</span>' + \
  1025. '</li>' + \
  1026. ''
  1027. data = re_footnote.sub(footdata_all + '</ul>', data, 1)
  1028. footnote_all = []
  1029. footdata_all = '<hr><ul id="footnote_data">'
  1030. else:
  1031. footnote = footnote_data[1]
  1032. footnote_name = footnote_data[0]
  1033. if footnote_name and not footnote:
  1034. if footnote_name in footnote_dict:
  1035. footnote_re[footnote_name] += 1
  1036. foot_plus_num = str(footnote_re[footnote_name])
  1037. footshort = footnote_dict[footnote_name] + '.' + foot_plus_num
  1038. footnote_all += [[float(footshort), footshort, 0]]
  1039. data = re_footnote.sub('' + \
  1040. '<sup>' + \
  1041. '<a href="javascript:do_open_foot(\'' + include_name + 'fn-' + footshort + '\', 0);" ' + \
  1042. 'id="' + include_name + 'rfn-' + footshort + '">' + \
  1043. '(' + footnote_name + ')' + \
  1044. '</a>' + \
  1045. '</sup><span id="' + include_name + 'dfn-' + footshort + '"></span>' + \
  1046. '', data, 1)
  1047. else:
  1048. data = re_footnote.sub('<sup><a href="javascript:void(0);">(' + footnote_name + ')</a></sup>', data, 1)
  1049. else:
  1050. footnote_number += 1
  1051. if not footnote_name:
  1052. footnote_name = str(footnote_number)
  1053. footnote_dict.update({ footnote_name : str(footnote_number) })
  1054. if not footnote_name in footnote_re:
  1055. footnote_re.update({ footnote_name : 0 })
  1056. else:
  1057. footnote_re[footnote_name] += 1
  1058. footnote_all += [[footnote_number, footnote_name, footnote]]
  1059. data = re_footnote.sub('' + \
  1060. '<sup>' + \
  1061. '<a href="javascript:do_open_foot(\'' + include_name + 'fn-' + str(footnote_number) + '\', 0);" ' + \
  1062. 'id="' + include_name + 'rfn-' + str(footnote_number) + '">' + \
  1063. '(' + footnote_name + ')' + \
  1064. '</a>' + \
  1065. '</sup><span id="' + include_name + 'dfn-' + str(footnote_number) + '"></span>' + \
  1066. '', data, 1)
  1067. else:
  1068. break
  1069. data = re.sub(r'\n+$', '', data)
  1070. footnote_all.sort()
  1071. for footdata in footnote_all:
  1072. if footdata[2] == 0:
  1073. footdata_in = ''
  1074. else:
  1075. footdata_in = str(footdata[2])
  1076. footdata_all += '' + \
  1077. '<li>' + \
  1078. '<a href="javascript:do_open_foot(\'' + include_name + 'fn-' + str(footdata[0]) + '\', 1);" ' + \
  1079. 'id="' + include_name + 'cfn-' + str(footdata[0]) + '">' + \
  1080. '(' + str(footdata[1]) + ')' + \
  1081. '</a> <span id="' + include_name + 'fn-' + str(footdata[0]) + '">' + footdata_in + '</span>' + \
  1082. '</li>' + \
  1083. ''
  1084. footdata_all += '</ul>'
  1085. footdata_all = '</div>' + footdata_all
  1086. if footdata_all == '</div><hr><ul id="footnote_data"></ul>':
  1087. footdata_all = '</div>'
  1088. data = re.sub(r'\n$', footdata_all, data + '\n', 1)
  1089. data = re.sub(r'&#x27;&#x27;&#x27;(?P<in>((?!&#x27;&#x27;&#x27;).)+)&#x27;&#x27;&#x27;', '<b>\g<in></b>', data)
  1090. data = re.sub(r'&#x27;&#x27;(?P<in>((?!&#x27;&#x27;).)+)&#x27;&#x27;', '<i>\g<in></i>', data)
  1091. data = re.sub(r'~~(?P<in>(?:(?!~~).)+)~~', '<s>\g<in></s>', data)
  1092. data = re.sub(r'--(?P<in>(?:(?!--).)+)--', '<s>\g<in></s>', data)
  1093. data = re.sub(r'__(?P<in>(?:(?!__).)+)__', '<u>\g<in></u>', data)
  1094. data = re.sub(r'\^\^(?P<in>(?:(?!\^\^).)+)\^\^', '<sup>\g<in></sup>', data)
  1095. data = re.sub(r',,(?P<in>(?:(?!,,).)+),,', '<sub>\g<in></sub>', data)
  1096. if category != '':
  1097. category = re.sub(r' \| $', '', category) + '</div></div>'
  1098. data += category
  1099. data = data.replace('<no_table>', '||')
  1100. data = data.replace('</td_1>', '</td>')
  1101. data = re.sub(r'<\/ul>\n?', '</ul>', data)
  1102. data = re.sub(r'<\/pre>\n?', '</pre>', data)
  1103. data = re.sub(r'(?P<in><div class="all_in_data"(?:(?:(?!id=).)+)? id="in_data_([^"]+)">)(\n)+', '\g<in>', data)
  1104. data = data.replace('\n\n<ul>', '\n<ul>')
  1105. data = data.replace('</ul>\n\n', '</ul>')
  1106. data = re.sub(r'^(\n)+', '', data)
  1107. data = re.sub(r'(\n)+<hr><ul id="footnote_data">', '<hr><ul id="footnote_data">', data)
  1108. data = re.sub(r'(?P<in><td(((?!>).)*)>)\n', '\g<in>', data)
  1109. data = re.sub(r'(\n)?<hr>(\n)?', '<hr>', data)
  1110. data = data.replace('</ul>\n\n<ul>', '</ul>\n<ul>')
  1111. data = data.replace('</ul>\n<ul>', '</ul><ul>')
  1112. data = data.replace('\n</ul>', '</ul>')
  1113. data = data.replace('\n', '<br>')
  1114. plus_data += '' + \
  1115. 'get_link_state("' + include_name + '");\n' + \
  1116. 'get_file_state("' + include_name + '");\n' + \
  1117. ''
  1118. plus_data = 'render_html("' + include_name + 'render_contect");\n' + plus_data
  1119. return [data, plus_data, backlink]