link.py 6.9 KB

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