link.py 6.7 KB

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