link.py 6.7 KB

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