namumark.py 53 KB

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