namumark.py 55 KB

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