1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
BSD 2-Clause License
Copyright (c) 2020-2021, The FreeBSD Project
Copyright (c) 2020-2021, Sergio Carlavilla <carlavilla@FreeBSD.org>
This script will generate the Table of Contents for any figures/images
in the books.
"""
import sys, getopt
import re
import os.path
languages = []
"""
To determine if a chapter is a chapter we are going to check if it is
anything else, an appendix, a part, the preface ... and if it is not
any of those, it will be a chapter.
It may not be the best option, but it works :)
"""
def checkIsChapter(chapter, chapterContent):
if "part" in chapter:
return False
elif "[preface]" in chapterContent:
return False
elif "[appendix]" in chapterContent:
return False
else:
return True
def setTOCTitle(language):
languages = {
'en': 'List of Figures',
'de': 'Abbildungsverzeichnis',
'el': 'Κατάλογος Σχημάτων',
'es': 'Lista de figuras',
'fr': 'Liste des illustrations',
'hu': 'Az ábrák listája',
'it': 'Lista delle figure',
'ja': '図の一覧',
'mn': 'Зургийн жагсаалт',
'nl': 'Lijst van afbeeldingen',
'pl': 'Spis rysunków',
'pt-br': 'Lista de Figuras',
'ru': 'Список иллюстраций',
'zh-cn': '插图清单',
'zh-tw': '附圖目錄'
}
return languages.get(language)
def main(argv):
justPrintOutput = False
langargs = []
try:
opts, args = getopt.gnu_getopt(argv,"hl:o",["language="])
except getopt.GetoptError:
print('books-toc-figures-creator.py [-o] -l <language>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('books-toc-figures-creator.py [-o] -l <language>')
sys.exit()
if opt == '-o':
justPrintOutput = True
elif opt in ("-l", "--language"):
langargs = arg.replace(" ",",").split(',')
# treat additional arguments as languages, but check if they
# contain ','
for l in args:
langargs.extend(l.replace(" ",",").split(','))
for language in langargs:
if not os.path.exists('./content/{}/books/books.adoc'.format(language)):
if not justPrintOutput:
print('Warning: no books found for language "{0}"'.format(language))
continue
with open('./content/{}/books/books.adoc'.format(language), 'r', encoding = 'utf-8') as booksFile:
books = [line.strip() for line in booksFile]
for book in books:
with open('./content/{0}/books/{1}/chapters-order.adoc'.format(language, book), 'r', encoding = 'utf-8') as chaptersFile:
chapters = [line.strip() for line in chaptersFile]
toc = "// Code @" + "generated by the FreeBSD Documentation toolchain. DO NOT EDIT.\n"
toc += "// Please don't change this file manually but run `make` to update it.\n"
toc += "// For more information, please read the FreeBSD Documentation Project Primer\n\n"
toc += "[.toc]\n"
toc += "--\n"
toc += '[.toc-title]\n'
toc += setTOCTitle(language) + '\n\n'
chapterCounter = 1
figureCounter = 1
for chapter in chapters:
with open('./content/{0}/books/{1}/{2}'.format(language, book, chapter), 'r', encoding = 'utf-8') as chapterFile:
chapterContent = chapterFile.read().splitlines()
chapterFile.close()
chapter = chapter.replace("/_index.adoc", "").replace(".adoc", "").replace("/chapter.adoc", "")
figureId = ""
figureTitle = ""
for lineNumber, chapterLine in enumerate(chapterContent, 1):
if re.match(r"^image::+", chapterLine) and re.match(r"^[.]{1}[^\n]+", chapterContent[lineNumber-2]) and re.match(r"^\[\[[^\n]+\]\]", chapterContent[lineNumber-3]):
figureTitle = chapterContent[lineNumber-2]
figureId = chapterContent[lineNumber-3]
if book == "handbook":
toc += "* {0}.{1} link:{2}#{3}[{4}]\n".format(chapterCounter, figureCounter, chapter, figureId.replace("[[", "").replace("]]", ""), figureTitle[1:])
else:
toc += "* {0}.{1} link:{2}#{3}[{4}]\n".format(chapterCounter, figureCounter, "", figureId.replace("[[", "").replace("]]", ""), figureTitle[1:])
figureCounter += 1
else:
figureId = ""
figureTitle = ""
if checkIsChapter(chapter, chapterContent):
chapterCounter += 1
figureCounter = 1 # Reset figure counter
toc += "--\n"
if justPrintOutput == False:
with open('./content/{0}/books/{1}/toc-figures.adoc'.format(language, book), 'w', encoding = 'utf-8') as tocFile:
tocFile.write(toc)
else:
print('./content/{0}/books/{1}/toc-figures.adoc'.format(language, book))
if __name__ == "__main__":
main(sys.argv[1:])
|