พอดีเพิ่งเขียนเสร็จเลยเอามาปะไว้ เผื่อวันไหนจะใช้จะได้หาเจอ (ฮา) สคริปท์เป็น python เพราะว่าตอนจะรันไม่ต้องคอมไพล์ ง่ายดี (แต่อาจจะดีบั๊กลำบากนิดนึง) ใครสนใจก็แกะโค๊ดเองละกันนะครับ 😀
มีจุดที่ hard code อยู่หลายที่เหมือนกัน (โดยเฉพาะชื่อไฟล์) ว่าง ๆ ค่อยแก้ ใช้แบบนี้ไปก่อน ยังไง ๆ ก็คงไม่เขียนเป็นโปรแกรมเต็ม ๆ อยู่แล้ว
ปล. link ข้างในยังผิดอยู่ ตอนนี้ manual edit ใน text editor ไปก่อน ไม่ได้ลำบากอะไรนัก
import html.parser
class ContentParser(html.parser.HTMLParser):
tdcount = 0
count = 0
start = None
end = None
def handle_starttag(self, tag, attrs):
if(tag == 'td'):
if(self.count == 2 and self.tdcount == 0 ):
start = self.getpos()
self.start = (start[0] , start[1] + len(self.get_starttag_text()) )
print(self.start)
self.tdcount = self.tdcount + 1
def handle_endtag(self, tag):
if(tag == 'td'):
self.tdcount = self.tdcount - 1
if(self.tdcount == 0):
if( self.count == 2):
self.end = self.getpos()
print(self.end)
self.count = self.count + 1
def gen_content(self, lines):
lineno = self.start[0] - 1
output = ""
while(lineno <= self.end[0] -1):
start = 0
end = len(lines[lineno])
if(lineno == self.start[0] - 1):
start = self.start[1]
if(lineno == self.end[0] - 1) :
end = self.end[1]
output = output + lines[lineno][start:end] + "n"
lineno = lineno+1
return output
class HhcParser(html.parser.HTMLParser):
parsed = 0
output = ""
def handle_starttag(self, tag, attrs):
if (tag == "param" and attrs[0][1] == "Local"):
# if(self.parsed == 4) : return
htmlFile = open(attrs[1][1])
print("Processing : " + attrs[1][1])
lines = htmlFile.readlines()
parser = ContentParser()
for line in lines :
parser.feed(line)
self.output = self.output + parser.gen_content(lines) + "n"
parser.close()
htmlFile.close()
self.parsed = self.parsed + 1
return 1
hhcFile = open('0672325969.hhc')
output = open('output/index.html', 'w')
parser = HhcParser()
output.write('')
line = hhcFile.readline()
while (line != ""):
parser.feed(line)
line = hhcFile.readline()
parser.close()
hhcFile.close()
output.write( parser.output)
output.write(' ')
output.close()