namumark.py 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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="((?:include_(?:[0-9]+)_)?(?: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="((?:include_(?:[0-9]+)_)?(?: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. # 테이블 앞에 공백 있는 경우 처리 필요
  463. # 테이블 col 옵션 적용시 셀 병합 고려 필요
  464. data = re.sub(r'\n +\|\|', '\n||', data)
  465. math_re = re.compile(r'\[math\(((?:(?!\)\]).)+)\)\]', re.I)
  466. while 1:
  467. math = math_re.search(data)
  468. if math:
  469. math = math.group(1)
  470. math = math.replace('{', '<math_mid_1>')
  471. math = math.replace('}', '<math_mid_2>')
  472. math = math.replace('\\', '<math_slash>')
  473. data = math_re.sub('<math>' + math + '</math>', data, 1)
  474. else:
  475. break
  476. data = data.replace('\\{', '<break_middle>')
  477. data = middle_parser(data)
  478. data = data.replace('<break_middle>', '\\{')
  479. first = 0
  480. math_re = re.compile(r'<math>((?:(?!<\/math>).)+)<\/math>', re.I)
  481. while 1:
  482. math = math_re.search(data)
  483. if math:
  484. math = math.group(1)
  485. math = math.replace('<math_mid_1>', '{')
  486. math = math.replace('<math_mid_2>', '}')
  487. math = math.replace('<math_slash>', '\\')
  488. first += 1
  489. data = math_re.sub('<span id="math_' + str(first) + '"></span>', data, 1)
  490. plus_data += '' + \
  491. 'try {\n' + \
  492. 'katex.render("' + nowiki_js(html.unescape(math)) + '", document.getElementById(\"' + include_name + 'math_' + str(first) + '\"));\n' + \
  493. '} catch {\n' + \
  494. 'document.getElementById(\"' + include_name + 'math_' + str(first) + '\").innerHTML = "<span style=\'color: red;\'>' + nowiki_js(math) + '</span>";\n' + \
  495. '}\n' + \
  496. ''
  497. else:
  498. break
  499. num = 0
  500. while 1:
  501. one_nowiki = re.search(r'(?:\\)(.)', data)
  502. if one_nowiki:
  503. one_nowiki = one_nowiki.groups()
  504. nowiki_num += 1
  505. end_data[include_name + 'nowiki_' + str(nowiki_num)] = one_nowiki[0]
  506. plus_data += '' + \
  507. 'if(document.getElementById("' + include_name + 'nowiki_' + str(nowiki_num) + '")) {\n' + \
  508. 'document.getElementById("' + include_name + 'nowiki_' + str(nowiki_num) + '").innerHTML = "' + nowiki_js(one_nowiki[0]) + '";\n' + \
  509. '}\n' + \
  510. ''
  511. data = re.sub(r'(?:\\)(.)', '<span id="' + include_name + 'nowiki_' + str(nowiki_num) + '"></span>', data, 1)
  512. else:
  513. break
  514. include_re = re.compile(r'\[include\(((?:(?!\)\]).)+)\)\]', re.I)
  515. if include_name == '':
  516. i = 0
  517. while 1:
  518. i += 1
  519. include = include_re.search(data)
  520. if include:
  521. include = include.group(1)
  522. include_data = re.search(r'^((?:(?!,).)+)', include)
  523. if include_data:
  524. include_data = include_data.group(1)
  525. else:
  526. include_data = 'Test'
  527. include_link = include_data
  528. backlink += [[title, include_link, 'include']]
  529. data = include_re.sub('' + \
  530. '<a id="' + include_name + 'include_link" class="include_' + str(i) + '" href="/w/' + tool.url_pas(include_link) + '">(' + include_link + ')</a>' + \
  531. '<div id="' + include_name + 'include_' + str(i) + '"></div>' + \
  532. '', data, 1)
  533. include_plus_data = []
  534. while 1:
  535. include_plus = re.search(r', ?((?:(?!=).)+)=((?:(?!,).)+)', include)
  536. if include_plus:
  537. include_plus = include_plus.groups()
  538. include_data_set = include_plus[1]
  539. find_data = re.findall(r'<span id="((?:include_(?:[0-9]+)_)?(?:nowiki_[0-9]+))">', include_data_set)
  540. for j in find_data:
  541. include_data_set = include_data_set.replace('<span id="' + j + '"></span>', end_data[j])
  542. include_plus_data += [[include_plus[0], include_data_set]]
  543. include = re.sub(r', ?((?:(?!=).)+)=((?:(?!,).)+)', '', include, 1)
  544. else:
  545. break
  546. plus_data += 'load_include("' + include_link + '", "' + include_name + 'include_' + str(i) + '", ' + str(include_plus_data) + ');\n'
  547. else:
  548. break
  549. else:
  550. data = include_re.sub('', data)
  551. data = data.replace('&amp;', '&')
  552. data = re.sub(r'\n##[^\n]+', '', data)
  553. div_1_re = re.compile(r'<div_1 ([^>]+)>((?:(?!<div_1|<\/div_1>).|\n*)+)<\/div_1>')
  554. while 1:
  555. wiki_table_data = div_1_re.search(data)
  556. if wiki_table_data:
  557. wiki_table_data = wiki_table_data.groups()
  558. if re.search(r'\|\|', wiki_table_data[1]):
  559. end_parser = table_start('\n' + wiki_table_data[1] + '\n')
  560. end_parser = re.sub(r'^\n', '', end_parser)
  561. end_parser = re.sub(r'\n$', '', end_parser)
  562. else:
  563. end_parser = wiki_table_data[1]
  564. data = div_1_re.sub('<div id="wiki_div" ' + wiki_table_data[0] + '>' + end_parser + '</div_2>', data, 1)
  565. else:
  566. break
  567. data = data.replace('</div_2>', '</div>')
  568. data = data.replace('</td>', '</td_1>')
  569. data += '\n'
  570. data = data.replace('\\', '&#92;')
  571. redirect_re = re.compile(r'\n#(?:redirect|넘겨주기) ([^\n]+)', re.I)
  572. redirect = redirect_re.search(data)
  573. if redirect:
  574. redirect = redirect.group(1)
  575. return_link = link_fix(redirect)
  576. main_link = html.unescape(return_link[0])
  577. other_link = return_link[1]
  578. backlink += [[title, main_link, 'redirect']]
  579. plus_data += '' + \
  580. 'var get_link = window.location.search.match(/(?:\?|&)from=([^&]+)/);\n' + \
  581. 'var get_link_2 = window.location.pathname.match(/^\/w\//);' + \
  582. 'if(!get_link && get_link_2) {\n' + \
  583. 'window.location.href = "/w/' + tool.url_pas(main_link) + '?from=' + tool.url_pas(title) + other_link + '";\n' + \
  584. '}\n' + \
  585. ''
  586. data = redirect_re.sub('\nredirect to ' + html.escape(main_link) + other_link, data, 1)
  587. no_toc_re = re.compile(r'\[(?:목차|toc)\((?:no)\)\]\n', re.I)
  588. toc_re = re.compile(r'\[(?:목차|toc)\]', re.I)
  589. if not no_toc_re.search(data):
  590. if not toc_re.search(data):
  591. data = re.sub(r'(?P<in>\n(={1,6})(#)? ?((?:(?!(?: #=| =)).)+) ?#?(?:=+)\n)', '\n[toc]\g<in>', data, 1)
  592. else:
  593. data = no_toc_re.sub('', data)
  594. data = '<div class="all_in_data" id="in_data_0">' + data
  595. toc_stack = [0, 0, 0, 0, 0, 0]
  596. toc_num = 0
  597. toc_data = '' + \
  598. '<div id="toc">' + \
  599. '<span id="toc_title">TOC</span>' + \
  600. '<br>' + \
  601. '<br>' + \
  602. ''
  603. edit_num = 0
  604. toc_head_re = re.compile(r'\n(={1,6})(#)? ?((?:(?!(?: #=| =)).)+) ?#?(?:=+)\n')
  605. while 1:
  606. toc = toc_head_re.search(data)
  607. if toc:
  608. toc = toc.groups()
  609. edit_num += 1
  610. toc_len_num = len(toc[0])
  611. toc_len_str = str(toc_len_num)
  612. toc_len_num -= 1
  613. toc_stack[toc_len_num] += 1
  614. for i in range(toc_len_num + 1, 6):
  615. toc_stack[i] = 0
  616. edit_num_str = str(edit_num)
  617. toc_level_str = '.'.join([str(i) for i in toc_stack if i != 0])
  618. toc_fol = '+' if toc[1] else '-'
  619. data = toc_head_re.sub(
  620. '\n' + \
  621. '</div>'
  622. '<h' + toc_len_str + ' id="s-' + toc_level_str + '">' + \
  623. '<a href="#toc">' + toc_level_str + '.</a> ' + toc[2] + ' ' + \
  624. '<span style="font-size: 12px">' + \
  625. '<a href="/edit/' + tool.url_pas(title) + '?section=' + edit_num_str + '">(Edit)</a>' + \
  626. ' ' + \
  627. '<a href="javascript:void(0);" onclick="do_open_folding(\'in_data_' + edit_num_str + '\', this);">' + \
  628. '(' + toc_fol + ')' + \
  629. '</a>' + \
  630. '</span>' + \
  631. '</h' + toc_len_str + '>' + \
  632. '<div class="all_in_data"' + (' style="display: none;"' if toc_fol == '+' else '') + ' id="in_data_' + edit_num_str + '">' + \
  633. '\n',
  634. data,
  635. 1
  636. )
  637. toc_main_data = toc[2]
  638. toc_main_data = re.sub(r'\[\*((?:(?! |\]).)*)(?: ((?:(?!(\[\*(?:(?:(?!\]).)+)\]|\])).)+))?\]', '', toc_main_data)
  639. toc_main_data = re.sub(r'<span id="math_[0-9]"><\/span>', '(Math)', toc_main_data)
  640. toc_data += '' + \
  641. '<span style="margin-left:' + str(len(re.findall(r'\.', toc_level_str)) * 10) + 'px;">' + \
  642. '<a href="#s-' + toc_level_str + '">' + toc_level_str + '.</a> ' + toc_main_data + \
  643. '</span>' + \
  644. '<br>' + \
  645. ''
  646. else:
  647. break
  648. toc_data += '</div>'
  649. data = toc_re.sub(toc_data, data)
  650. macro_re = re.compile(r'\[([^[(]+)\(((?:(?!\[|\)]).)+)\)\]')
  651. macro_data = macro_re.findall(data)
  652. for i in macro_data:
  653. macro_name = i[0].lower()
  654. if macro_name == 'youtube' or macro_name == 'kakaotv' or macro_name == 'nicovideo':
  655. width = re.search(r', ?width=((?:(?!,).)+)', i[1])
  656. if width:
  657. video_width = width.group(1)
  658. if re.search(r'^[0-9]+$', video_width):
  659. video_width += 'px'
  660. else:
  661. video_width = '560px'
  662. height = re.search(r', ?height=((?:(?!,).)+)', i[1])
  663. if height:
  664. video_height = height.group(1)
  665. if re.search(r'^[0-9]+$', video_height):
  666. video_height += 'px'
  667. else:
  668. video_height = '315px'
  669. code = re.search(r'^((?:(?!,).)+)', i[1])
  670. if code:
  671. video_code = code.group(1)
  672. else:
  673. video_code = ''
  674. video_start = ''
  675. if macro_name == 'youtube':
  676. start = re.search(r', ?(start=(?:(?!,).)+)', i[1])
  677. if start:
  678. video_start = '?' + start.group(1)
  679. video_code = re.sub(r'^https:\/\/www\.youtube\.com\/watch\?v=', '', video_code)
  680. video_code = re.sub(r'^https:\/\/youtu\.be\/', '', video_code)
  681. video_src = 'https://www.youtube.com/embed/' + video_code
  682. elif macro_name == 'kakaotv':
  683. video_code = re.sub(r'^https:\/\/tv\.kakao\.com\/channel\/9262\/cliplink\/', '', video_code)
  684. video_code = re.sub(r'^http:\/\/tv\.kakao\.com\/v\/', '', video_code)
  685. video_src = 'https://tv.kakao.com/embed/player/cliplink/' + video_code +'?service=kakao_tv'
  686. else:
  687. video_src = 'https://embed.nicovideo.jp/watch/' + video_code
  688. data = macro_re.sub(
  689. '<iframe style="width: ' + video_width + '; height: ' + video_height + ';" src="' + video_src + video_start + '" frameborder="0" allowfullscreen></iframe>',
  690. data,
  691. 1
  692. )
  693. elif macro_name == 'anchor':
  694. data = macro_re.sub('<span id="' + i[1] + '"></span>', data, 1)
  695. elif macro_name == 'ruby':
  696. ruby_code = re.search(r'^([^,]+)', i[1])
  697. if ruby_code:
  698. ruby_code = ruby_code.group(1)
  699. else:
  700. ruby_code = 'Test'
  701. ruby_top = re.search(r'ruby=([^,]+)', i[1], flags = re.I)
  702. if ruby_top:
  703. ruby_top = ruby_top.group(1)
  704. else:
  705. ruby_top = 'Test'
  706. ruby_color = re.search(r'color=([^,]+)', i[1], flags = re.I)
  707. if ruby_color:
  708. ruby_color = 'color: ' + ruby_color.group(1) + ';'
  709. else:
  710. ruby_color = ''
  711. ruby_data = '' + \
  712. '<ruby>' + \
  713. ruby_code \
  714. + '<rp>(</rp>' + \
  715. '<rt style="' + ruby_color + '">' + ruby_top + '</rt>' + \
  716. '<rp>)</rp>' + \
  717. '</ruby>' + \
  718. ''
  719. data = macro_re.sub(ruby_data, data, 1)
  720. elif macro_name == 'age' or macro_name == 'dday':
  721. try:
  722. old = datetime.datetime.strptime(now_time, '%Y-%m-%d')
  723. will = datetime.datetime.strptime(i[1], '%Y-%m-%d')
  724. e_data = old - will
  725. if macro_name == 'age':
  726. data = macro_re.sub(str(int(e_data.days / 365)), data, 1)
  727. else:
  728. data = macro_re.sub((str(e_data.days) if re.search(r'^-', str(e_data.days)) else ('+' + str(e_data.days))), data, 1)
  729. except:
  730. data = macro_re.sub('age-dday-error', data, 1)
  731. else:
  732. data = macro_re.sub('<macro_start>' + i[0] + '<macro_middle>' + i[1] + '<macro_end>', data, 1)
  733. data = data.replace('<macro_start>', '[')
  734. data = data.replace('<macro_middle>', '(')
  735. data = data.replace('<macro_end>', ')]')
  736. blockquote_re = re.compile(r'(\n(?:&gt; ?(?:[^\n]*)\n)+)')
  737. while 1:
  738. blockquote = blockquote_re.search(data)
  739. if blockquote:
  740. blockquote = re.sub(r'^\n&gt; ?', '', blockquote.group(1))
  741. blockquote = re.sub(r'\n&gt; ?', '\n', blockquote)
  742. blockquote = re.sub(r'\n$', '', blockquote)
  743. data = blockquote_re.sub('\n<blockquote>' + blockquote + '</blockquote>\n', data, 1)
  744. else:
  745. break
  746. hr_re = re.compile(r'\n-{4,9}\n')
  747. while 1:
  748. if hr_re.search(data):
  749. data = hr_re.sub('\n<hr>\n', data, 1)
  750. else:
  751. break
  752. data = re.sub(r'(?P<in>\n +\* ?(?:(?:(?!\|\|).)+))\|\|', '\g<in>\n ||', data)
  753. 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)
  754. li_re = re.compile(r'(\n(?:(?: *)\* (?:[^\n]+)\n)+)')
  755. while 1:
  756. li_data = li_re.search(data)
  757. if li_data:
  758. li_data = li_data.group(1)
  759. li_end_data = ''
  760. sub_li = re.findall(r'( *)\* ([^\n]+)\n', li_data)
  761. for i in sub_li:
  762. li_end_data += '<li style="margin-left: ' + str(20 if len(i[0]) == 0 else (len(i[0]) * 20)) + 'px;">' + i[1] + '</li>'
  763. data = li_re.sub('\n\n<ul>' + li_end_data + '</ul>\n', data, 1)
  764. else:
  765. break
  766. data = re.sub(r'<\/ul>\n \|\|', '</ul>||', data)
  767. data = re.sub(r'\|\|</blockquote>', '</blockquote>||', data)
  768. while 1:
  769. indent = re.search(r'\n( +)', data)
  770. if indent:
  771. data = re.sub(r'\n( +)', '\n' + ('<span style="margin-left: 20px;"></span>' * len(indent.group(1))), data, 1)
  772. else:
  773. break
  774. data = table_start(data)
  775. category = ''
  776. link_re = re.compile('\[\[((?:(?!\[\[|\]\]|<\/td>).)+)\]\]')
  777. category_re = re.compile(r'^(?:category|분류):', re.I)
  778. e_link_id = 0
  779. while 1:
  780. link = link_re.search(data)
  781. if link:
  782. link = link.group(1)
  783. str_e_link_id = str(e_link_id)
  784. e_link_id += 1
  785. link_split = re.search(r'((?:(?!\|).)+)(?:\|((?:(?!\|).)+))', link)
  786. if link_split:
  787. link_split = link_split.groups()
  788. main_link = link_split[0]
  789. see_link = link_split[1]
  790. inter_same = 0
  791. else:
  792. main_link = link
  793. see_link = link
  794. inter_same = 1
  795. if re.search(r'^((?:file|파일)|(?:out|외부)):', main_link):
  796. file_style = ''
  797. file_width = re.search(r'width=((?:(?!&).)+)', see_link)
  798. if file_width:
  799. file_width = file_width.group(1)
  800. if re.search(r'px$', file_width):
  801. file_style += 'width: ' + file_width + ';'
  802. else:
  803. file_style += 'width: ' + file_width + 'px;'
  804. file_height = re.search(r'height=((?:(?!&).)+)', see_link)
  805. if file_height:
  806. file_height = file_height.group(1)
  807. if re.search(r'px$', file_height):
  808. file_style += 'height: ' + file_height + ';'
  809. else:
  810. file_style += 'height: ' + file_height + 'px;'
  811. file_align = re.search(r'align=((?:(?!&).)+)', see_link)
  812. if file_align:
  813. file_align = file_align.group(1)
  814. if file_align == 'center':
  815. file_align = 'display: block; text-align: center;'
  816. else:
  817. file_align = 'float: ' + file_align + ';'
  818. else:
  819. file_align = ''
  820. file_color = re.search(r'bgcolor=((?:(?!&).)+)', see_link)
  821. if file_color:
  822. file_color = 'background: ' + file_color.group(1) + '; display: inline-block;'
  823. else:
  824. file_color = ''
  825. file_alt = re.search(r'alt=((?:(?!&).)+)', see_link)
  826. if file_alt:
  827. file_alt = file_alt.group(1)
  828. else:
  829. file_alt = ''
  830. if re.search(r'^(?:out|외부):', main_link):
  831. file_src = re.sub(r'^(?:out|외부):', '', main_link)
  832. file_alt = main_link if file_alt == '' else file_alt
  833. exist = 'Yes'
  834. else:
  835. file_data = re.search(r'^(?:file|파일):((?:(?!\.).)+)\.(.+)$', main_link)
  836. if file_data:
  837. file_data = file_data.groups()
  838. file_name = file_data[0]
  839. file_end = file_data[1]
  840. if main_link != title:
  841. backlink += [[title, main_link, 'file']]
  842. else:
  843. file_name = 'TEST'
  844. file_end = 'jpg'
  845. file_src = '/image/' + tool.sha224_replace(file_name) + '.' + file_end
  846. file_alt = ('file:' + file_name + '.' + file_end) if file_alt == '' else file_alt
  847. exist = None
  848. data = link_re.sub(
  849. '<span style="' + file_align + '">' + \
  850. '<span style="' + file_color + '" ' + \
  851. 'class="' + include_name + 'file_finder" ' + \
  852. 'under_style="' + file_style + '" ' + \
  853. 'under_alt="' + file_alt + '" ' + \
  854. 'under_src="' + file_src + '" ' + \
  855. 'under_href="' + ("out_link" if exist else '/upload?name=' + tool.url_pas(file_name)) + '">' + \
  856. '</span>' + \
  857. '</span>',
  858. data,
  859. 1
  860. )
  861. elif category_re.search(main_link):
  862. if include_name == '':
  863. if category == '':
  864. category += '<div id="cate_all"><div id="cate">Category : '
  865. main_link = category_re.sub('category:', main_link)
  866. link_id = ''
  867. curs.execute(tool.db_change("select title from data where title = ?"), [main_link])
  868. if re.search(r'#blur', main_link):
  869. link_id = ' hidden_link'
  870. main_link = main_link.replace('#blur', '')
  871. see_link = see_link.replace('#blur', '')
  872. backlink += [[title, main_link, 'cat']]
  873. category += '' + \
  874. '<a class="' + include_name + 'link_finder' + link_id + '" ' + \
  875. 'href="/w/' + tool.url_pas(main_link) + '">' + \
  876. category_re.sub('', see_link) + \
  877. '</a> | ' + \
  878. ''
  879. data = link_re.sub('', data, 1)
  880. elif re.search(r'^inter:((?:(?!:).)+):', main_link):
  881. inter_data = re.search(r'^inter:((?:(?!:).)+):((?:(?!\]\]).)+)', main_link)
  882. inter_data = inter_data.groups()
  883. curs.execute(tool.db_change('select link, icon from inter where title = ?'), [inter_data[0]])
  884. inter = curs.fetchall()
  885. if inter:
  886. return_link = link_fix(inter_data[1], 1)
  887. main_link = html.unescape(return_link[0])
  888. other_link = return_link[1]
  889. inter_view = inter[0][1] if inter[0][1] != '' else (inter_data[0] + ':')
  890. data = link_re.sub('' + \
  891. '<a id="inside" ' + \
  892. 'name="' + include_name + 'set_link_' + str_e_link_id + '" ' + \
  893. 'href="">' + inter_view + see_link + '</a>' + \
  894. '',
  895. data,
  896. 1
  897. )
  898. plus_data += "" + \
  899. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].href = '" + \
  900. (inter[0][0] + tool.url_pas(main_link) + other_link).replace('\'', '\\\'') + "';" + \
  901. "\n" + \
  902. ""
  903. if inter_same == 1:
  904. plus_data += "" + \
  905. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].innerHTML = '" + \
  906. (inter_view + main_link + other_link).replace('\'', '\\\'') + "';" + \
  907. "\n" + \
  908. ""
  909. else:
  910. data = link_re.sub('', data, 1)
  911. elif re.search(r'^(\/(?:.+))$', main_link):
  912. under_title = re.search(r'^(\/(?:.+))$', main_link)
  913. under_title = under_title.group(1)
  914. if see_link != main_link:
  915. data = link_re.sub('[[' + title + under_title + '|' + see_link + ']]', data, 1)
  916. else:
  917. data = link_re.sub('[[' + title + under_title + ']]', data, 1)
  918. elif re.search(r'^http(s)?:\/\/', main_link):
  919. data = link_re.sub('' + \
  920. '<a id="out_link" ' + \
  921. 'name="' + include_name + 'set_link_' + str_e_link_id + '" ' + \
  922. 'rel="nofollow" ' + \
  923. 'href="">' + see_link + '</a>' + \
  924. '',
  925. data,
  926. 1
  927. )
  928. plus_data += "" + \
  929. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].href = '" + \
  930. main_link.replace('\'', '\\\'') + "';" + \
  931. "\n" + \
  932. ""
  933. if inter_same == 1:
  934. plus_data += "" + \
  935. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].innerHTML = '" + \
  936. main_link.replace('\'', '\\\'') + "';" + \
  937. "\n" + \
  938. ""
  939. else:
  940. return_link = link_fix(main_link)
  941. main_link = html.unescape(return_link[0])
  942. other_link = return_link[1]
  943. if re.search(r'^\/', main_link):
  944. main_link = re.sub(r'^\/', title + '/', main_link)
  945. elif re.search(r'\.\.\/\/', main_link):
  946. main_link = re.sub(r'\.\.\/\/', '/', main_link)
  947. elif re.search(r'^\.\.\/', main_link):
  948. main_link = re.sub(r'^\.\.\/', re.sub(r'(?P<in>.+)\/.*$', '\g<in>', title), main_link)
  949. if main_link != title and main_link != '':
  950. backlink += [[title, main_link, '']]
  951. curs.execute(tool.db_change("select title from data where title = ?"), [main_link])
  952. if not curs.fetchall():
  953. backlink += [[title, main_link, 'no']]
  954. data = link_re.sub('' + \
  955. '<a class="' + include_name + 'link_finder" ' + \
  956. 'name="' + include_name + 'set_link_' + str_e_link_id + '" ' + \
  957. 'title="" ' + \
  958. 'href="">' + see_link + '</a>' + \
  959. '',
  960. data,
  961. 1
  962. )
  963. plus_data += "" + \
  964. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].href = '" + \
  965. ('/w/' + tool.url_pas(main_link) + other_link).replace('\'', '\\\'') + "';" + \
  966. "\n" + \
  967. ""
  968. plus_data += "" + \
  969. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].title = '" + \
  970. (html.escape(main_link) + other_link).replace('\'', '\\\'') + "';" + \
  971. "\n" + \
  972. ""
  973. if inter_same == 1:
  974. plus_data += "" + \
  975. "document.getElementsByName('" + include_name + "set_link_" + str_e_link_id + "')[0].innerHTML = '" + \
  976. (html.escape(main_link) + other_link).replace('\'', '\\\'') + "';" + \
  977. "\n" + \
  978. ""
  979. else:
  980. if re.search(r'^#', other_link):
  981. data = link_re.sub(
  982. '<a title="' + other_link + '" href="' + other_link + '">' + (other_link if see_link == other_link else see_link) + '</a>',
  983. data,
  984. 1
  985. )
  986. else:
  987. data = link_re.sub('<b>' + see_link + '</b>', data, 1)
  988. else:
  989. break
  990. if re.search(r'\[pagecount\]', data, flags = re.I):
  991. plus_data += 'page_count();\n'
  992. data = re.sub(r'\[pagecount\]', '<span class="all_page_count"></span>', data, flags = re.I)
  993. data = re.sub(r'\[date\]', now_time, data, flags = re.I)
  994. data = re.sub(r'\[clearfix\]', '<div style="clear:both"></div>', data, flags = re.I)
  995. data = re.sub(r'\[br\]', '<br>', data, flags = re.I)
  996. # 각주 기능
  997. footnote_number = 0
  998. footnote_all = []
  999. footnote_dict = {}
  1000. footnote_re = {}
  1001. footdata_all = '<ul id="footnote_data">'
  1002. re_footnote = re.compile(r'(?:\[\*((?:(?! |\]).)*)(?: ((?:(?!(?:\[\*|\])).)+))?\]|(\[(?:각주|footnote)\]))')
  1003. while 1:
  1004. footnote = re_footnote.search(data)
  1005. if footnote:
  1006. footnote_data = footnote.groups()
  1007. if footnote_data[2]:
  1008. footnote_all.sort()
  1009. for footdata in footnote_all:
  1010. if footdata[2] == 0:
  1011. footdata_in = ''
  1012. else:
  1013. footdata_in = footdata[2]
  1014. footdata_all += '' + \
  1015. '<li>' + \
  1016. '<a href="javascript:do_open_foot(\'' + include_name + 'fn-' + str(footdata[0]) + '\', 1);" ' + \
  1017. 'id="' + include_name + 'cfn-' + str(footdata[0]) + '">' + \
  1018. '(' + footdata[1] + ')' + \
  1019. '</a> <span id="' + include_name + 'fn-' + str(footdata[0]) + '">' + footdata_in + '</span>' + \
  1020. '</li>' + \
  1021. ''
  1022. data = re_footnote.sub(footdata_all + '</ul>', data, 1)
  1023. footnote_all = []
  1024. footdata_all = '<ul id="footnote_data">'
  1025. else:
  1026. footnote = footnote_data[1]
  1027. footnote_name = footnote_data[0]
  1028. if footnote_name and not footnote:
  1029. if footnote_name in footnote_dict:
  1030. footnote_re[footnote_name] += 1
  1031. foot_plus_num = str(footnote_re[footnote_name])
  1032. footshort = footnote_dict[footnote_name] + '.' + foot_plus_num
  1033. footnote_all += [[float(footshort), footshort, 0]]
  1034. data = re_footnote.sub('' + \
  1035. '<sup>' + \
  1036. '<a href="javascript:do_open_foot(\'' + include_name + 'fn-' + footshort + '\', 0);" ' + \
  1037. 'id="' + include_name + 'rfn-' + footshort + '">' + \
  1038. '(' + footnote_name + ')' + \
  1039. '</a>' + \
  1040. '</sup><span id="' + include_name + 'dfn-' + footshort + '"></span>' + \
  1041. '', data, 1)
  1042. else:
  1043. data = re_footnote.sub('<sup><a href="javascript:void(0);">(' + footnote_name + ')</a></sup>', data, 1)
  1044. else:
  1045. footnote_number += 1
  1046. if not footnote_name:
  1047. footnote_name = str(footnote_number)
  1048. footnote_dict.update({ footnote_name : str(footnote_number) })
  1049. if not footnote_name in footnote_re:
  1050. footnote_re.update({ footnote_name : 0 })
  1051. else:
  1052. footnote_re[footnote_name] += 1
  1053. footnote_all += [[footnote_number, footnote_name, footnote]]
  1054. data = re_footnote.sub('' + \
  1055. '<sup>' + \
  1056. '<a href="javascript:do_open_foot(\'' + include_name + 'fn-' + str(footnote_number) + '\', 0);" ' + \
  1057. 'id="' + include_name + 'rfn-' + str(footnote_number) + '">' + \
  1058. '(' + footnote_name + ')' + \
  1059. '</a>' + \
  1060. '</sup><span id="' + include_name + 'dfn-' + str(footnote_number) + '"></span>' + \
  1061. '', data, 1)
  1062. else:
  1063. break
  1064. data = re.sub(r'\n+$', '', data)
  1065. footnote_all.sort()
  1066. for footdata in footnote_all:
  1067. if footdata[2] == 0:
  1068. footdata_in = ''
  1069. else:
  1070. footdata_in = str(footdata[2])
  1071. footdata_all += '' + \
  1072. '<li>' + \
  1073. '<a href="javascript:do_open_foot(\'' + include_name + 'fn-' + str(footdata[0]) + '\', 1);" ' + \
  1074. 'id="' + include_name + 'cfn-' + str(footdata[0]) + '">' + \
  1075. '(' + str(footdata[1]) + ')' + \
  1076. '</a> <span id="' + include_name + 'fn-' + str(footdata[0]) + '">' + footdata_in + '</span>' + \
  1077. '</li>' + \
  1078. ''
  1079. footdata_all += '</ul>'
  1080. footdata_all = '' if footdata_all == '<ul id="footnote_data"></ul>' else footdata_all
  1081. footdata_all = '</div>' + footdata_all
  1082. data += footdata_all
  1083. # 기본 꾸미기 문법
  1084. data = re.sub(r'&#x27;&#x27;&#x27;(?P<in>((?!&#x27;&#x27;&#x27;).)+)&#x27;&#x27;&#x27;', '<b>\g<in></b>', data)
  1085. data = re.sub(r'&#x27;&#x27;(?P<in>((?!&#x27;&#x27;).)+)&#x27;&#x27;', '<i>\g<in></i>', data)
  1086. data = re.sub(r'~~(?P<in>(?:(?!~~).)+)~~', '<s>\g<in></s>', data)
  1087. data = re.sub(r'--(?P<in>(?:(?!--).)+)--', '<s>\g<in></s>', data)
  1088. data = re.sub(r'__(?P<in>(?:(?!__).)+)__', '<u>\g<in></u>', data)
  1089. data = re.sub(r'\^\^(?P<in>(?:(?!\^\^).)+)\^\^', '<sup>\g<in></sup>', data)
  1090. data = re.sub(r',,(?P<in>(?:(?!,,).)+),,', '<sub>\g<in></sub>', data)
  1091. # 최종 처리
  1092. data += (re.sub(r' \| $', '', category) + '</div></div>') if category != '' else ''
  1093. data = data.replace('<no_table>', '||')
  1094. data = data.replace('</td_1>', '</td>')
  1095. data = re.sub(r'<\/ul>\n?', '</ul>', data)
  1096. data = re.sub(r'<\/pre>\n?', '</pre>', data)
  1097. data = re.sub(r'(?P<in><div class="all_in_data"(?:(?:(?!id=).)+)? id="in_data_([^"]+)">)(\n)+', '\g<in>', data)
  1098. data = data.replace('\n\n<ul>', '\n<ul>')
  1099. data = data.replace('</ul>\n\n', '</ul>')
  1100. data = re.sub(r'^(\n)+', '', data)
  1101. data = re.sub(r'(\n)+<ul id="footnote_data">', '<ul id="footnote_data">', data)
  1102. data = re.sub(r'(?P<in><td(((?!>).)*)>)\n', '\g<in>', data)
  1103. data = re.sub(r'(\n)?<hr>(\n)?', '<hr>', data)
  1104. data = data.replace('</ul>\n\n<ul>', '</ul>\n<ul>')
  1105. data = data.replace('</ul>\n<ul>', '</ul><ul>')
  1106. data = data.replace('\n</ul>', '</ul>')
  1107. data = data.replace('\n', '<br>')
  1108. # 파일, 링크, HTML 작동 JS
  1109. plus_data += '' + \
  1110. 'get_link_state("' + include_name + '");\n' + \
  1111. 'get_file_state("' + include_name + '");\n' + \
  1112. ''
  1113. plus_data = 'render_html("' + include_name + 'render_contect");\n' + plus_data
  1114. return [data, plus_data, backlink]