Skip to content

Instantly share code, notes, and snippets.

@shinyaa31
Last active November 15, 2018 05:34
Show Gist options
  • Save shinyaa31/171302ee7a77acc93e31c428aef7ccb5 to your computer and use it in GitHub Desktop.
Save shinyaa31/171302ee7a77acc93e31c428aef7ccb5 to your computer and use it in GitHub Desktop.
HTMLの見出しタグ情報から目次テキストを生成.
def processFile(InputFile):
"""
入力ファイルの内容を解析&目次部分の出力
"""
# それぞれの見出しを目次情報として活用するか否か.
useH1tag = False
useH2tag = True
useH3tag = True
useH4tag = True
useH5tag = False
inputHTML = open(InputFile, 'r')
# 見出しタグがあれば目次遷移用のソースコードを作成
for line in inputHTML:
if (len(line) >= 4):
bol = line[0:3]
if (useH1tag and bol == '<h1'):
print((' ' * 0) + getTocLine(line))
elif (useH2tag and bol == '<h2'):
print((' ' * 0) + getTocLine(line))
elif (useH3tag and bol == '<h3'):
print((' ' * 4) + getTocLine(line))
elif (useH4tag and bol == '<h4'):
print((' ' * 8) + getTocLine(line))
elif (useH5tag and bol == '<h5'):
print((' ' * 12) + getTocLine(line))
def getTocLine(line):
"""
見出しタグより遷移リンクを生成.
"""
import xml.etree.ElementTree as ET
headerTag = ET.fromstring(line.strip())
return '- <a href="#' + headerTag.attrib['id'] + '">' + headerTag.text + '</a>'
if __name__ == "__main__":
import sys
print("<!-- auto generated Table of Contents:START. -->")
print("<h2>目次</h2>")
processFile(sys.argv[1])
print("<!-- auto generated Table of Contents:END-->")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment