#!/usr/bin/env python3 helptext=''' Created on 06.01.2014 this parser generates php-readable data and static html out of faqtext.txt. The format is strict, but very easy: The FAQ is separated into SECTIONs, each of which may contain an arbitrary number of items, which consist (in this order) of a QUESTION, an alpha-numeric, unique LABEL of your choice and an ANSWER: Example: SECTION: General Questions about SCIP QUESTION: What is SCIP? LABEL:whatisscip ANSWER:
SCIP is a constraint integer program solver. It can be used as a framework for branch-cut-and-price and contains all necessary plugins to serve as a standalone solver for MIP, MINLP, and PBO.
... The example has the right format to go: - We have four keywords for sections, questions, labels and answers, resp. - an item first mentions a QUESTION:, which then gets a LABEL:, and finally, an ANSWER:. Do not mix up this order. - Each keyword ends with a colon ':' (!) - The label is a one-liner - line break after the 'ANSWER:'-tag - Questions and answers should be simple HTML text elements, do not provide specific classes etc. therein. To make faq-generation for both home use and web use possible, extra-keywords should be used to identify relative locations to the documentation and file extensions: - use PATHTODOC to denote relative paths to the documentation. - use LINKEXT to denote file-extensions. The parser automatically interprets this. The written faqdata.php can be transformed with php into static HTML by use of localfaq.php. The written faq.inc produces the same output (without the need for php). @author Gregor Hendel @author Matthias Miltenberger ''' import re import sys import argparse parser = argparse.ArgumentParser(epilog=helptext) parser.add_argument('--faqtextfile', type=str, default='faqtext.txt', help='faq text file') parser.add_argument('--linkext', type=str, default='shtml', help='file extension for internal links') sectiontag = "SECTION:" questiontag = "QUESTION:" answertag = "ANSWER:" labeltag = "LABEL:" faqinc_header=''' ''' def formatitem(item): ''' returns a fully formatted php array containing all item information ''' return """ array( 'question'=>'%s', 'answer'=>'%s', 'label'=>'%s' )""" % item def formatsection(section, items): ''' returns a fully formatted array to represent an entire section together with its items ''' return """ array( 'title'=>%s, 'content'=>array(%s), )""" % (repr(section), ",\n".join(map(formatitem, items.__iter__()))) def formatallsections(sections, sectionitems): ''' formats a list of sections ''' return """ $faq = array( %s );""" % (",\n".join([formatsection(section, sectionitems[section]) for section in sections])) def write_faqinc(f, sections, sectiontimes): ''' write main part of faq.inc ''' for section in sections : f.write('