2
0

link.py 7.5 KB

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