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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2012 Cyril Bonté
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''
TODO : ability to split chapters into several files
TODO : manage keyword locality (server/proxy/global ; ex : maxconn)
TODO : Remove global variables where possible
'''
import os
import subprocess
import sys
import cgi
import re
import time
import datetime
from optparse import OptionParser
from mako.template import Template
from mako.lookup import TemplateLookup
from mako.exceptions import TopLevelLookupException
from parser import PContext
from parser import remove_indent
from parser import *
from urllib import quote
VERSION = ""
HAPROXY_GIT_VERSION = False
def main():
global VERSION, HAPROXY_GIT_VERSION
usage="Usage: %prog --infile <infile> --outfile <outfile>"
optparser = OptionParser(description='Generate HTML Document from HAProxy configuation.txt',
version=VERSION,
usage=usage)
optparser.add_option('--infile', '-i', help='Input file mostly the configuration.txt')
optparser.add_option('--outfile','-o', help='Output file')
optparser.add_option('--base','-b', default = '', help='Base directory for relative links')
(option, args) = optparser.parse_args()
if not (option.infile and option.outfile) or len(args) > 0:
optparser.print_help()
exit(1)
option.infile = os.path.abspath(option.infile)
option.outfile = os.path.abspath(option.outfile)
os.chdir(os.path.dirname(__file__))
VERSION = get_git_version()
if not VERSION:
sys.exit(1)
HAPROXY_GIT_VERSION = get_haproxy_git_version(os.path.dirname(option.infile))
convert(option.infile, option.outfile, option.base)
# Temporarily determine the version from git to follow which commit generated
# the documentation
def get_git_version():
if not os.path.isdir(".git"):
print >> sys.stderr, "This does not appear to be a Git repository."
return
try:
p = subprocess.Popen(["git", "describe", "--tags", "--match", "v*"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except EnvironmentError:
print >> sys.stderr, "Unable to run git"
return
version = p.communicate()[0]
if p.returncode != 0:
print >> sys.stderr, "Unable to run git"
return
if len(version) < 2:
return
version = version[1:].strip()
version = re.sub(r'-g.*', '', version)
return version
def get_haproxy_git_version(path):
try:
p = subprocess.Popen(["git", "describe", "--tags", "--match", "v*"], cwd=path, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except EnvironmentError:
return False
version = p.communicate()[0]
if p.returncode != 0:
return False
if len(version) < 2:
return False
version = version[1:].strip()
version = re.sub(r'-g.*', '', version)
return version
def getTitleDetails(string):
array = string.split(".")
title = array.pop().strip()
chapter = ".".join(array)
level = max(1, len(array))
if array:
toplevel = array[0]
else:
toplevel = False
return {
"title" : title,
"chapter" : chapter,
"level" : level,
"toplevel": toplevel
}
# Parse the whole document to insert links on keywords
def createLinks():
global document, keywords, keywordsCount, keyword_conflicts, chapters
print >> sys.stderr, "Generating keywords links..."
delimiters = [
dict(start='"', end='"', multi=True ),
dict(start='- ' , end='\n' , multi=False),
]
for keyword in keywords:
keywordsCount[keyword] = 0
for delimiter in delimiters:
keywordsCount[keyword] += document.count(delimiter['start'] + keyword + delimiter['end'])
if (keyword in keyword_conflicts) and (not keywordsCount[keyword]):
# The keyword is never used, we can remove it from the conflicts list
del keyword_conflicts[keyword]
if keyword in keyword_conflicts:
chapter_list = ""
for chapter in keyword_conflicts[keyword]:
chapter_list += '<li><a href="#%s">%s</a></li>' % (quote("%s (%s)" % (keyword, chapters[chapter]['title'])), chapters[chapter]['title'])
for delimiter in delimiters:
if delimiter['multi']:
document = document.replace(delimiter['start'] + keyword + delimiter['end'],
delimiter['start'] + '<span class="dropdown">' +
'<a class="dropdown-toggle" data-toggle="dropdown" href="#">' +
keyword +
'<span class="caret"></span>' +
'</a>' +
'<ul class="dropdown-menu">' +
'<li class="dropdown-header">This keyword is available in sections :</li>' +
chapter_list +
'</ul>' +
'</span>' + delimiter['end'])
else:
document = document.replace(delimiter['start'] + keyword + delimiter['end'], delimiter['start'] + '<a href="#' + quote(keyword) + '">' + keyword + '</a>' + delimiter['end'])
else:
for delimiter in delimiters:
document = document.replace(delimiter['start'] + keyword + delimiter['end'], delimiter['start'] + '<a href="#' + quote(keyword) + '">' + keyword + '</a>' + delimiter['end'])
if keyword.startswith("option "):
shortKeyword = keyword[len("option "):]
keywordsCount[shortKeyword] = 0
for delimiter in delimiters:
keywordsCount[keyword] += document.count(delimiter['start'] + shortKeyword + delimiter['end'])
if (shortKeyword in keyword_conflicts) and (not keywordsCount[shortKeyword]):
# The keyword is never used, we can remove it from the conflicts list
del keyword_conflicts[shortKeyword]
for delimiter in delimiters:
document = document.replace(delimiter['start'] + shortKeyword + delimiter['start'], delimiter['start'] + '<a href="#' + quote(keyword) + '">' + shortKeyword + '</a>' + delimiter['end'])
def documentAppend(text, retline = True):
global document
document += text
if retline:
document += "\n"
def init_parsers(pctxt):
return [
underline.Parser(pctxt),
arguments.Parser(pctxt),
seealso.Parser(pctxt),
example.Parser(pctxt),
table.Parser(pctxt),
underline.Parser(pctxt),
keyword.Parser(pctxt),
]
# The parser itself
def convert(infile, outfile, base=''):
global document, keywords, keywordsCount, chapters, keyword_conflicts
if len(base) > 0 and base[:-1] != '/':
base += '/'
hasSummary = False
data = []
fd = file(infile,"r")
for line in fd:
line.replace("\t", " " * 8)
line = line.rstrip()
data.append(line)
fd.close()
pctxt = PContext(
TemplateLookup(
directories=[
'templates'
]
)
)
parsers = init_parsers(pctxt)
pctxt.context = {
'headers': {},
'document': "",
'base': base,
}
sections = []
currentSection = {
"details": getTitleDetails(""),
"content": "",
}
chapters = {}
keywords = {}
keywordsCount = {}
specialSections = {
"default": {
"hasKeywords": True,
},
"4.1": {
"hasKeywords": True,
},
}
pctxt.keywords = keywords
pctxt.keywordsCount = keywordsCount
pctxt.chapters = chapters
print >> sys.stderr, "Importing %s..." % infile
nblines = len(data)
i = j = 0
while i < nblines:
line = data[i].rstrip()
if i < nblines - 1:
next = data[i + 1].rstrip()
else:
next = ""
if (line == "Summary" or re.match("^[0-9].*", line)) and (len(next) > 0) and (next[0] == '-') \
and ("-" * len(line)).startswith(next): # Fuzzy underline length detection
sections.append(currentSection)
currentSection = {
"details": getTitleDetails(line),
"content": "",
}
j = 0
i += 1 # Skip underline
while not data[i + 1].rstrip():
i += 1 # Skip empty lines
else:
if len(line) > 80:
print >> sys.stderr, "Line `%i' exceeds 80 columns" % (i + 1)
currentSection["content"] = currentSection["content"] + line + "\n"
j += 1
if currentSection["details"]["title"] == "Summary" and line != "":
hasSummary = True
# Learn chapters from the summary
details = getTitleDetails(line)
if details["chapter"]:
chapters[details["chapter"]] = details
i += 1
sections.append(currentSection)
chapterIndexes = sorted(chapters.keys())
document = ""
# Complete the summary
for section in sections:
details = section["details"]
title = details["title"]
if title:
fulltitle = title
if details["chapter"]:
#documentAppend("<a name=\"%s\"></a>" % details["chapter"])
fulltitle = details["chapter"] + ". " + title
if not details["chapter"] in chapters:
print >> sys.stderr, "Adding '%s' to the summary" % details["title"]
chapters[details["chapter"]] = details
chapterIndexes = sorted(chapters.keys())
for section in sections:
details = section["details"]
pctxt.details = details
level = details["level"]
title = details["title"]
content = section["content"].rstrip()
print >> sys.stderr, "Parsing chapter %s..." % title
if (title == "Summary") or (title and not hasSummary):
summaryTemplate = pctxt.templates.get_template('summary.html')
documentAppend(summaryTemplate.render(
pctxt = pctxt,
chapters = chapters,
chapterIndexes = chapterIndexes,
))
if title and not hasSummary:
hasSummary = True
else:
continue
if title:
documentAppend('<a class="anchor" id="%s" name="%s"></a>' % (details["chapter"], details["chapter"]))
if level == 1:
documentAppend("<div class=\"page-header\">", False)
documentAppend('<h%d id="chapter-%s" data-target="%s"><small><a class="small" href="#%s">%s.</a></small> %s</h%d>' % (level, details["chapter"], details["chapter"], details["chapter"], details["chapter"], cgi.escape(title, True), level))
if level == 1:
documentAppend("</div>", False)
if content:
if False and title:
# Display a navigation bar
documentAppend('<ul class="well pager">')
documentAppend('<li><a href="#top">Top</a></li>', False)
index = chapterIndexes.index(details["chapter"])
if index > 0:
documentAppend('<li class="previous"><a href="#%s">Previous</a></li>' % chapterIndexes[index - 1], False)
if index < len(chapterIndexes) - 1:
documentAppend('<li class="next"><a href="#%s">Next</a></li>' % chapterIndexes[index + 1], False)
documentAppend('</ul>', False)
content = cgi.escape(content, True)
content = re.sub(r'section ([0-9]+(.[0-9]+)*)', r'<a href="#\1">section \1</a>', content)
pctxt.set_content(content)
if not title:
lines = pctxt.get_lines()
pctxt.context['headers'] = {
'title': '',
'subtitle': '',
'version': '',
'author': '',
'date': ''
}
if re.match("^-+$", pctxt.get_line().strip()):
# Try to analyze the header of the file, assuming it follows
# those rules :
# - it begins with a "separator line" (several '-' chars)
# - then the document title
# - an optional subtitle
# - a new separator line
# - the version
# - the author
# - the date
pctxt.next()
pctxt.context['headers']['title'] = pctxt.get_line().strip()
pctxt.next()
subtitle = ""
while not re.match("^-+$", pctxt.get_line().strip()):
subtitle += " " + pctxt.get_line().strip()
pctxt.next()
pctxt.context['headers']['subtitle'] += subtitle.strip()
if not pctxt.context['headers']['subtitle']:
# No subtitle, try to guess one from the title if it
# starts with the word "HAProxy"
if pctxt.context['headers']['title'].startswith('HAProxy '):
pctxt.context['headers']['subtitle'] = pctxt.context['headers']['title'][8:]
pctxt.context['headers']['title'] = 'HAProxy'
pctxt.next()
pctxt.context['headers']['version'] = pctxt.get_line().strip()
pctxt.next()
pctxt.context['headers']['author'] = pctxt.get_line().strip()
pctxt.next()
pctxt.context['headers']['date'] = pctxt.get_line().strip()
pctxt.next()
if HAPROXY_GIT_VERSION:
pctxt.context['headers']['version'] = 'version ' + HAPROXY_GIT_VERSION
# Skip header lines
pctxt.eat_lines()
pctxt.eat_empty_lines()
documentAppend('<div>', False)
delay = []
while pctxt.has_more_lines():
try:
specialSection = specialSections[details["chapter"]]
except:
specialSection = specialSections["default"]
line = pctxt.get_line()
if i < nblines - 1:
nextline = pctxt.get_line(1)
else:
nextline = ""
oldline = line
pctxt.stop = False
for parser in parsers:
line = parser.parse(line)
if pctxt.stop:
break
if oldline == line:
# nothing has changed,
# delays the rendering
if delay or line != "":
delay.append(line)
pctxt.next()
elif pctxt.stop:
while delay and delay[-1].strip() == "":
del delay[-1]
if delay:
remove_indent(delay)
documentAppend('<pre class="text">%s\n</pre>' % "\n".join(delay), False)
delay = []
documentAppend(line, False)
else:
while delay and delay[-1].strip() == "":
del delay[-1]
if delay:
remove_indent(delay)
documentAppend('<pre class="text">%s\n</pre>' % "\n".join(delay), False)
delay = []
documentAppend(line, True)
pctxt.next()
while delay and delay[-1].strip() == "":
del delay[-1]
if delay:
remove_indent(delay)
documentAppend('<pre class="text">%s\n</pre>' % "\n".join(delay), False)
delay = []
documentAppend('</div>')
if not hasSummary:
summaryTemplate = pctxt.templates.get_template('summary.html')
print chapters
document = summaryTemplate.render(
pctxt = pctxt,
chapters = chapters,
chapterIndexes = chapterIndexes,
) + document
# Log warnings for keywords defined in several chapters
keyword_conflicts = {}
for keyword in keywords:
keyword_chapters = list(keywords[keyword])
keyword_chapters.sort()
if len(keyword_chapters) > 1:
print >> sys.stderr, 'Multi section keyword : "%s" in chapters %s' % (keyword, list(keyword_chapters))
keyword_conflicts[keyword] = keyword_chapters
keywords = list(keywords)
keywords.sort()
createLinks()
# Add the keywords conflicts to the keywords list to make them available in the search form
# And remove the original keyword which is now useless
for keyword in keyword_conflicts:
sections = keyword_conflicts[keyword]
offset = keywords.index(keyword)
for section in sections:
keywords.insert(offset, "%s (%s)" % (keyword, chapters[section]['title']))
offset += 1
keywords.remove(keyword)
print >> sys.stderr, "Exporting to %s..." % outfile
template = pctxt.templates.get_template('template.html')
try:
footerTemplate = pctxt.templates.get_template('footer.html')
footer = footerTemplate.render(
pctxt = pctxt,
headers = pctxt.context['headers'],
document = document,
chapters = chapters,
chapterIndexes = chapterIndexes,
keywords = keywords,
keywordsCount = keywordsCount,
keyword_conflicts = keyword_conflicts,
version = VERSION,
date = datetime.datetime.now().strftime("%Y/%m/%d"),
)
except TopLevelLookupException:
footer = ""
fd = open(outfile,'w')
print >> fd, template.render(
pctxt = pctxt,
headers = pctxt.context['headers'],
base = base,
document = document,
chapters = chapters,
chapterIndexes = chapterIndexes,
keywords = keywords,
keywordsCount = keywordsCount,
keyword_conflicts = keyword_conflicts,
version = VERSION,
date = datetime.datetime.now().strftime("%Y/%m/%d"),
footer = footer
)
fd.close()
if __name__ == '__main__':
main()
|