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