link.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import sqlite3
  2. import re
  3. from urllib import parse
  4. import hashlib
  5. def url_pas(data):
  6. return parse.quote(data).replace('/','%2F')
  7. def sha224(data):
  8. return hashlib.sha224(bytes(data, 'utf-8')).hexdigest()
  9. def link(conn, title, data, num, category, backlink):
  10. curs = conn.cursor()
  11. data = data.replace('\', '\\')
  12. m = re.findall("\[\[(분류:(?:(?:(?!\]\]|#).)+))((?:#(?:(?:(?!#|\]\]).)+))+)?\]\]", data)
  13. for g in m:
  14. if title != g[0]:
  15. if num == 1:
  16. backlink += [[title, g[0], 'cat']]
  17. curs.execute("select title from data where title = ?", [g[0]])
  18. if curs.fetchall():
  19. red = ""
  20. else:
  21. red = 'class="not_thing"'
  22. if(category != ''):
  23. category += ' / '
  24. style = ''
  25. title_name = re.sub("분류:", "", g[0])
  26. if g[1]:
  27. if re.search('#blur', g[1]):
  28. style = ' onmouseover="this.innerHTML=\'' + title_name + '\';"'
  29. title_name = '<span id="inside">스포일러</span>'
  30. category += '<a ' + red + ' ' + style + ' href="/w/' + url_pas(g[0]) + '">' + title_name + '</a>'
  31. data = re.sub("\[\[(분류:(?:(?:(?!\]\]|#).)+))((?:#(?:(?:(?!#|\]\]).)+))+)?\]\]", '', data, 1)
  32. test = re.findall('\[\[wiki:([^|\]]+)(?:\|([^\]]+))?\]\]', data)
  33. for wiki in test:
  34. if wiki[1]:
  35. out = wiki[1]
  36. else:
  37. out = wiki[0]
  38. data = re.sub('\[\[wiki:([^|\]]+)(?:\|([^\]]+))?\]\]', '<a id="inside" href="/' + wiki[0] + '">' + out + '</a>', data, 1)
  39. test = re.findall('\[\[inter:([^:]+):((?:(?!\||]]).)+)(?:\|([^\]]+))?]]', data)
  40. for wiki in test:
  41. curs.execute('select link from inter where title = ?', [wiki[0]])
  42. inter = curs.fetchall()
  43. if not inter:
  44. data = re.sub('\[\[inter:([^:]+):((?:(?!\||]]).)+)(?:\|([^\]]+))?]]', '인터위키 정보 없음', data, 1)
  45. else:
  46. if wiki[2]:
  47. out = wiki[0] + ':' + wiki[2]
  48. else:
  49. out = wiki[0] + ':' + wiki[1]
  50. data = re.sub('\[\[inter:([^:]+):((?:(?!\||]]).)+)(?:\|([^\]]+))?]]', '<a id="inside" href="' + inter[0][0] + wiki[1] + '">' + out + '</a>', data, 1)
  51. data = re.sub("\[\[(?::(?P<in>(?:분류|파일):(?:(?:(?!\]\]).)*)))\]\]", "[[\g<in>]]", data)
  52. a = re.findall('\[\[\.\.\/(\|(?:(?!]]).)+)?]]', data)
  53. for i in a:
  54. b = re.search('(.*)\/', title)
  55. if b:
  56. m = b.groups()
  57. if i:
  58. data = re.sub('\[\[\.\.\/(\|((?!]]).)+)?]]', '[[' + m[0] + i + ']]', data, 1)
  59. else:
  60. data = re.sub('\[\[\.\.\/(\|((?!]]).)+)?]]', '[[' + m[0] + ']]', data, 1)
  61. else:
  62. if i:
  63. data = re.sub('\[\[\.\.\/(\|((?!]]).)+)?]]', '[[' + title + i + ']]', data, 1)
  64. else:
  65. data = re.sub('\[\[\.\.\/(\|((?!]]).)+)?]]', '[[' + title + ']]', data, 1)
  66. data = re.sub('\[\[(?P<in>\/(?:(?!]]|\|).)+)(?P<out>\|(?:(?:(?!]]).)+))?]]', '[[' + title + '\g<in>\g<out>]]', data)
  67. link = re.compile('\[\[((?:(?!\[\[|\]\]|\|).)*)(?:\|((?:(?!\[\[|\]\]).)*))?\]\]')
  68. while 1:
  69. l_d = link.search(data)
  70. if l_d:
  71. d = l_d.groups()
  72. if re.search('^(?:파일|외부):', d[0]):
  73. width = ''
  74. height = ''
  75. align = ''
  76. span = ['', '']
  77. try:
  78. w_d = re.search('width=([0-9]+(?:[a-z%]+)?)', d[1])
  79. if w_d:
  80. width = 'width="' + w_d.groups()[0] + '" '
  81. h_d = re.search('height=([0-9]+(?:[a-z%]+)?)', d[1])
  82. if h_d:
  83. height = 'height="' + h_d.groups()[0] + '" '
  84. a_d = re.search('align=(center|right)', d[1])
  85. if a_d:
  86. span[0] = '<span style="display: block; text-align: ' + a_d.groups()[0] + ';">'
  87. span[1] = '</span>'
  88. except:
  89. pass
  90. f_d = re.search('^파일:([^.]+)\.(.+)$', d[0])
  91. if f_d:
  92. if not re.search("^파일:([^\n]*)", title):
  93. if num == 1:
  94. backlink += [[title, d[0], 'file']]
  95. file_name = f_d.groups()
  96. curs.execute("select title from data where title = ?", ['파일:' + file_name[0] + '.' + file_name[1]])
  97. if not curs.fetchall():
  98. img = '<a class="not_thing" href="/w/' + url_pas('파일:' + file_name[0] + '.' + file_name[1]) + '">파일:' + file_name[0] + '.' + file_name[1] + '</a>'
  99. else:
  100. img = span[0] + '<img src="/image/' + sha224(file_name[0]) + '.' + file_name[1] + '" ' + width + height + '>' + span[1]
  101. data = link.sub(img, data, 1)
  102. else:
  103. img = span[0] + '<img src="' + re.sub('^외부:', '', d[0]) + '" ' + width + height + '>' + span[1]
  104. data = link.sub(img, data, 1)
  105. elif re.search('^https?:\/\/', re.sub('<([^>]*)>', '', d[0])):
  106. view = d[0]
  107. try:
  108. if re.search('(.+)', d[1]):
  109. view = d[1]
  110. except:
  111. pass
  112. data = link.sub('<a class="out_link" rel="nofollow" href="' + re.sub('<([^>]*)>', '', d[0]) + '">' + view + '</a>', data, 1)
  113. else:
  114. view = d[0].replace('\\\\', '<slash>').replace('\\', '').replace('<slash>', '\\')
  115. try:
  116. if re.search('(.+)', d[1]):
  117. view = d[1].replace('\\\\', '<slash>').replace('\\', '').replace('<slash>', '\\')
  118. except:
  119. pass
  120. sh = ''
  121. s_d = re.search('#((?:(?!x27;|#).)+)$', d[0])
  122. if s_d:
  123. href = re.sub('#((?:(?!x27;|#).)+)$', '', d[0])
  124. sh = '#' + s_d.groups()[0]
  125. else:
  126. href = d[0]
  127. if d[0] == title:
  128. data = link.sub('<b>' + view + '</b>', data, 1)
  129. elif re.search('^#', d[0]):
  130. data = link.sub('<a title="' + sh + '" href="' + sh + '">' + view + '</a>', data, 1)
  131. else:
  132. a = re.sub('<([^>]*)>', '', href.replace('&#x27;', "'").replace('&quot;', '"').replace('\\\\', '<slash>').replace('\\', '').replace('<slash>', '\\'))
  133. if num == 1:
  134. backlink += [[title, a, '']]
  135. curs.execute("select title from data where title = ?", [a])
  136. if not curs.fetchall():
  137. no = 'class="not_thing"'
  138. if num == 1:
  139. backlink += [[title, a, 'no']]
  140. else:
  141. no = ''
  142. data = link.sub('<a ' + no + ' title="' + re.sub('<([^>]*)>', '', href) + sh + '" href="/w/' + url_pas(a) + sh + '">' + view.replace('\\', '\\\\') + '</a>', data, 1)
  143. else:
  144. break
  145. data = data.replace('\\', '&#92;')
  146. return [data, category, backlink]