link.py 7.0 KB

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