-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxt2tags-1.7.py
executable file
·2628 lines (2297 loc) · 84.6 KB
/
txt2tags-1.7.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
# txt2tags - generic text conversion tool
# http://txt2tags.sf.net
#
# Copyright 2001, 2002, 2003 Aurelio Marinho Jargas
#
# 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, version 2.
#
# 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.
#
# You have received a copy of the GNU General Public License along
# with this program, on the COPYING file.
#
# the code is better, even readable now, but needs more improvements
# please wait for the upcoming 2.0 series for a cleaner one
#XXX Python coding warning
# Avoid common mistakes:
# - do NOT use newlist=list instead newlist=list[:]
# - do NOT use newdic=dic instead newdic=dic.copy()
# - do NOT use dic[key] instead dic.get(key)
import re, string, os, sys, getopt, traceback
from time import strftime,time,localtime
my_url = 'http://txt2tags.sf.net'
my_email = '[email protected]'
my_version = '1.7' #-betaN
DEBUG = 0 # do not edit here, please use --debug
targets = ['txt', 'sgml', 'html', 'pm6', 'mgp', 'moin', 'man', 'tex']
FLAGS = {'noheaders':0,'enumtitle':0 ,'maskemail':0 ,'stdout' :0,
'toconly' :0,'toc' :0 ,'gui' :0 ,'dump-source':0}
OPTIONS = {'toclevel' :3,'style' :'','type' :'','outfile' :'',
'split':0, 'lang':''}
CONFIG_KEYWORDS = ['encoding', 'style', 'cmdline','preproc','postproc']
CONF = {}
regex = {}
TAGS = {}
rules = {}
currdate = strftime('%Y%m%d',localtime(time())) # ISO current date
lang = 'english'
doctype = outfile = ''
STDIN = STDOUT = '-'
ESCCHAR = '\x00'
LINEBREAK = {'default':'\n', 'win':'\r\n', 'mac':'\r'}
#my_version = my_version + '-dev' + currdate[4:] # devel!
# global vars for doClose*()
quotedepth = []
listindent = []
listids = []
subarea = None
tableborder = 0
# set the Line Break across platforms
LB = LINEBREAK.get(sys.platform[:3]) or LINEBREAK['default']
versionstr = "txt2tags version %s <%s>"%(my_version,my_url)
usage = """
%s
Usage: txt2tags -t <type> [OPTIONS] file.t2t
-t, --type set target document type. currently supported:
%s
-o, --outfile=FILE set FILE as the output file name ('-' for STDOUT)
--stdout same as '-o -' or '--outfile -' (deprecated option)
-H, --noheaders suppress header, title and footer information
-n, --enumtitle enumerate all title lines as 1, 1.1, 1.1.1, etc
--maskemail hide email from spam robots. [email protected] turns <x (a) y z>
--toc add TOC (Table of Contents) to target document
--toconly print document TOC and exit
--toclevel=N set maximum TOC level (depth) to N
--gui invoke Graphical Tk Interface
--style=FILE use FILE as the document style (like Html CSS)
-h, --help print this help information and exit
-V, --version print program version and exit
Extra options for HTML target (needs sgml-tools):
--split split documents. values: 0, 1, 2 (default 0)
--lang document language (default english)
By default, converted output is saved to 'file.<type>'.
Use --outfile to force an output file name.
If input file is '-', reads from STDIN.
If output file is '-', dumps output to STDOUT.\
"""%(versionstr, re.sub(r"[]'[]",'',repr(targets)))
# here is all the target's templates
# you may edit them to fit your needs
# - the %(HEADERn)s strings represent the Header lines
# - use %% to represent a literal %
#
HEADER_TEMPLATE = {
'txt': """\
%(HEADER1)s
%(HEADER2)s
%(HEADER3)s
""",
'sgml': """\
<!doctype linuxdoc system>
<article>
<title>%(HEADER1)s
<author>%(HEADER2)s
<date>%(HEADER3)s
""",
'html': """\
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META NAME="generator" CONTENT="http://txt2tags.sf.net">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=%(ENCODING)s">
<LINK REL="stylesheet" TYPE="text/css" HREF="%(STYLE)s">
<TITLE>%(HEADER1)s</TITLE>
</HEAD><BODY BGCOLOR="white" TEXT="black">
<P ALIGN="center"><CENTER><H1>%(HEADER1)s</H1>
<FONT SIZE=4>
<I>%(HEADER2)s</I><BR>
%(HEADER3)s
</FONT></CENTER>
""",
# TODO man section 1 is hardcoded...
'man': """\
.TH "%(HEADER1)s" 1 "%(HEADER3)s" "%(HEADER2)s"
""",
# TODO style to <HR>
'pm6': """\
<PMTags1.0 win><C-COLORTABLE ("Preto" 1 0 0 0)
><@Normal=
<FONT "Times New Roman"><CCOLOR "Preto"><SIZE 11>
<HORIZONTAL 100><LETTERSPACE 0><CTRACK 127><CSSIZE 70><C+SIZE 58.3>
<C-POSITION 33.3><C+POSITION 33.3><P><CBASELINE 0><CNOBREAK 0><CLEADING -0.05>
<GGRID 0><GLEFT 7.2><GRIGHT 0><GFIRST 0><G+BEFORE 7.2><G+AFTER 0>
<GALIGNMENT "justify"><GMETHOD "proportional"><G& "ENGLISH">
<GPAIRS 12><G%% 120><GKNEXT 0><GKWIDOW 0><GKORPHAN 0><GTABS $>
<GHYPHENATION 2 34 0><GWORDSPACE 75 100 150><GSPACE -5 0 25>
><@Bullet=<@-PARENT "Normal"><FONT "Abadi MT Condensed Light">
<GLEFT 14.4><G+BEFORE 2.15><G%% 110><GTABS(25.2 l "")>
><@PreFormat=<@-PARENT "Normal"><FONT "Lucida Console"><SIZE 8><CTRACK 0>
<GLEFT 0><G+BEFORE 0><GALIGNMENT "left"><GWORDSPACE 100 100 100><GSPACE 0 0 0>
><@Title1=<@-PARENT "Normal"><FONT "Arial"><SIZE 14><B>
<GCONTENTS><GLEFT 0><G+BEFORE 0><GALIGNMENT "left">
><@Title2=<@-PARENT "Title1"><SIZE 12><G+BEFORE 3.6>
><@Title3=<@-PARENT "Title1"><SIZE 10><GLEFT 7.2><G+BEFORE 7.2>
><@Title4=<@-PARENT "Title3">
><@Title5=<@-PARENT "Title3">
><@Quote=<@-PARENT "Normal"><SIZE 10><I>>
%(HEADER1)s
%(HEADER2)s
%(HEADER3)s
""",
'mgp': """\
#!/usr/X11R6/bin/mgp -t 90
%%deffont "normal" xfont "utopia-medium-r", charset "iso8859-1"
%%deffont "normal-i" xfont "utopia-medium-i", charset "iso8859-1"
%%deffont "normal-b" xfont "utopia-bold-r" , charset "iso8859-1"
%%deffont "normal-bi" xfont "utopia-bold-i" , charset "iso8859-1"
%%deffont "mono" xfont "courier-medium-r", charset "iso8859-1"
%%default 1 size 5
%%default 2 size 8, fore "yellow", font "normal-b", center
%%default 3 size 5, fore "white", font "normal", left, prefix " "
%%tab 1 size 4, vgap 30, prefix " ", icon arc "red" 40, leftfill
%%tab 2 prefix " ", icon arc "orange" 40, leftfill
%%tab 3 prefix " ", icon arc "brown" 40, leftfill
%%tab 4 prefix " ", icon arc "darkmagenta" 40, leftfill
%%tab 5 prefix " ", icon arc "magenta" 40, leftfill
%%%%------------------------- end of headers -----------------------------
%%page
%%size 10, center, fore "yellow"
%(HEADER1)s
%%font "normal-i", size 6, fore "white", center
%(HEADER2)s
%%font "mono", size 7, center
%(HEADER3)s
""",
# TODO please, improve me!
'moin': """\
%(HEADER1)s
%(HEADER2)s
%(HEADER3)s
""",
'tex': \
r"""\documentclass[11pt,a4paper]{article}
\usepackage{amsfonts,amssymb,graphicx,url}
\usepackage[%(ENCODING)s]{inputenc} %% char encoding
\pagestyle{plain} %% do page numbering ('empty' turns off)
\frenchspacing %% no aditional spaces after periods
\setlength{\parskip}{8pt}\parindent=0pt %% no paragraph indentation
%% uncomment next line for fancy PDF output on Adobe Acrobat Reader
%%\usepackage[pdfstartview=FitV,colorlinks=true,bookmarks=true]{hyperref}
\title{%(HEADER1)s}
\author{%(HEADER2)s}
\begin{document}
\date{%(HEADER3)s}
\maketitle
"""
}
#-----------------------------------------------------------------------
def Quit(msg, exitcode=0): print msg ; sys.exit(exitcode)
def Error(msg): print "ERROR: %s"%msg ; sys.exit()
def echo(msg): print '\033[32;1m%s\033[m'%msg # quick debug
def Debug(msg,i=0,linenr=None):
if i > DEBUG: return
if linenr is not None:
print "(%d) %04d:%s"%(i,linenr,msg)
else:
print "(%d) %s"%(i,msg)
def Readfile(file, remove_linebreaks=0):
if file == '-':
try: data = sys.stdin.readlines()
except: Error('You must feed me with data on STDIN!')
else:
try: f = open(file); data = f.readlines() ; f.close()
except: Error("Cannot read file:\n %s"%file)
if remove_linebreaks:
data = map(lambda x:re.sub('[\n\r]+$','',x), data)
return data
def Savefile(file, contents):
try: f = open(file, 'wb')
except: Error("Cannot open file for writing:\n %s"%file)
if type(contents) == type([]): doit = f.writelines
else: doit = f.write
doit(contents) ; f.close()
def get_include_contents(file, path=''):
"Parses %!include: value and extract file contents"
# set include type
id = 'T2T'
if file[0] == file[-1] == '`':
id = 'VERB'
file = file[1:-1] # remove ``
elif file[0] == file[-1] == "'":
id = 'PASS'
file = file[1:-1] # remove ''
# handle remote dir execution
filepath = os.path.join(path, file)
# pass-thru
if id == 'PASS':
return id, Readfile(filepath, remove_linebreaks=1)
# VERB text
if id == 'VERB':
lines = Readfile(filepath, remove_linebreaks=1)
# escape inner '---' that would end VERB block
lines = map(lambda x: re.sub('^---$','--- ',x), lines)
# add VERB block identifiers
lines = ['---'] + lines + ['---']
# default txt2tags marked text
else:
id = 'T2T'
lines = get_file_body(filepath)
# add delimiter comments
lines.insert(0, '%%INCLUDED_%s starts here: %s'%(id,file))
lines.append('%%INCLUDED_%s ends here: %s'%(id,file))
return id, lines
def ParseConfig(text='',name='', target=''):
ret = {}
if not text: return ret
re_name = name or '[a-z]+'
re_target = target or '[a-z]*'
cfgregex = re.compile("""
^%%!\s* # leading id with opt spaces
(?P<name>%s)\s* # config name
(\((?P<target>%s)\))? # optional target spec inside ()
\s*:\s* # key:value delimiter with opt spaces
(?P<value>\S.+?) # config value
\s*$ # rstrip() spaces and hit EOL
"""%(re_name,re_target), re.I+re.VERBOSE)
prepostregex = re.compile("""
# ---[ PATTERN ]---
^( "([^"]*)" # "double quoted" or
| '([^']*)' # 'single quoted' or
| ([^\s]+) # single_word
)
\s+ # separated by spaces
# ---[ REPLACE ]---
( "([^"]*)" # "double quoted" or
| '([^']*)' # 'single quoted' or
| (.*) # anything
)
\s*$
""", re.VERBOSE)
match = cfgregex.match(text)
if match:
ret = {'name' :string.lower(match.group('name') or ''),
'target':string.lower(match.group('target') or 'all'),
'value' :match.group('value') }
# Special config with two quoted values (%!preproc: "foo" 'bar')
if ret['name'] in ['preproc','postproc']:
valmatch = prepostregex.search(ret['value'])
if not valmatch: return None
getval = valmatch.group
patt = getval(2) or getval(3) or getval(4) or ''
repl = getval(6) or getval(7) or getval(8) or ''
ret['value'] = (patt, repl)
return ret
class Cmdline:
def __init__(self, cmdline=[], nocheck=0):
self.conf = {}
self.cmdline = cmdline
self.cmdline_conf = {}
self.dft_options = OPTIONS.copy()
self.dft_flags = FLAGS.copy()
self.all_options = self.dft_options.keys()
self.all_flags = self.dft_flags.keys()
self.defaults = self._get_empty_conf()
self.nocheck = nocheck
if cmdline: self.parse()
#TODO protect quotes contents
def _tokenize(self, cmd_string):
return string.split(cmd_string)
def parse(self):
"return a dic with all options:value found"
if not self.cmdline: return {}
Debug("cmdline: %s"%self.cmdline, 1)
options = {'infile': '', 'infiles':''}
# compose valid options list
longopts = ['help','version'] + self.all_flags + \
map(lambda x:x+'=', self.all_options) # add =
cmdline = self.cmdline[1:] # del prog name
# get cmdline options
try: (opt, args) = getopt.getopt(cmdline, 'hVnHt:o:', longopts)
except getopt.error, errmsg:
Error("%s (try --help)"%errmsg)
# get infile, if any
if args:
options['infile'] = args[0]
options['infiles'] = args # multi
# parse all options
for name,val in opt:
if name in ['-h','--help' ]: Quit(usage)
elif name in ['-V','--version']: Quit(versionstr)
elif name in ['-t','--type' ]: options['type'] = val
elif name in ['-o','--outfile' ]: options['outfile'] = val
elif name in ['-n','--enumtitle']: options['enumtitle'] = 1
elif name in ['-H','--noheaders']: options['noheaders'] = 1
elif name in ['--stdout']: options['outfile'] = STDOUT
else: options[name[2:]] = val or 1 # del --
# save results
Debug("cmdline arguments: %s"%options, 1)
self.cmdline_conf = options
def compose(self, conf={}):
"compose full command line from CONF dict"
if not conf: return ''
args = []
cfg = conf.copy()
valid_opts = self.all_options + self.all_flags
use_short = {'noheaders':'H', 'enumtitle':'n'}
# remove useless options
if cfg.get('toconly'):
del cfg['noheaders']
del cfg['outfile'] # defaults to STDOUT
if cfg.get('type') == 'txt':
del cfg['type'] # already default
args.append('--toconly') # must be the first
del cfg['toconly']
# add target type
if cfg.has_key('type'):
args.append('-t '+cfg['type'])
del cfg['type']
# add other options
for key in cfg.keys():
if key not in valid_opts: continue # must be a %!setting
if key == 'outfile': continue # later
val = cfg[key]
if not val: continue
# default values are useless on cmdline
if val == self.dft_options.get(key): continue
# -short format
if key in use_short.keys():
args.append('-'+use_short[key])
continue
# --long format
if key in self.all_flags: # add --option
args.append('--'+key)
else: # add --option=value
args.append('--%s=%s'%(key,val))
# the outfile using -o
if cfg.has_key('outfile') and \
cfg['outfile'] != self.dft_options.get('outfile'):
args.append('-o '+cfg['outfile'])
# the input file is always at the end
if cfg.has_key('infile'):
args.append(cfg['infile'])
# return as a single string
ret = string.join(args,' ')
Debug("Diet command line: %s"%ret, 1)
return ret
def merge(self, extraopts=''):
"insert cmdline portion BEFORE current cmdline"
if not extraopts: return
if type(extraopts) == type(''):
extraopts = self._tokenize(extraopts)
if not self.cmdline: self.cmdline = extraopts
else: self.cmdline = ['t2t-merged'] +extraopts +self.cmdline[1:]
self.parse()
def _get_outfile_name(self, conf):
"dirname is the same for {in,out}file"
infile = conf['infile']
if not infile: return ''
if infile == STDIN or conf['outfile'] == STDOUT:
outfile = STDOUT
else:
basename = re.sub('\.(txt|t2t)$','',infile)
outfile = "%s.%s"%(basename, conf['type'])
self.dft_options['outfile'] = outfile # save for self.compose()
Debug(" infile: '%s'"%infile , 1)
Debug("outfile: '%s'"%outfile, 1)
return outfile
def _sanity(self, dic):
"basic cmdline syntax checkings"
if not dic: return {}
if not dic['infile'] or not dic['type']:
Quit(usage, 1) # no filename/doctype
if not targets.count(dic['type']): # check target
Error("Invalid document type '%s' (try --help)"%(
dic['type']))
#DISABLED: conflicting with %!cmdline: -o foo
#if len(dic['infiles']) > 1 and dic['outfile']: # -o FILE *.t2t
# Error("--outfile can't be used with multiple files")
for opt in self.all_options: # check numeric options
opttype = type(self.dft_options[opt])
if dic.get(opt) and opttype == type(9):
try: dic[opt] = int(dic.get(opt)) # save
except: Error('--%s value must be a number'%opt)
if dic['split'] not in [0,1,2]: # check split level
Error('Option --split must be 0, 1 or 2')
return dic
def merge_conf(self, newconfs={}, override=0):
"include Config Area settings into self.conf"
if not self.conf: self.get_conf()
if not newconfs: return self.conf
for key in newconfs.keys():
if key == 'cmdline': continue # already done
# filters are always accumulative
if key in ['preproc','postproc']:
if not self.conf.has_key(key):
self.conf[key] = []
self.conf[key].extend(newconfs[key])
continue
# add anyway
if override:
self.conf[key] = newconfs[key]
continue
# just update if still 'virgin'
if self.conf.has_key(key) and \
self.conf[key] == self.defaults.get(key):
self.conf[key] = newconfs[key]
# add new
if not self.conf.has_key(key):
self.conf[key] = newconfs[key]
Debug("Merged CONF (override=%s): %s"%(override,self.conf), 1)
return self.conf
def _get_empty_conf(self):
econf = self.dft_options.copy()
for k in self.dft_flags.keys(): econf[k] = self.dft_flags[k]
return econf
def get_conf(self):
"set vars and flags according to options dic"
if not self.cmdline_conf:
if not self.cmdline: return {}
self.parse()
dic = self.cmdline_conf
conf = self.defaults.copy()
## store flags & options
for flag in self.all_flags:
if dic.has_key(flag): conf[flag] = 1
for opt in self.all_options + ['infile', 'infiles']:
if dic.has_key(opt): conf[opt] = dic.get(opt)
if not conf['type'] and conf['toconly']: conf['type'] = 'txt'
if not conf['type'] and conf['dump-source']: conf['type'] = 'txt'
if not self.nocheck: conf = self._sanity(conf)
## some gotchas for specific issues
doctype = conf['type']
infile = conf['infile']
# toconly is stronger than others
if conf['toconly']:
conf['noheaders'] = 1
conf['toc'] = 0
conf['split'] = 0
conf['gui'] = 0
conf['outfile'] = STDOUT
conf['toclevel'] = conf['toclevel'] or \
self.dft_options['toclevel']
# dump-source is stronger than others (including toconly)
if conf['dump-source']:
conf['toconly'] = 0
conf['noheaders'] = 0
conf['toc'] = 0
conf['split'] = 0
conf['gui'] = 0
conf['outfile'] = STDOUT
# split: just HTML, no stdout, 1st do a sgml, then sgml2html
if conf['split']:
if doctype != 'html':
conf['split'] = 0
else:
conf['type'] = 'sgml'
if conf['outfile'] == STDOUT:
conf['outfile'] = ''
outfile = conf['outfile'] or self._get_outfile_name(conf)
# final checkings
if conf['split'] and outfile == STDOUT:
Error('--split: You must provide a FILE (not STDIN)')
if infile == outfile and outfile != STDOUT:
Error("SUICIDE WARNING!!! (see --outfile)\n source"+\
" and target files has the same name: "+outfile)
### author's note: "yes, i've got my sample.t2t file deleted
### before add this test... :/"
conf['outfile'] = outfile
conf['cmdline'] = self.cmdline
Debug("CONF data: %s\n"%conf, 1)
self.conf = conf
return self.conf
#
### End of Cmdline class
class Proprierties:
def __init__(self, filename=''):
self.buffer = [''] # text start at pos 1
self.areas = ['head','conf','body']
self.arearef = []
self.headers = ['','','']
self.config = self.get_empty_config()
self.lastline = 0
self.filename = filename
self.conflines = []
self.bodylines = []
if filename:
self.read_file(filename)
self.find_areas()
self.set_headers()
self.set_config()
def read_file(self, file):
lines = Readfile(file)
if not lines: Error('Empty file! %s'%file)
self.buffer.extend(lines)
def get_empty_config(self):
empty = {}
for targ in targets+['all']: empty[targ] = {}
return empty
def find_areas(self):
"Run through buffer and identify head/conf/body areas"
buf = self.buffer ; ref = [1,4,0] # defaults
if not string.strip(buf[1]): # no header
ref[0] = 0 ; ref[1] = 2
for i in range(ref[1],len(buf)): # find body init
if string.strip(buf[i]) and buf[i][0] != '%':
ref[2] = i ; break # !blank, !comment
if ParseConfig(buf[i], 'include'):
ref[2] = i ; break # %!include command
if ref[1] == ref[2]: ref[1] = 0 # no conf area
for i in 0,1,2: # del !existent
if not ref[i]: self.areas[i] = ''
self.arearef = ref # save results
self.lastline = len(self.buffer)-1
Debug('Head,Conf,Body start line: %s'%ref, 1)
# store CONF and BODY lines found
cfgend = ref[2] or len(buf)
self.conflines = buf[ref[1]:cfgend]
if ref[2]: self.bodylines = buf[ref[2]:]
def set_headers(self):
"Extract and save headers contents"
if not self.arearef: self.find_areas()
if not self.areas.count('head'): return
if self.lastline < 3:
#TODO on gui this checking is !working
Error(
"Premature end of Headers on '%s'."%self.filename +\
'\n\nFile has %s line(s), but '%self.lastline +\
'Headers should be composed by 3 lines. ' +\
'\nMaybe you should left the first line blank? ' +\
'(for no headers)')
for i in 0,1,2:
self.headers[i] = string.strip(self.buffer[i+1])
Debug("Headers found: %s"%self.headers, 1, i+1)
def set_config(self):
"Extract and save config contents (including includes)"
if not self.arearef: self.find_areas()
if not self.areas.count('conf'): return
keywords = string.join(CONFIG_KEYWORDS, '|')
linenr = self.arearef[1]-1 # for debug messages
for line in self.conflines:
linenr = linenr + 1
if len(line) < 3: continue
if line[:2] != '%!': continue
cfg = ParseConfig(line, keywords)
# any _valid_ config found?
if not cfg:
Debug('Bogus Config Line',1,linenr)
continue
# get data
targ, key, val = cfg['target'],cfg['name'], cfg['value']
# check config target specification
if targ not in targets+['all']:
Debug("Config Error: Invalid target '%s', ignoring"%targ,
1,linenr)
continue
# filters are multiple config
if key in ['preproc','postproc']:
if not self.config['all'].has_key(key): # 1st one
self.config['all'][key] = []
# all filters are saved to target 'all'
# finish_him will decide what to consider
self.config['all'][key].append((targ,)+val)
else:
self.config[targ][key] = val
Debug("Found config for target '%s': '%s', value '%s'"%(
targ,key,val),1,linenr)
Debug("All %%!CONFIG: %s"%self.config, 1)
def get_file_body(file):
"Returns all the document BODY lines (including includes)"
prop = Proprierties()
prop.read_file(file)
prop.find_areas()
return prop.bodylines
def finish_him(outlist, CONF):
"Writing output to screen or file"
outfile = CONF['outfile']
outlist = unmaskEscapeChar(outlist)
# do PostProc
if CONF['postproc']:
postoutlist = []
for line in outlist:
for targ,patt,repl in CONF['postproc']:
if targ not in [CONF['type'], 'all']: continue
try : line = re.sub(patt, repl, line)
except: Error("Invalid PostProc filter regex: '%s'"%patt)
postoutlist.append(line)
outlist = postoutlist[:]
if outfile == STDOUT:
if CONF['gui']:
return outlist
else:
for line in outlist: print line
else:
Savefile(outfile, addLineBreaks(outlist))
if not CONF['gui']: print 'wrote %s'%(outfile)
if CONF['split']:
print "--- html..."
sgml2html = 'sgml2html -s %s -l %s %s'%(
CONF['split'],CONF['lang'] or lang,outfile)
print "Running system command:", sgml2html
os.system(sgml2html)
def toc_maker(toc, conf):
"Compose TOC list 'by hand'"
# TOC is a tag, so there's nothing to do here
if TAGS['TOC']: return []
# toc is a valid t2t marked text (list type), that is converted
if conf['toc'] or conf['toconly']:
fakeconf = conf.copy()
fakeconf['noheaders'] = 1
fakeconf['toconly'] = 0
fakeconf['maskemail'] = 0
fakeconf['dump-source'] = 0
fakeconf['preproc'] = []
fakeconf['postproc'] = []
toc,foo = convert(toc, fakeconf)
# TOC between bars (not for --toconly)
if conf['toc']:
para = TAGS['paragraph']
tocbar = [para, regex['x'].sub('-'*72,TAGS['bar1']), para]
toc = tocbar + toc + tocbar
return toc
def getTags(doctype):
keys = [
'paragraph','title1','title2','title3','title4','title5',
'numtitle1','numtitle2','numtitle3','numtitle4','numtitle5',
'areaPreOpen','areaPreClose',
'areaQuoteOpen','areaQuoteClose',
'fontMonoOpen','fontMonoClose',
'fontBoldOpen','fontBoldClose',
'fontItalicOpen','fontItalicClose',
'fontBolditalicOpen','fontBolditalicClose',
'fontUnderlineOpen','fontUnderlineClose',
'listOpen','listClose','listItem',
'numlistOpen','numlistClose','numlistItem',
'deflistOpen','deflistClose','deflistItem1','deflistItem2',
'bar1','bar2',
'url','urlMark','email','emailMark',
'img','imgsolo',
'tableOpen','tableClose','tableLineOpen','tableLineClose',
'tableCellOpen','tableCellClose',
'tableTitleCellOpen','tableTitleCellClose',
'anchor','comment','TOC',
'EOD'
]
alltags = {
'txt': {
'title1' : ' \a' ,
'title2' : '\t\a' ,
'title3' : '\t\t\a' ,
'title4' : '\t\t\t\a' ,
'title5' : '\t\t\t\t\a',
'areaQuoteOpen' : ' ' ,
'listItem' : '- ' ,
'numlistItem' : '\a. ' ,
'bar1' : '\a' ,
'bar2' : '\a' ,
'url' : '\a' ,
'urlMark' : '\a (\a)' ,
'email' : '\a' ,
'emailMark' : '\a (\a)' ,
'img' : '[\a]' ,
},
'html': {
'paragraph' : '<P>' ,
'title1' : '<H1>\a</H1>' ,
'title2' : '<H2>\a</H2>' ,
'title3' : '<H3>\a</H3>' ,
'title4' : '<H4>\a</H4>' ,
'title5' : '<H5>\a</H5>' ,
'areaPreOpen' : '<PRE>' ,
'areaPreClose' : '</PRE>' ,
'areaQuoteOpen' : '<BLOCKQUOTE>' ,
'areaQuoteClose' : '</BLOCKQUOTE>' ,
'fontMonoOpen' : '<CODE>' ,
'fontMonoClose' : '</CODE>' ,
'fontBoldOpen' : '<B>' ,
'fontBoldClose' : '</B>' ,
'fontItalicOpen' : '<I>' ,
'fontItalicClose' : '</I>' ,
'fontBolditalicOpen' : '<B><I>' ,
'fontBolditalicClose' : '</I></B>' ,
'fontUnderlineOpen' : '<U>' ,
'fontUnderlineClose' : '</U>' ,
'listOpen' : '<UL>' ,
'listClose' : '</UL>' ,
'listItem' : '<LI>' ,
'numlistOpen' : '<OL>' ,
'numlistClose' : '</OL>' ,
'numlistItem' : '<LI>' ,
'deflistOpen' : '<DL>' ,
'deflistClose' : '</DL>' ,
'deflistItem1' : '<DT>\a</DT>' ,
'deflistItem2' : '<DD>' ,
'bar1' : '<HR NOSHADE SIZE=1>' ,
'bar2' : '<HR NOSHADE SIZE=5>' ,
'url' : '<A HREF="\a">\a</A>' ,
'urlMark' : '<A HREF="\a">\a</A>' ,
'email' : '<A HREF="mailto:\a">\a</A>' ,
'emailMark' : '<A HREF="mailto:\a">\a</A>' ,
'img' : '<IMG ALIGN="\a" SRC="\a" BORDER="0">',
'imgsolo' : '<P ALIGN="center">\a</P>' ,
'tableOpen' : '<table\a cellpadding=4 border=\a>',
'tableClose' : '</table>' ,
'tableLineOpen' : '<tr>' ,
'tableLineClose' : '</tr>' ,
'tableCellOpen' : '<td\a>' ,
'tableCellClose' : '</td>' ,
'tableTitleCellOpen' : '<th>' ,
'tableTitleCellClose' : '</th>' ,
'tableAlignLeft' : '' ,
'tableAlignCenter' : ' align="center"',
'tableCellAlignLeft' : '' ,
'tableCellAlignRight' : ' align="right"' ,
'tableCellAlignCenter': ' align="center"',
'anchor' : '<a name="\a"></a>',
'comment' : '<!-- \a -->' ,
'EOD' : '</BODY></HTML>'
},
'sgml': {
'paragraph' : '<p>' ,
'title1' : '<sect>\a<p>' ,
'title2' : '<sect1>\a<p>' ,
'title3' : '<sect2>\a<p>' ,
'title4' : '<sect3>\a<p>' ,
'title5' : '<sect4>\a<p>' ,
'areaPreOpen' : '<tscreen><verb>' ,
'areaPreClose' : '</verb></tscreen>' ,
'areaQuoteOpen' : '<quote>' ,
'areaQuoteClose' : '</quote>' ,
'fontMonoOpen' : '<tt>' ,
'fontMonoClose' : '</tt>' ,
'fontBoldOpen' : '<bf>' ,
'fontBoldClose' : '</bf>' ,
'fontItalicOpen' : '<em>' ,
'fontItalicClose' : '</em>' ,
'fontBolditalicOpen' : '<bf><em>' ,
'fontBolditalicClose' : '</em></bf>' ,
'fontUnderlineOpen' : '<bf><em>' ,
'fontUnderlineClose' : '</em></bf>' ,
'listOpen' : '<itemize>' ,
'listClose' : '</itemize>' ,
'listItem' : '<item>' ,
'numlistOpen' : '<enum>' ,
'numlistClose' : '</enum>' ,
'numlistItem' : '<item>' ,
'deflistOpen' : '<descrip>' ,
'deflistClose' : '</descrip>' ,
'deflistItem1' : '<tag>\a</tag>' ,
'bar1' : '<!-- \a -->' ,
'bar2' : '<!-- \a -->' ,
'url' : '<htmlurl url="\a" name="\a">' ,
'urlMark' : '<htmlurl url="\a" name="\a">' ,
'email' : '<htmlurl url="mailto:\a" name="\a">' ,
'emailMark' : '<htmlurl url="mailto:\a" name="\a">' ,
'img' : '<figure><ph vspace=""><img src="\a">'+\
'</figure>' ,
'tableOpen' : '<table><tabular ca="\a">' ,
'tableClose' : '</tabular></table>' ,
'tableLineClose' : '<rowsep>' ,
'tableCellClose' : '<colsep>' ,
'tableTitleCellClose' : '<colsep>' ,
'tableColAlignLeft' : 'l' ,
'tableColAlignRight' : 'r' ,
'tableColAlignCenter' : 'c' ,
'comment' : '<!-- \a -->' ,
'TOC' : '<toc>' ,
'EOD' : '</article>'
},
'tex': {
'title1' : '\n\\newpage\section*{\a}',
'title2' : '\\subsection*{\a}' ,
'title3' : '\\subsubsection*{\a}' ,
# title 4/5: DIRTY: para+BF+\\+\n
'title4' : '\\paragraph{}\\textbf{\a}\\\\\n',
'title5' : '\\paragraph{}\\textbf{\a}\\\\\n',
'numtitle1' : '\n\\newpage\section{\a}',
'numtitle2' : '\\subsection{\a}' ,
'numtitle3' : '\\subsubsection{\a}' ,
'areaPreOpen' : '\\begin{verbatim}' ,
'areaPreClose' : '\\end{verbatim}' ,
'areaQuoteOpen' : '\\begin{quotation}' ,
'areaQuoteClose' : '\\end{quotation}' ,
'fontMonoOpen' : '\\texttt{' ,
'fontMonoClose' : '}' ,
'fontBoldOpen' : '\\textbf{' ,
'fontBoldClose' : '}' ,
'fontItalicOpen' : '\\textit{' ,
'fontItalicClose' : '}' ,
'fontBolditalicOpen' : '\\textbf{\\textit{' ,
'fontBolditalicClose' : '}}' ,
'fontUnderlineOpen' : '\\underline{' ,
'fontUnderlineClose' : '}' ,
'listOpen' : '\\begin{itemize}' ,
'listClose' : '\\end{itemize}' ,
'listItem' : '\\item ' ,
'numlistOpen' : '\\begin{enumerate}' ,
'numlistClose' : '\\end{enumerate}' ,
'numlistItem' : '\\item ' ,
'deflistOpen' : '\\begin{description}',
'deflistClose' : '\\end{description}' ,
'deflistItem1' : '\\item[\a]' ,
'bar1' : '\n\\hrulefill{}\n' ,
'bar2' : '\n\\rule{\linewidth}{1mm}\n',
'url' : '\\url{\a}' ,
'urlMark' : '\\textit{\a} (\\url{\a})' ,
'email' : '\\url{\a}' ,
'emailMark' : '\\textit{\a} (\\url{\a})' ,
'img' : '\\begin{figure}\\includegraphics{\a}'+\
'\\end{figure}',
'tableOpen' : '\\begin{center}\\begin{tabular}{\a|}',
'tableClose' : '\\end{tabular}\\end{center}',
'tableLineOpen' : '\\hline ' ,
'tableLineClose' : ' \\\\' ,
'tableCellClose' : ' & ' ,
'tableTitleCellOpen' : '\\textbf{',
'tableTitleCellClose' : '} & ' ,
'tableColAlignLeft' : '|l' ,
'tableColAlignRight' : '|r' ,
'tableColAlignCenter' : '|c' ,
'comment' : '% \a' ,
'TOC' : '\\newpage\\tableofcontents',
'EOD' : '\\end{document}'
},
'moin': {
'title1' : '= \a =' ,
'title2' : '== \a ==' ,
'title3' : '=== \a ===' ,
'title4' : '==== \a ====' ,
'title5' : '===== \a =====',
'areaPreOpen' : '{{{' ,
'areaPreClose' : '}}}' ,
'areaQuoteOpen' : ' ' ,
'fontMonoOpen' : '{{{' ,
'fontMonoClose' : '}}}' ,
'fontBoldOpen' : "'''" ,
'fontBoldClose' : "'''" ,
'fontItalicOpen' : "''" ,
'fontItalicClose' : "''" ,
'fontBolditalicOpen' : "'''''" ,
'fontBolditalicClose' : "'''''" ,
'fontUnderlineOpen' : "'''''" ,
'fontUnderlineClose' : "'''''" ,
'listItem' : ' * ' ,
'numlistItem' : ' \a. ' ,
'bar1' : '----' ,
'bar2' : '----' ,
'url' : '[\a]' ,
'urlMark' : '[\a \a]' ,
'email' : '[\a]' ,
'emailMark' : '[\a \a]' ,
'img' : '[\a]' ,
'tableLineOpen' : '||' ,
'tableCellClose' : '||' ,
'tableTitleCellClose' : '||'
},
'mgp': {
'paragraph' : '%font "normal", size 5\n' ,
'title1' : '%page\n\n\a' ,
'title2' : '%page\n\n\a' ,
'title3' : '%page\n\n\a' ,
'title4' : '%page\n\n\a' ,
'title5' : '%page\n\n\a' ,
'areaPreOpen' : '\n%font "mono"' ,
'areaPreClose' : '%font "normal"' ,
'areaQuoteOpen' : '%prefix " "' ,
'areaQuoteClose' : '%prefix " "' ,
'fontMonoOpen' : '\n%cont, font "mono"\n' ,
'fontMonoClose' : '\n%cont, font "normal"\n' ,
'fontBoldOpen' : '\n%cont, font "normal-b"\n' ,
'fontBoldClose' : '\n%cont, font "normal"\n' ,
'fontItalicOpen' : '\n%cont, font "normal-i"\n' ,