forked from danyork/makefaq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakefaq.py
executable file
·1248 lines (1075 loc) · 39.7 KB
/
makefaq.py
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# -------------------------------------------------------------
#
# makefaq.py
# Revision: 2.6
# Rev Date: ?? <month> 2009
#
# This program is designed to take a text file and generate
# a single-page formatted Frequently-Asked-Question file.
#
# It reads in FAQ categories, questions and answers from (by default)
# a file called "faq.dat", adds an HTML header and footer, and
# writes the information to "faq.html". There is also the
# ability to write out a text version.
#
# See the "Notes" section below for more details.
#
# If you have any comments about this script, of if you make
# an improved version, please contact Dan York at
#
# Copyright (c) 1999-2009 Dan York, [email protected]
# http://www.Lodestar2.com/software/makefaq/ or
# http://www.makefaq.org/
#
# The author acknowledges significant contributions to the
# code by Dave Seidel ([email protected]) and he can
# definitely be considered as the co-author of this code.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or any later version.
#
# This program is distributed in the hope that it will be useful
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# http://www.gnu.org/copyleft/gpl.html
#
# -------------------------------------------------------------
#
# If you use this program to generate a FAQ page for a public
# web site, please do send the URL to [email protected] so
# that your FAQ can be listed on the home page for makefaq as
# another example.
#
# -------------------------------------------------------------
#
# LANGUAGE LOCALIZATION ISSUES
#
# If you do not have the LANG environment variable set, you will need to
# set your locale here or use the '-L' command-line option.
# CURRENTLY SUPPORTED values are:
#
# 'de_DE' (German)
# 'en_US' (US English)
# 'fr_FR' (French)
# 'pt_BR' (Brazilian Portugeuse)
#
# This affects the dates, 'FAQ Revised' and "Table of Contents" strings.
#
# If you do not set a LANG or use '-L', you will receive the 'en_US'
# settings.
LOCALE = "en_US"
# If you would like to add support for your language, first determine the
# appropriate POSIX locale code and then add the appropriate messages to
# the "LocalizeStrings" function below. If your LOCALE is not a valid code,
# makefaq will print a error message and use the 'en_US' settings instead.
# If for some reason you want to ignore the LANG environment variable and
# force it to use a specific locale, use the '-L' command-line option.
# (Note that this *is* case-sensitive.)
#
# NOTE TO USERS ON NON-LINUX PLATFORMS: It appears that current versions of
# the locale module supplied with Python on at least Windows, HP-UX and
# FreeBSD 4.x do not allow the operating system locale to be set. Because
# of this, the exact format of the date/time stamp may not be localized to
# the conventions of your locale. Other than that, however, the program
# will work fine and, if you are using one of the locales mentioned above,
# the appropriate text strings *will* be printed out.
#
# -------------------------------------------------------------
# NOTES
#
# This program is made available purely so that others might
# be saved the frustration of building FAQ pages by hand.
# Examples of its use can be found at:
#
# http://www.makefaq.org/faq.html
#
# The input file is called "faq.dat" by default (but can be changed
# by command-line options). It uses an XML-ish language in the data
# file to delimit the three items: category of the question, question
# text and answer text. An example would be:
#
# <c>General
# <q>What is the meaning of life?
# <a>I have <i>no</i> idea!
#
# The Question and Answer sections can include HTML, as shown above.
# The category, question and answer can all be on one line, or can be
# on separate lines. The <c>, <q>, and <a> delimiters can be on the
# same line as their text, as shown above, or can be on a line of
# their own. Unlike true XML, there are no end tags. See the sample
# 'faq.dat' provided with the program for examples of data file layout.
#
# In the directory where the script is run, there must be three
# files (in the default configuration):
#
# - faq.dat - the text file with the questions and answers
# - faqheader.html - an HTML file with the top of the file
# - faqfooter.html - an HTML file with the bottom of the file
#
# Sample files should have been provided with this code.
#
# Please read the accompanying "ChangeLog" file to understand
# the substantial changes that have been made to the code base.
#
# Also please look at the README file before using this script.
#
# -------------------------------------------------------------
# BUGS
#
# If special entities like < and > are used in faq.dat to
# give a < and > sign, they are NOT stripped in the text or screen
# output.
#
# -------------------------------------------------------------
# REVISION HISTORY
#
# ?? ??? 2009 - 2.6 ...
#
# 29 Feb 2004 - 2.5 released with PEP-263 definition of source code
# encoding so that python 2.3 will not generate warnings
# 11 Nov 2002 - 2.4 released with ability to export to DocBook XML
# and the '-N' command-line option to suppress numbering
# 10 Aug 2002 - 2.3 released with '-j' command-line option and CSS
# support
# 13 Jul 2001 - 2.2 released with bug fix so it will run on non-Linux
# operating systems
# 22 Apr 2001 - 2.1 released with minor bug fix and Portuguese lang info
# 2 Apr 2001 - 2.0 released with multi-line datafile format
# 24 Mar 2001 - 0.5 released with '-d' command-line option
# 22 Feb 2000 - 0.4 uploaded with fix to data sorting problem
# 8 Feb 2000 - 0.3.1 uploaded with minor bug fix
# 22 Jan 2000 - 0.3 uploaded after substantantial modifications
# by Dave Seidel ([email protected])
# 14 Jan 2000 - (dave) added command line processing, error handling,
# comments
# 13 Jan 2000 - (dave) submitted to BEAST project
# 3 Jan 2000 - 0.2 uploaded in beta state to web site
#
# -------------------------------------------------------------
# QUICK SUMMARY OF CHANGES TO VERSION 2.6
#
# <text here>
#
# -------------------------------------------------------------
# QUICK SUMMARY OF CHANGES TO VERSION 2.5
#
# Purely a bug fix version so that users with python 2.3 will no
# longer see a warning about missing source code encoding.
#
# -------------------------------------------------------------
# QUICK SUMMARY OF CHANGES TO VERSION 2.4
#
# See the accompanying "ChangeLog" file for full details.
#
# New features:
# - There is now the ability to generate a DocBook XML <qandaset>
# file which could then be processed using XSLT tools into a
# PDF file or other format (including HTML). Usage is through
# the "DocBookXML" config that was added, as in:
#
# ./makefaq.py -c DocBookXML
#
# plus whatever other command-line options are desired. The
# default output file for this config is "faq-output.xml".
#
# Note that the contents of the question and answer tags are
# simply copied to the output XML file, so if they contain
# HTML tags, those will be copied over, and would then make
# the resulting XML file no longer valid.
#
# - A "-N" command-line option was added that will suppress
# numbering of questions and answers. This was desirable for
# the DocBook XML generation as the standard DocBook XSLT
# stylesheets can provide numbering for FAQ entries.
#
# - The attribute "self.createTOC" was added to the DefaultConfig
# and is set to TRUE so that a TOC will be generated by default.
# However, this can be overridden in subclassed configs in
# order to suppress the creation of a TOC by setting the value
# to FALSE. As an example, this was done in the DocBookXMLConfig.
#
# Updates:
# - German strings updated (Fabian Melzow)
# - Portugeuse strings updated (Mauro Persano)
#
# -------------------------------------------------------------
# ACKNOWLEDGEMENTS
#
# Beyond Dave Seidel, the following people need to be acknowledged
# for their assistance either with direct code or with ideas that led
# me to the right code. The version number in which their contributions
# was first incorporated is in parentheses after their name.
#
# - Guy Brand for the suggestion to use the locale module and
# the strftime function for localization, also for the
# French phrases (2.0)
# - Nicolas Devillard for his multi-line entry implementation (2.0)
# - Wolfgang Ocker for one multi-line entry implementation and for
# his implementation with gettext (both of which were very useful
# in trying to figure out how to implement both features in 2.0)
# - Wolfgang Ocker and Lenz Grimmer for the German phrases (2.0)
# - César A. K. Grossmann for his Portugeuse phrases (2.1)
# - Michael Wiedmann for catching the PrintTimeStamp bug (2.1) and
# also for packaging makefaq as a Debian package (from 2.0 on)
# - Michael Wiedmann for the man page (2.1)
# - Jerry (??) and Ken Ito for identifying the PrintTimeStamp setlocale
# issue on non-Linux platforms and Gareth Noyce for verifying the
# problem (2.2)
# - Fabian Melzow for the German JumpString translation (2.4)
# - Bernhard Reiter for the suggestion to move comments about functions
# into actual python docstrings (2.4)
# - Mauro Persano for the Portugeuse JumpString translation (2.4)
# - Kenzaburo Ito for his URL for the Mantis FAQ (2.4)
#
# See also the CREDITS file included with the source code.
#
# -------------------------------------------------------------
import getopt
import locale
import os
from sys import argv, stdout, stderr, exit
from time import asctime, localtime, time, strftime
from string import strip, split, atoi
from re import sub
TRUE = 1
FALSE = 0
def TellTruth(thing):
"""Simple function to return either true or false."""
if thing:
return 'TRUE'
return 'FALSE'
def LocalizeStrings(cfg,lc):
""" These are the strings that are printed for the "FAQ Revised" and
"Table of Contents" strings in the output file.
IF YOU WOULD LIKE TO ADD ANOTHER LANGUAGE, copy the three lines for
one of the entries beginning with 'elif' and modify them to have your
locale code and appropriate messages. PLEASE SEND ANY ADDITIONS TO ME
at '[email protected]' so that I can incorporate them into future
versions of the program. """
if lc == 'fr_FR':
cfg.RevString = 'Dernière révision:'
cfg.TOCString = 'Table des matières'
cfg.JumpString = 'Return to top of page'
elif lc == 'de_DE':
cfg.RevString = 'FAQ überarbeitet am:'
cfg.TOCString = 'Inhalt'
cfg.JumpString ='Zurück zum Seitenanfang'
elif lc == 'pt_BR':
cfg.RevString = 'FAQ Revisado em:'
cfg.TOCString = 'Tabela de Conteúdo'
cfg.JumpString = 'Voltar ao topo da página'
# Note that the a with an acute accent should be á in HTML
# ADD NEW LOCALES HERE. THEY MUST BE BEFORE THE 'else:' statement below.
# Note that indentation *is* significant in python and your 'elif' statement
# needs to line up with the other ones. You can remove the comment
# characters from the lines below and use it if you wish.
#elif lc == 'de_DE':
# cfg.RevString = 'FAQ Revised:'
# cfg.TOCString = 'Inhalt'
# cfg.JumpString ='Zurück zum Seitenanfang'
# If no locale matches, default to US English
else:
cfg.RevString = 'FAQ Revised:'
cfg.TOCString = 'Table of Contents'
cfg.JumpString = 'Return to top of page'
# -------------------------------------------------------------
class DefaultConfig:
"""
Base configuration class, with all filename and formatting
defaults; note that all the data defined in this class
is inherited by its subclasses, unless you override it
"""
def show(self):
print ' Configuration: ' + self.name + '\n' + \
' Input file: ' + self.infile + '\n' + \
' Header file: ' + self.headfile + '\n' + \
' Footer file: ' + self.footfile + '\n' + \
' Output file: ' + self.outfile + '\n' + \
'Old-style Delimiter: ' + self.delimiter + '\n' + \
' Cat Delimiter: ' + self.cdelim + '\n' + \
' Q Delimiter: ' + self.qdelim + '\n' + \
' A Delimiter: ' + self.adelim + '\n' + \
' addJumps: ' + TellTruth(self.addJumps) + '\n' + \
' createTOC: ' + TellTruth(self.createTOC) + '\n' + \
'numberQuestions: ' + TellTruth(self.numberQuestions) + '\n' + \
' hasLinks: ' + TellTruth(self.hasLinks)
def __repr__(self):
return self.name;
def __init__(self):
# name
self.name = "default"
# if this is FALSE, we strip and/or transform
# HTML tags; see the function FixSpecialText below
self.hasLinks = TRUE
# is a TOC created? In most cases, the answer will be true,
# but in some cases, such as DocBook output, we want to
# suppress creation of a TOC
self.createTOC = TRUE
# Are the categories and questions numbered? Default is yes.
self.numberQuestions = TRUE
# default filenames
self.headfile = 'faqheader.html'
self.footfile = 'faqfooter.html'
self.infile = 'faq.dat'
# If you want the default behaviour of makefaq 0.2,
# which was to dump the output to stdout, change the outfile
# line to:
#
# self.outfile = 'STDOUT'
#
self.outfile = 'faq.html'
#
# Set a flag in the default configuration that it uses the most
# current XML format. This allows for this
# to be overwritten in another config.
#
# The numbers are input on the command line in conjunction with
# the "-r" flag. For instance, you can use "-r 1" to use the
# original flat file or "-r 2" to use my pseudo-XML of makefaq 2.x.
# You could use "-r 3", but that XML format is not yet implemented.
#
self.fileformat = 2
#
# set the default delimiter in a single-line file
#
self.delimiter = '|'
#
# set the default delimiters used in multi-line files
#
self.cdelim = "<c>"
self.qdelim = "<q>"
self.adelim = "<a>"
#
# internationalization and localization
# Tries to use the LANG env variable first. Failing that, it
# falls back on the LOCALE defined at the top of the file.
#
try:
self.locale=os.environ['LANG']
except:
self.locale = LOCALE
LocalizeStrings(self, self.locale)
#
# timestamp
#
self.TS = {
'Pre' : '<p><i>',
'Post' : '</i></p>',
'Pre+' : '',
'Post+' : ''
}
# headings
self.Head = {
'Pre' : '<hr><h2>',
'Post' : '</h2>',
'Pre+' : '',
'Post+' : ''
}
# sections
self.Sec = {
'Pre' : '<dl>',
'Post' : '</dl>',
'Pre+' : '',
'Post+' : ''
}
# table of contents
self.TOC = {
'Pre' : '<dl>',
'Post' : '</dl>',
'Pre+' : '',
'Post+' : '',
'CatPre' : '<dt><b>',
'CatPost' : '</b></dt>',
'ListPre' : '<dd><ul>',
'ListPost' : '</ul></dd>',
'EntryPre' : '<li><a href="#',
'EntryIn' : '">',
'EntryPost' : '</a></li>'
}
# questions
self.Q = {
'Pre' : '<dt><b><a name="',
'In' : '\">',
'Post' : '</a></b></dt>'
}
# answers
self.A = {
'Pre' : '<dd>',
'Post' : '<br><br></dd>'
}
# This flag is for adding a link to return to the top of
# the page after every answer
self.addJumps = FALSE
# -------------------------------------------------------------
class TextConfig(DefaultConfig):
""" Text output configuration """
def __init__(self):
DefaultConfig.__init__(self)
# ID
self.name = 'text'
# flags
self.hasLinks = FALSE
# filenames
self.headfile = 'faqheader.txt'
self.footfile = 'faqfooter.txt'
self.infile = 'faq.dat'
self.outfile = 'faq.txt'
# default delimiter
self.delimiter = '|'
# timestamp
self.TS['Pre'] = '\n'
self.TS['Post'] = '\n\n'
self.TS['Pre+'] = ''
self.TS['Post+'] = ''
# heading
self.Head['Pre'] = ''
self.Head['Post'] = '\n'
self.Head['Pre+'] = ''
self.Head['Post+'] = ''
# sections
self.Sec['Pre'] = ''
self.Sec['Post'] = ''
self.Sec['Pre+'] = ''
self.Sec['Post+'] = ''
# TOC
self.TOC['Pre'] = ''
self.TOC['Post'] = '\n'
self.TOC['Pre+'] = ''
self.TOC['Post+'] = ''
self.TOC['CatPre'] = ''
self.TOC['CatPost'] = ''
self.TOC['ListPre'] = ''
self.TOC['ListPost'] = '\n'
self.TOC['EntryPre'] = ''
self.TOC['EntryIn'] = ''
self.TOC['EntryPost'] = ''
# questions
self.Q['Pre'] = ''
self.Q['In'] = ''
self.Q['Post'] = '\n'
# answers
self.A['Pre'] = ''
self.A['Post'] = '\n'
# -------------------------------------------------------------
class ScreenConfig(DefaultConfig):
""" screen (text) output configuration """
def __init__(self):
DefaultConfig.__init__(self)
# ID
self.name = 'screen'
# flags
self.hasLinks = FALSE
# filenames
self.headfile = 'faqheader.txt'
self.footfile = 'faqfooter.txt'
self.infile = 'faq.dat'
self.outfile = 'STDOUT'
# default delimiter
self.delimiter = '|'
# timestamp
self.TS['Pre'] = '\n'
self.TS['Post'] = '\n\n'
self.TS['Pre+'] = ''
self.TS['Post+'] = ''
# heading
self.Head['Pre'] = ''
self.Head['Post'] = '\n'
self.Head['Pre+'] = ''
self.Head['Post+'] = ''
# sections
self.Sec['Pre'] = ''
self.Sec['Post'] = ''
self.Sec['Pre+'] = ''
self.Sec['Post+'] = ''
# TOC
self.TOC['Pre'] = ''
self.TOC['Post'] = '\n'
self.TOC['Pre+'] = ''
self.TOC['Post+'] = ''
self.TOC['CatPre'] = ''
self.TOC['CatPost'] = ''
self.TOC['ListPre'] = ''
self.TOC['ListPost'] = '\n'
self.TOC['EntryPre'] = ''
self.TOC['EntryIn'] = ''
self.TOC['EntryPost'] = ''
# questions
self.Q['Pre'] = ''
self.Q['In'] = ''
self.Q['Post'] = '\n'
# answers
self.A['Pre'] = ''
self.A['Post'] = '\n'
# -------------------------------------------------------------
class DocBookXMLConfig(DefaultConfig):
"""Configuration to create DocBook XML
Target is to have entries like:
<qandaentry>
<question id="4">
<para>Will the product still be called xxxxxx</para>
</question>
<answer>
<para>That has not been determined yet.</para>
</answer>
</qandaentry>
"""
def __init__(self):
DefaultConfig.__init__(self)
self.name = "DocBookXML"
# Don't create a TOC, use numbering or create links
self.createTOC = FALSE
self.numberQuestions = FALSE
self.hasLinks = FALSE
self.headfile = 'faqheader.xml'
self.footfile = 'faqfooter.xml'
self.infile = 'faq.dat'
self.outfile = 'faq-output.xml'
# timestamp
#
# NEED TO WORK THIS ONE OUT
#
self.TS = {
'Pre' : '<para><emphasis>',
'Post' : '</emphasis></para>',
'Pre+' : '',
'Post+' : ''
}
# headings
#
# This may seem strange to put the <qandadiv> in the Pre+
# part of the headings, but this is necessary because HTML
# headings are outside of a structure, while XML headings
# are inside of an structure.
#
self.Head = {
'Pre' : '<title>',
'Post' : '</title>',
'Pre+' : '<qandadiv>\n',
'Post+' : ''
}
# sections
self.Sec = {
'Pre' : '',
'Post' : '</qandadiv>\n',
'Pre+' : '',
'Post+' : ''
}
# questions
self.Q = {
'Pre' : '<qandaentry>\n<question>\n<para>',
'In' : '',
'Post' : '</para>\n</question>'
}
#
# possible definition to get id into <question>
# currently not used.
#self.Q = {
# 'Pre' : '<qandaentry>\n<question id="',
# 'In' : '">\n<para>',
# 'Post' : '</para>\n</question>'
# }
# answers
self.A = {
'Pre' : '<answer>\n<para>',
'Post' : '</para>\n</answer>\n</qandaentry>\n'
}
# -------------------------------------------------------------
class BEASTConfig(DefaultConfig):
"""BEAST configuration
Sample configuration provided by Dave Seidel
([email protected])"""
def __init__(self):
DefaultConfig.__init__(self)
# ID
self.name = 'BEAST'
# filenames
self.headfile = 'html.1.faq'
self.footfile = 'html.2.faq'
self.infile = 'faq.dat'
self.outfile = 'faq.html'
# timestamp
self.TS['Pre+'] = '<tr><td>'
self.TS['Post+'] = '</td></tr>'
# headings
self.Head['Pre'] = ''
self.Head['Post'] = ''
self.Head['Pre+'] = '<tr><td bgcolor="#005D5D"><font face="Lucida, Verdana, Arial, sans-serif" color="#D0E4D0" size="+2">'
self.Head['Post+'] = '</font></td></tr>'
# sections
self.Sec['Pre+'] = '<tr><td><font face="Lucida, Verdana, Arial, sans-serif">'
self.Sec['Post+'] = '</font></td></tr>'
# table of contents
self.TOC['Pre+'] = '<tr><td><font face="Lucida, Verdana, Arial, sans-serif">'
self.TOC['Post+'] = '</font></td></tr>'
# -------------------------------------------------------------
#
# table of available configurations; if you add a new
# configuration, please add its class name to this list
#
# -------------------------------------------------------------
configTab = [
DefaultConfig,
TextConfig,
ScreenConfig,
DocBookXMLConfig,
BEASTConfig
]
# -------------------------------------------------------------
def PrintConfigs():
"""Tells each member of configTab to print itself"""
print 'Available configurations:'
for i in configTab:
cfg = i()
cfg.show()
# -------------------------------------------------------------
def FindConfig(name):
"""Given a config name, attempts to find a matching entry
in configTab; if found returns an *instance* of the
matching class"""
for i in configTab:
cfg = i()
if name == str(cfg):
return cfg
# -------------------------------------------------------------
class FaqEntry:
""" Class defining an individual FAQ entry """
def __init__(self,content):
self.question = content[0]
self.answer = content[1]
def __repr__(self):
return "\n" + self.question + "\n" + self.answer
# -------------------------------------------------------------
def IncludeFile(out, inputfile):
"""Reads in a file and writes it out, with an error message."""
try:
input = open(inputfile, 'r')
except:
stderr.write('Error opening file ' + inputfile + ' for inclusion.\n')
exit(1)
text = input.read()
out.write(text)
# -------------------------------------------------------------
def FixSpecialText(text):
"""This is where we strip and/or transform certain HTML tags
for plain-text output formats"""
# <br> becomes \n
fixed = sub('<br>', '\n', text)
# <a></a> gets stripped (use '?' qualifier for a non-greedy match)
fixed = sub('<a href=".+?">', '', fixed)
fixed = sub('</a>', '', fixed)
#<i>foo</i> becomes *foo*
fixed = sub('<i>', '*', fixed)
fixed = sub('</i>', '*', fixed)
fixed = sub('<em>', '*', fixed)
fixed = sub('</em>', '*', fixed)
# </li> becomes "; " (so at least we get a semicolon-separated list)
fixed = sub('</li>', '; ', fixed)
# Need to figure out how to strip out < and > and replace them
# with < and >
#fixed = sub('<', '<', fixed)
#fixed = sub('>', '>', fixed)
# strip everything else
fixed = sub('<.+?>', '', fixed)
return fixed
# -------------------------------------------------------------
def ReadOrigSource(cfg):
"""The function below is the original ReadSource that reads in faq.dat
files with all the contents on a single long line. It has been
retained here to provide backward compatibility so that older data
files can still be used. At some future point, it will be merged
back in with the new ReadSource to have a single function."""
try:
input = open(cfg.infile, 'r')
except:
stderr.write('Error opening file ' + cfg.infile + ' as input.\n')
exit(1)
faq1 = {}
catlist = []
i = 1
for line in input.readlines():
x = split(line, cfg.delimiter)
if len(x) < 3:
print 'Error: ' + cfg.infile + ', line ' + str(i) + ': bad format'
return
i = i + 1
category = strip(x[0])
x[1] = strip(x[1])
x[2] = strip(x[2])
# clean up answer question/text
if not cfg.hasLinks:
x[1] = FixSpecialText(x[1])
x[2] = FixSpecialText(x[2])
# if the category exists, append the entry
# otherwise add a new category, *then* append the entry.
if faq1.has_key(category):
faq1[category].append(FaqEntry(x[1:]))
else:
catlist.append(category)
faq1[category] = []
faq1[category].append(FaqEntry(x[1:]))
input.close()
return faq1, catlist
# -------------------------------------------------------------
def ReadPseudoXMLSource(cfg):
"""The ReadPseudoSource below handles multiline input in a pseudo-XML
format."""
try:
input = open(cfg.infile, 'r')
except:
stderr.write('Error opening file ' + cfg.infile + ' as input.\n')
exit(1)
faq1 = {}
catlist = []
i = 1
# read in entire file
all = input.read()
# split into blocks on category delimiter (default is <c>)
blocks = split(all,cfg.cdelim)
for line in blocks[1:]:
try:
a = split(line, cfg.qdelim)
b = split(a[1], cfg.adelim)
x = [a[0], b[0], b[1]]
except:
print 'Error: ' + cfg.infile + ', question ' + str(i) + ': bad format'
return
if len(x) < 3:
print 'Error: ' + cfg.infile + ', question ' + str(i) + ': bad format'
return
i = i + 1
category = strip(x[0])
x[1] = strip(x[1])
x[2] = strip(x[2])
# (v2.1)The following line was suggested by Nicolas Devillard and
# basically makes any "paragraph" in the data file into an HTML
# paragraph, i.e. you have lines of text, a blank line, and then
# more text. It's really just a hack to avoid typing extra <p>'s
# in the .dat file. He asked for it and I am including it here
# COMMENTED OUT for those who wish to enable this setting. I do not
# enable it by default because it may generate extra <p>'s in other
# HTML code where people do not want them to be.
#
#x[2] = sub('\n\n','\n<p>\n',x[2])
# clean up answer question/text
if not cfg.hasLinks:
x[1] = FixSpecialText(x[1])
x[2] = FixSpecialText(x[2])
# if the category exists, append the entry
# otherwise add a new category, *then* append the entry.
if faq1.has_key(category):
faq1[category].append(FaqEntry(x[1:]))
else:
catlist.append(category)
faq1[category] = []
faq1[category].append(FaqEntry(x[1:]))
input.close()
return faq1, catlist
# -------------------------------------------------------------
def PrintTimeStamp(cfg, out):
""" This function prints out the timestamp for when the FAQ was
revised. It first attempts to set the locale so that the timestamp
will be generated using the date/time format of your location.
NOTE that on non-Linux platforms, there seems to be a problem setting
the locale, so it may just use whatever system-generated date/time is
supplied by the operating system."""
try:
# First try to set to locale to whatever the user (or system) has
# configured
locale.setlocale(locale.LC_ALL,cfg.locale)
except locale.Error:
# If that fails, default to 'en_US'
try:
locale.setlocale(locale.LC_ALL,"en_US")
stderr.write("Locale %s not supported. Using 'en_US' instead." % cfg.locale)
except:
#If there is a problem with setting the locale, do nothing and just
# use whatever date/time the system provides
pass
out.write("%s%s%s %s%s%s\n" % (cfg.TS['Pre+'], cfg.TS['Pre'],cfg.RevString,
strftime("%A %d %B %Y %H:%M:%S",localtime(time())),
cfg.TS['Post'], cfg.TS['Post+']))
# -------------------------------------------------------------
def PrintTOC(cfg, out, faq1, catlist):
"""Prints out the "table of contents" - the initial list of questions
with their links to the questions further below on the page."""
out.write("%s%s%s%s%s\n" % (cfg.Head['Pre+'], cfg.Head['Pre'], cfg.TOCString,\
cfg.Head['Post'], cfg.Head['Post+']))
out.write("%s%s\n" % (cfg.Sec['Pre+'], cfg.Sec['Pre']))
i = 1
for x in catlist:
if cfg.numberQuestions:
out.write("%s%s. %s%s\n" % (cfg.TOC['CatPre'],
str(i), x,
cfg.TOC['CatPost']))
else:
out.write("%s%s%s\n" % (cfg.TOC['CatPre'],
x,
cfg.TOC['CatPost']))
out.write("%s\n" % cfg.TOC['ListPre'])
if cfg.hasLinks and cfg.numberQuestions:
for y in range(len(faq1[x])):
out.write("%s%s%s%s%s.%s. %s%s\n" % (cfg.TOC['EntryPre'],
x, str(y),
cfg.TOC['EntryIn'],
str(i), str(y + 1),
faq1[x][y].question,
cfg.TOC['EntryPost']))
elif cfg.hasLinks:
for y in range(len(faq1[x])):
out.write("%s%s%s%s%s%s\n" % (cfg.TOC['EntryPre'],
x, str(y),
cfg.TOC['EntryIn'],
faq1[x][y].question,
cfg.TOC['EntryPost']))
elif cfg.numberQuestions:
for y in range(len(faq1[x])):
out.write("%s%s%s.%s. %s%s\n" % (cfg.TOC['EntryPre'],
cfg.TOC['EntryIn'],
str(i), str(y + 1),
faq1[x][y].question,
cfg.TOC['EntryPost']))
else:
for y in range(len(faq1[x])):
out.write("%s%s%s%s\n" % (cfg.TOC['EntryPre'],
cfg.TOC['EntryIn'],
faq1[x][y].question,
cfg.TOC['EntryPost']))
out.write("%s\n" % cfg.TOC['ListPost'])
i = i + 1
out.write("%s%s\n" % (cfg.Sec['Post'], cfg.Sec['Post+']))
# -------------------------------------------------------------
def PrintQA(cfg, out, faq1, feedback, catlist):
"""Prints out the Questions and corresponding Answers"""
i = 1
for x in catlist:
if cfg.numberQuestions:
out.write("%s%s%s. %s%s%s\n" % (cfg.Head['Pre+'], cfg.Head['Pre'],
str(i), x,
cfg.Head['Post'], cfg.Head['Post+']))
else:
out.write("%s%s%s%s%s\n" % (cfg.Head['Pre+'], cfg.Head['Pre'],
x,
cfg.Head['Post'], cfg.Head['Post+']))