link.py 6.7 KB

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