namumark.py 54 KB

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