namumark.py 55 KB

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