PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/old/txt2tags-1.6.py

http://txt2tags.googlecode.com/
Python | 2542 lines | 2502 code | 10 blank | 30 comment | 9 complexity | 04ccb57d7ce5ada49fd8c0233f13047c MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, WTFPL
  1. #!/usr/bin/env python
  2. # txt2tags - generic text conversion tool
  3. # http://txt2tags.sf.net
  4. #
  5. # Copyright 2001, 2002, 2003 Aurelio Marinho Jargas
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, version 2.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You have received a copy of the GNU General Public License along
  17. # with this program, on the COPYING file.
  18. #
  19. # the code is better, even readable now, but needs more improvements
  20. # please wait for the upcoming 2.0 series for a cleaner one
  21. #XXX Python coding warning
  22. # Avoid common mistakes:
  23. # - do NOT use newlist=list instead newlist=list[:]
  24. # - do NOT use newdic=dic instead newdic=dic.copy()
  25. # - do NOT use dic[key] instead dic.get(key)
  26. import re, string, os, sys, getopt, traceback
  27. from time import strftime,time,localtime
  28. my_url = 'http://txt2tags.sf.net'
  29. my_email = 'verde@aurelio.net'
  30. my_version = '1.6' #-betaMMDD
  31. DEBUG = 0 # do not edit here, please use --debug
  32. targets = ['txt', 'sgml', 'html', 'pm6', 'mgp', 'moin', 'man', 'tex']
  33. FLAGS = {'noheaders':0,'enumtitle':0 ,'maskemail':0 ,'stdout' :0,
  34. 'toconly' :0,'toc' :0 ,'gui' :0 ,'included':0}
  35. OPTIONS = {'toclevel' :3,'style' :'','type' :'','outfile' :'',
  36. 'split':0, 'lang':''}
  37. CONFIG_KEYWORDS = ['encoding', 'style', 'cmdline','preproc','postproc']
  38. CONF = {}
  39. regex = {}
  40. TAGS = {}
  41. rules = {}
  42. currdate = strftime('%Y%m%d',localtime(time())) # ISO current date
  43. lang = 'english'
  44. doctype = outfile = ''
  45. STDIN = STDOUT = '-'
  46. ESCCHAR = '\x00'
  47. LINEBREAK = {'default':'\n', 'win':'\r\n', 'mac':'\r'}
  48. #my_version = my_version + '-dev' + currdate[4:] # devel!
  49. # global vars for doClose*()
  50. quotedepth = []
  51. listindent = []
  52. listids = []
  53. subarea = None
  54. tableborder = 0
  55. # set the Line Break across platforms
  56. LB = LINEBREAK.get(sys.platform[:3]) or LINEBREAK['default']
  57. versionstr = "txt2tags version %s <%s>"%(my_version,my_url)
  58. usage = """
  59. %s
  60. Usage: txt2tags -t <type> [OPTIONS] file.t2t
  61. -t, --type set target document type. actually supported:
  62. %s
  63. -o, --outfile=FILE set FILE as the output filename ('-' for STDOUT)
  64. --stdout same as '-o -' or '--outfile -' (deprecated option)
  65. -H, --noheaders suppress header, title and footer information
  66. -n, --enumtitle enumerate all title lines as 1, 1.1, 1.1.1, etc
  67. --maskemail hide email from spam robots. x@y.z turns <x (a) y z>
  68. --toc add TOC (Table of Contents) to target document
  69. --toconly print document TOC and exit
  70. --toclevel=N set maximum TOC level (deepness) to N
  71. --gui invoke Graphical Tk Interface
  72. --style=FILE use FILE as the document style (like Html CSS)
  73. -h, --help print this help information and exit
  74. -V, --version print program version and exit
  75. Extra options for HTML target (needs sgml-tools):
  76. --split split documents. values: 0, 1, 2 (default 0)
  77. --lang document language (default english)
  78. By default, converted output is saved to 'file.<type>'.
  79. Use --outfile to force an output filename.
  80. If input file is '-', reads from STDIN.
  81. If outfile is '-', dumps output to STDOUT.\
  82. """%(versionstr, re.sub(r"[]'[]",'',repr(targets)))
  83. # here is all the target's templates
  84. # you may edit them to fit your needs
  85. # - the %(HEADERn)s strings represent the Header lines
  86. # - use %% to represent a literal %
  87. #
  88. HEADER_TEMPLATE = {
  89. 'txt': """\
  90. %(HEADER1)s
  91. %(HEADER2)s
  92. %(HEADER3)s
  93. """,
  94. 'sgml': """\
  95. <!doctype linuxdoc system>
  96. <article>
  97. <title>%(HEADER1)s
  98. <author>%(HEADER2)s
  99. <date>%(HEADER3)s
  100. """,
  101. 'html': """\
  102. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  103. <HTML>
  104. <HEAD>
  105. <META NAME="generator" CONTENT="http://txt2tags.sf.net">
  106. <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=%(ENCODING)s">
  107. <LINK REL="stylesheet" TYPE="text/css" HREF="%(STYLE)s">
  108. <TITLE>%(HEADER1)s</TITLE>
  109. </HEAD><BODY BGCOLOR="white" TEXT="black">
  110. <P ALIGN="center"><CENTER><H1>%(HEADER1)s</H1>
  111. <FONT SIZE=4>
  112. <I>%(HEADER2)s</I><BR>
  113. %(HEADER3)s
  114. </FONT></CENTER>
  115. """,
  116. # TODO man section 1 is hardcoded...
  117. 'man': """\
  118. .TH "%(HEADER1)s" 1 %(HEADER3)s "%(HEADER2)s"
  119. """,
  120. # TODO style to <HR>
  121. 'pm6': """\
  122. <PMTags1.0 win><C-COLORTABLE ("Preto" 1 0 0 0)
  123. ><@Normal=
  124. <FONT "Times New Roman"><CCOLOR "Preto"><SIZE 11>
  125. <HORIZONTAL 100><LETTERSPACE 0><CTRACK 127><CSSIZE 70><C+SIZE 58.3>
  126. <C-POSITION 33.3><C+POSITION 33.3><P><CBASELINE 0><CNOBREAK 0><CLEADING -0.05>
  127. <GGRID 0><GLEFT 7.2><GRIGHT 0><GFIRST 0><G+BEFORE 7.2><G+AFTER 0>
  128. <GALIGNMENT "justify"><GMETHOD "proportional"><G& "ENGLISH">
  129. <GPAIRS 12><G%% 120><GKNEXT 0><GKWIDOW 0><GKORPHAN 0><GTABS $>
  130. <GHYPHENATION 2 34 0><GWORDSPACE 75 100 150><GSPACE -5 0 25>
  131. ><@Bullet=<@-PARENT "Normal"><FONT "Abadi MT Condensed Light">
  132. <GLEFT 14.4><G+BEFORE 2.15><G%% 110><GTABS(25.2 l "")>
  133. ><@PreFormat=<@-PARENT "Normal"><FONT "Lucida Console"><SIZE 8><CTRACK 0>
  134. <GLEFT 0><G+BEFORE 0><GALIGNMENT "left"><GWORDSPACE 100 100 100><GSPACE 0 0 0>
  135. ><@Title1=<@-PARENT "Normal"><FONT "Arial"><SIZE 14><B>
  136. <GCONTENTS><GLEFT 0><G+BEFORE 0><GALIGNMENT "left">
  137. ><@Title2=<@-PARENT "Title1"><SIZE 12><G+BEFORE 3.6>
  138. ><@Title3=<@-PARENT "Title1"><SIZE 10><GLEFT 7.2><G+BEFORE 7.2>
  139. ><@Title4=<@-PARENT "Title3">
  140. ><@Title5=<@-PARENT "Title3">
  141. ><@Quote=<@-PARENT "Normal"><SIZE 10><I>>
  142. %(HEADER1)s
  143. %(HEADER2)s
  144. %(HEADER3)s
  145. """,
  146. 'mgp': """\
  147. #!/usr/X11R6/bin/mgp -t 90
  148. %%deffont "normal" xfont "utopia-medium-r", charset "iso8859-1"
  149. %%deffont "normal-i" xfont "utopia-medium-i", charset "iso8859-1"
  150. %%deffont "normal-b" xfont "utopia-bold-r" , charset "iso8859-1"
  151. %%deffont "normal-bi" xfont "utopia-bold-i" , charset "iso8859-1"
  152. %%deffont "mono" xfont "courier-medium-r", charset "iso8859-1"
  153. %%default 1 size 5
  154. %%default 2 size 8, fore "yellow", font "normal-b", center
  155. %%default 3 size 5, fore "white", font "normal", left, prefix " "
  156. %%tab 1 size 4, vgap 30, prefix " ", icon arc "red" 40, leftfill
  157. %%tab 2 prefix " ", icon arc "orange" 40, leftfill
  158. %%tab 3 prefix " ", icon arc "brown" 40, leftfill
  159. %%tab 4 prefix " ", icon arc "darkmagenta" 40, leftfill
  160. %%tab 5 prefix " ", icon arc "magenta" 40, leftfill
  161. %%%%------------------------- end of headers -----------------------------
  162. %%page
  163. %%size 10, center, fore "yellow"
  164. %(HEADER1)s
  165. %%font "normal-i", size 6, fore "white", center
  166. %(HEADER2)s
  167. %%font "mono", size 7, center
  168. %(HEADER3)s
  169. """,
  170. # TODO please, improve me!
  171. 'moin': """\
  172. %(HEADER1)s
  173. %(HEADER2)s
  174. %(HEADER3)s
  175. """,
  176. 'tex': \
  177. r"""\documentclass[11pt,a4paper]{article}
  178. \usepackage{amsfonts,amssymb,graphicx,url}
  179. \usepackage[%(ENCODING)s]{inputenc} %% char encoding
  180. \pagestyle{plain} %% do page numbering ('empty' turns off)
  181. \frenchspacing %% no aditional spaces after periods
  182. \setlength{\parskip}{8pt}\parindent=0pt %% no paragraph indentation
  183. %% uncomment next line for fancy PDF output on Adobe Acrobat Reader
  184. %%\usepackage[pdfstartview=FitV,colorlinks=true,bookmarks=true]{hyperref}
  185. \title{%(HEADER1)s}
  186. \author{%(HEADER2)s}
  187. \begin{document}
  188. \date{%(HEADER3)s}
  189. \maketitle
  190. """
  191. }
  192. #-----------------------------------------------------------------------
  193. def Quit(msg, exitcode=0): print msg ; sys.exit(exitcode)
  194. def Error(msg): print "ERROR: %s"%msg ; sys.exit()
  195. def echo(msg): print '\033[32;1m%s\033[m'%msg # quick debug
  196. def Debug(msg,i=0,linenr=None):
  197. if i > DEBUG: return
  198. if linenr is not None:
  199. print "(%d) %04d:%s"%(i,linenr,msg)
  200. else:
  201. print "(%d) %s"%(i,msg)
  202. def Readfile(file):
  203. if file == '-':
  204. try: data = sys.stdin.readlines()
  205. except: Error('You must feed me with data on STDIN!')
  206. else:
  207. try: f = open(file); data = f.readlines() ; f.close()
  208. except: Error("Cannot read file:\n %s"%file)
  209. return data
  210. def Savefile(file, contents):
  211. try: f = open(file, 'wb')
  212. except: Error("Cannot open file for writing:\n %s"%file)
  213. if type(contents) == type([]): doit = f.writelines
  214. else: doit = f.write
  215. doit(contents) ; f.close()
  216. def ParseConfig(text='',name='', target=''):
  217. ret = {}
  218. if not text: return ret
  219. re_name = name or '[a-z]+'
  220. re_target = target or '[a-z]*'
  221. cfgregex = re.compile("""
  222. ^%%!\s* # leading id with opt spaces
  223. (?P<name>%s)\s* # config name
  224. (\((?P<target>%s)\))? # optional target spec inside ()
  225. \s*:\s* # key:value delimiter with opt spaces
  226. (?P<value>\S.+?) # config value
  227. \s*$ # rstrip() spaces and hit EOL
  228. """%(re_name,re_target), re.I+re.VERBOSE)
  229. prepostregex = re.compile("""
  230. # ---[ PATTERN ]---
  231. ^( "([^"]*)" # "double quoted" or
  232. | '([^']*)' # 'single quoted' or
  233. | ([^\s]+) # single_word
  234. )
  235. \s+ # separated by spaces
  236. # ---[ REPLACE ]---
  237. ( "([^"]*)" # "double quoted" or
  238. | '([^']*)' # 'single quoted' or
  239. | (.*) # anything
  240. )
  241. \s*$
  242. """, re.VERBOSE)
  243. match = cfgregex.match(text)
  244. if match:
  245. ret = {'name' :string.lower(match.group('name') or ''),
  246. 'target':string.lower(match.group('target') or 'all'),
  247. 'value' :match.group('value') }
  248. # Special config with two quoted values (%!preproc: "foo" 'bar')
  249. if ret['name'] in ['preproc','postproc']:
  250. valmatch = prepostregex.search(ret['value'])
  251. if not valmatch: return None
  252. getval = valmatch.group
  253. patt = getval(2) or getval(3) or getval(4) or ''
  254. repl = getval(6) or getval(7) or getval(8) or ''
  255. ret['value'] = (patt, repl)
  256. return ret
  257. class Cmdline:
  258. def __init__(self, cmdline=[], nocheck=0):
  259. self.conf = {}
  260. self.cmdline = cmdline
  261. self.cmdline_conf = {}
  262. self.dft_options = OPTIONS.copy()
  263. self.dft_flags = FLAGS.copy()
  264. self.all_options = self.dft_options.keys()
  265. self.all_flags = self.dft_flags.keys()
  266. self.defaults = self._get_empty_conf()
  267. self.nocheck = nocheck
  268. if cmdline: self.parse()
  269. #TODO protect quotes contents
  270. def _tokenize(self, cmd_string):
  271. return string.split(cmd_string)
  272. def parse(self):
  273. "return a dic with all options:value found"
  274. if not self.cmdline: return {}
  275. Debug("cmdline: %s"%self.cmdline, 1)
  276. options = {'infile': '', 'infiles':''}
  277. # compose valid options list
  278. longopts = ['help','version'] + self.all_flags + \
  279. map(lambda x:x+'=', self.all_options) # add =
  280. cmdline = self.cmdline[1:] # del prog name
  281. # get cmdline options
  282. try: (opt, args) = getopt.getopt(cmdline, 'hVnHt:o:', longopts)
  283. except getopt.GetoptError:
  284. Error('Bad option or missing argument (try --help)')
  285. # get infile, if any
  286. if args:
  287. options['infile'] = args[0]
  288. options['infiles'] = args # multi
  289. # parse all options
  290. for name,val in opt:
  291. if name in ['-h','--help' ]: Quit(usage)
  292. elif name in ['-V','--version']: Quit(versionstr)
  293. elif name in ['-t','--type' ]: options['type'] = val
  294. elif name in ['-o','--outfile' ]: options['outfile'] = val
  295. elif name in ['-n','--enumtitle']: options['enumtitle'] = 1
  296. elif name in ['-H','--noheaders']: options['noheaders'] = 1
  297. elif name in ['--stdout']: options['outfile'] = STDOUT
  298. else: options[name[2:]] = val or 1 # del --
  299. # save results
  300. Debug("cmdline arguments: %s"%options, 1)
  301. self.cmdline_conf = options
  302. def compose(self, conf={}):
  303. "compose full command line from CONF dict"
  304. if not conf: return ''
  305. args = []
  306. cfg = conf.copy()
  307. valid_opts = self.all_options + self.all_flags
  308. use_short = {'noheaders':'H', 'enumtitle':'n'}
  309. # remove useless options
  310. if cfg.get('toconly'):
  311. del cfg['noheaders']
  312. del cfg['outfile'] # defaults to STDOUT
  313. if cfg.get('type') == 'txt':
  314. del cfg['type'] # already default
  315. args.append('--toconly') # must be the first
  316. del cfg['toconly']
  317. # add target type
  318. if cfg.has_key('type'):
  319. args.append('-t '+cfg['type'])
  320. del cfg['type']
  321. # add other options
  322. for key in cfg.keys():
  323. if key not in valid_opts: continue # must be a %!setting
  324. if key == 'outfile': continue # later
  325. val = cfg[key]
  326. if not val: continue
  327. # default values are useless on cmdline
  328. if val == self.dft_options.get(key): continue
  329. # -short format
  330. if key in use_short.keys():
  331. args.append('-'+use_short[key])
  332. continue
  333. # --long format
  334. if key in self.all_flags: # add --option
  335. args.append('--'+key)
  336. else: # add --option=value
  337. args.append('--%s=%s'%(key,val))
  338. # the outfile using -o
  339. if cfg.has_key('outfile') and \
  340. cfg['outfile'] != self.dft_options.get('outfile'):
  341. args.append('-o '+cfg['outfile'])
  342. # the input file is always at the end
  343. if cfg.has_key('infile'):
  344. args.append(cfg['infile'])
  345. # return as a single string
  346. ret = string.join(args,' ')
  347. Debug("Diet command line: %s"%ret, 1)
  348. return ret
  349. def merge(self, extraopts=''):
  350. "insert cmdline portion BEFORE current cmdline"
  351. if not extraopts: return
  352. if type(extraopts) == type(''):
  353. extraopts = self._tokenize(extraopts)
  354. if not self.cmdline: self.cmdline = extraopts
  355. else: self.cmdline = ['t2t-merged'] +extraopts +self.cmdline[1:]
  356. self.parse()
  357. def _get_outfile_name(self, conf):
  358. "dirname is the same for {in,out}file"
  359. infile = conf['infile']
  360. if not infile: return ''
  361. if infile == STDIN or conf['outfile'] == STDOUT:
  362. outfile = STDOUT
  363. else:
  364. basename = re.sub('\.(txt|t2t)$','',infile)
  365. outfile = "%s.%s"%(basename, conf['type'])
  366. self.dft_options['outfile'] = outfile # save for self.compose()
  367. Debug(" infile: '%s'"%infile , 1)
  368. Debug("outfile: '%s'"%outfile, 1)
  369. return outfile
  370. def _sanity(self, dic):
  371. "basic cmdline syntax checkings"
  372. if not dic: return {}
  373. if not dic['infile'] or not dic['type']:
  374. Quit(usage, 1) # no filename/doctype
  375. if not targets.count(dic['type']): # check target
  376. Error("Invalid document type '%s' (try --help)"%(
  377. dic['type']))
  378. #DISABLED: conflicting with %!cmdline: -o foo
  379. #if len(dic['infiles']) > 1 and dic['outfile']: # -o FILE *.t2t
  380. # Error("--outfile can't be used with multiple files")
  381. for opt in self.all_options: # check numeric options
  382. opttype = type(self.dft_options[opt])
  383. if dic.get(opt) and opttype == type(9):
  384. try: dic[opt] = int(dic.get(opt)) # save
  385. except: Error('--%s value must be a number'%opt)
  386. if dic['split'] not in [0,1,2]: # check split level
  387. Error('Option --split must be 0, 1 or 2')
  388. return dic
  389. def merge_conf(self, newconfs={}, override=0):
  390. "include Config Area settings into self.conf"
  391. if not self.conf: self.get_conf()
  392. if not newconfs: return self.conf
  393. for key in newconfs.keys():
  394. if key == 'cmdline': continue # already done
  395. # filters are always accumulative
  396. if key in ['preproc','postproc']:
  397. if not self.conf.has_key(key):
  398. self.conf[key] = []
  399. self.conf[key].extend(newconfs[key])
  400. continue
  401. # add anyway
  402. if override:
  403. self.conf[key] = newconfs[key]
  404. continue
  405. # just update if still 'virgin'
  406. if self.conf.has_key(key) and \
  407. self.conf[key] == self.defaults.get(key):
  408. self.conf[key] = newconfs[key]
  409. # add new
  410. if not self.conf.has_key(key):
  411. self.conf[key] = newconfs[key]
  412. Debug("Merged CONF (override=%s): %s"%(override,self.conf), 1)
  413. return self.conf
  414. def _get_empty_conf(self):
  415. econf = self.dft_options.copy()
  416. for k in self.dft_flags.keys(): econf[k] = self.dft_flags[k]
  417. return econf
  418. def get_conf(self):
  419. "set vars and flags according to options dic"
  420. if not self.cmdline_conf:
  421. if not self.cmdline: return {}
  422. self.parse()
  423. dic = self.cmdline_conf
  424. conf = self.defaults.copy()
  425. ## store flags & options
  426. for flag in self.all_flags:
  427. if dic.has_key(flag): conf[flag] = 1
  428. for opt in self.all_options + ['infile', 'infiles']:
  429. if dic.has_key(opt): conf[opt] = dic.get(opt)
  430. if not conf['type'] and conf['toconly']: conf['type'] = 'txt'
  431. if not self.nocheck: conf = self._sanity(conf)
  432. ## some gotchas for specific issues
  433. doctype = conf['type']
  434. infile = conf['infile']
  435. # toconly is stronger than others
  436. if conf['toconly']:
  437. conf['noheaders'] = 1
  438. conf['toc'] = 0
  439. conf['split'] = 0
  440. conf['gui'] = 0
  441. conf['outfile'] = STDOUT
  442. conf['toclevel'] = conf['toclevel'] or \
  443. self.dft_options['toclevel']
  444. # split: just HTML, no stdout, 1st do a sgml, then sgml2html
  445. if conf['split']:
  446. if doctype != 'html':
  447. conf['split'] = 0
  448. else:
  449. conf['type'] = 'sgml'
  450. if conf['outfile'] == STDOUT:
  451. conf['outfile'] = ''
  452. outfile = conf['outfile'] or self._get_outfile_name(conf)
  453. # final checkings
  454. if conf['split'] and outfile == STDOUT:
  455. Error('--split: You must provide a FILE (not STDIN)')
  456. if infile == outfile and outfile != STDOUT:
  457. Error("SUICIDE WARNING!!! (see --outfile)\n source"+\
  458. " and target files has the same name: "+outfile)
  459. ### author's note: "yes, i've got my sample.t2t file deleted
  460. ### before add this test... :/"
  461. conf['outfile'] = outfile
  462. conf['cmdline'] = self.cmdline
  463. Debug("CONF data: %s\n"%conf, 1)
  464. self.conf = conf
  465. return self.conf
  466. #
  467. ### End of Cmdline class
  468. class Proprierties:
  469. def __init__(self, filename=''):
  470. self.buffer = [''] # text start at pos 1
  471. self.areas = ['head','conf','body']
  472. self.arearef = []
  473. self.headers = ['','','']
  474. self.config = self.get_empty_config()
  475. self.lastline = 0
  476. self.filename = filename
  477. self.conflines = []
  478. self.bodylines = []
  479. if filename:
  480. self.read_file(filename)
  481. self.find_areas()
  482. self.set_headers()
  483. self.set_config()
  484. def read_file(self, file):
  485. lines = Readfile(file)
  486. if not lines: Error('Empty file! %s'%file)
  487. self.buffer.extend(lines)
  488. def get_empty_config(self):
  489. empty = {}
  490. for targ in targets+['all']: empty[targ] = {}
  491. return empty
  492. def find_areas(self):
  493. "Run through buffer and identify head/conf/body areas"
  494. buf = self.buffer ; ref = [1,4,0] # defaults
  495. if not string.strip(buf[1]): # no header
  496. ref[0] = 0 ; ref[1] = 2
  497. for i in range(ref[1],len(buf)): # find body init
  498. if string.strip(buf[i]) and buf[i][0] != '%':
  499. ref[2] = i ; break # !blank, !comment
  500. if ref[1] == ref[2]: ref[1] = 0 # no conf area
  501. for i in 0,1,2: # del !existent
  502. if not ref[i]: self.areas[i] = ''
  503. self.arearef = ref # save results
  504. self.lastline = len(self.buffer)-1
  505. Debug('Head,Conf,Body start line: %s'%ref, 1)
  506. # store CONF and BODY lines found
  507. cfgend = ref[2] or len(buf)
  508. self.conflines = buf[ref[1]:cfgend]
  509. if ref[2]: self.bodylines = buf[ref[2]:]
  510. def set_headers(self):
  511. "Extract and save headers contents"
  512. if not self.arearef: self.find_areas()
  513. if not self.areas.count('head'): return
  514. if self.lastline < 3:
  515. #TODO on gui this checking is !working
  516. Error(
  517. "Premature end of Headers on '%s'."%self.filename +\
  518. '\n\nFile has %s line(s), but '%self.lastline +\
  519. 'Headers should be composed by 3 lines. ' +\
  520. '\nMaybe you should left the first line blank? ' +\
  521. '(for no headers)')
  522. for i in 0,1,2:
  523. self.headers[i] = string.strip(self.buffer[i+1])
  524. Debug("Headers found: %s"%self.headers, 1, i+1)
  525. def set_config(self):
  526. "Extract and save config contents (including includes)"
  527. if not self.arearef: self.find_areas()
  528. if not self.areas.count('conf'): return
  529. keywords = string.join(CONFIG_KEYWORDS, '|')
  530. linenr = self.arearef[1]-1 # for debug messages
  531. for line in self.conflines:
  532. linenr = linenr + 1
  533. if len(line) < 3: continue
  534. if line[:2] != '%!': continue
  535. cfg = ParseConfig(line, keywords)
  536. # any _valid_ config found?
  537. if not cfg:
  538. Debug('Bogus Config Line',1,linenr)
  539. continue
  540. # get data
  541. targ, key, val = cfg['target'],cfg['name'], cfg['value']
  542. # check config target specification
  543. if targ not in targets+['all']:
  544. Debug("Config Error: Invalid target '%s', ignoring"%targ,
  545. 1,linenr)
  546. continue
  547. # filters are multiple config
  548. if key in ['preproc','postproc']:
  549. if not self.config['all'].has_key(key): # 1st one
  550. self.config['all'][key] = []
  551. # all filters are saved to target 'all'
  552. # finish_him will decide what to consider
  553. self.config['all'][key].append((targ,)+val)
  554. else:
  555. self.config[targ][key] = val
  556. Debug("Found config for target '%s': '%s', value '%s'"%(
  557. targ,key,val),1,linenr)
  558. Debug("All %%!CONFIG: %s"%self.config, 1)
  559. def get_file_body(file):
  560. "Returns all the document BODY lines (including includes)"
  561. prop = Proprierties()
  562. prop.read_file(file)
  563. prop.find_areas()
  564. return prop.bodylines
  565. def finish_him(outlist, CONF):
  566. "Writing output to screen or file"
  567. outfile = CONF['outfile']
  568. outlist = unmaskEscapeChar(outlist)
  569. # do PostProc
  570. if CONF['postproc']:
  571. postoutlist = []
  572. for line in outlist:
  573. for targ,patt,repl in CONF['postproc']:
  574. if targ not in [CONF['type'], 'all']: continue
  575. line = re.sub(patt, repl, line)
  576. postoutlist.append(line)
  577. outlist = postoutlist[:]
  578. if outfile == STDOUT:
  579. for line in outlist: print line
  580. else:
  581. Savefile(outfile, addLineBreaks(outlist))
  582. if not CONF['gui']: print 'wrote %s'%(outfile)
  583. if CONF['split']:
  584. print "--- html..."
  585. sgml2html = 'sgml2html -s %s -l %s %s'%(
  586. CONF['split'],CONF['lang'] or lang,outfile)
  587. print "Running system command:", sgml2html
  588. os.system(sgml2html)
  589. def toc_maker(toc, conf):
  590. "Compose TOC list 'by hand'"
  591. # TOC is a tag, so there's nothing to do here
  592. if TAGS['TOC']: return []
  593. # toc is a valid t2t marked text (list type), that is converted
  594. if conf['toc'] or conf['toconly']:
  595. fakeconf = conf.copy()
  596. fakeconf['noheaders'] = 1
  597. fakeconf['toconly'] = 0
  598. fakeconf['maskemail'] = 0
  599. fakeconf['preproc'] = []
  600. fakeconf['postproc'] = []
  601. toc,foo = convert(toc, fakeconf)
  602. # TOC between bars (not for --toconly)
  603. if conf['toc']:
  604. para = TAGS['paragraph']
  605. tocbar = [para, regex['x'].sub('-'*72,TAGS['bar1']), para]
  606. toc = tocbar + toc + tocbar
  607. return toc
  608. def getTags(doctype):
  609. keys = [
  610. 'paragraph','title1','title2','title3','title4','title5',
  611. 'numtitle1','numtitle2','numtitle3','numtitle4','numtitle5',
  612. 'areaPreOpen','areaPreClose',
  613. 'areaQuoteOpen','areaQuoteClose',
  614. 'fontMonoOpen','fontMonoClose',
  615. 'fontBoldOpen','fontBoldClose',
  616. 'fontItalicOpen','fontItalicClose',
  617. 'fontBolditalicOpen','fontBolditalicClose',
  618. 'fontUnderlineOpen','fontUnderlineClose',
  619. 'listOpen','listClose','listItem',
  620. 'numlistOpen','numlistClose','numlistItem',
  621. 'deflistOpen','deflistClose','deflistItem1','deflistItem2',
  622. 'bar1','bar2',
  623. 'url','urlMark','email','emailMark',
  624. 'img','imgsolo',
  625. 'tableOpen','tableClose','tableLineOpen','tableLineClose',
  626. 'tableCellOpen','tableCellClose',
  627. 'tableTitleCellOpen','tableTitleCellClose',
  628. 'anchor','comment','TOC',
  629. 'EOD'
  630. ]
  631. alltags = {
  632. 'txt': {
  633. 'title1' : ' \a' ,
  634. 'title2' : '\t\a' ,
  635. 'title3' : '\t\t\a' ,
  636. 'title4' : '\t\t\t\a' ,
  637. 'title5' : '\t\t\t\t\a',
  638. 'areaQuoteOpen' : ' ' ,
  639. 'listItem' : '- ' ,
  640. 'numlistItem' : '\a. ' ,
  641. 'bar1' : '\a' ,
  642. 'bar2' : '\a' ,
  643. 'url' : '\a' ,
  644. 'urlMark' : '\a (\a)' ,
  645. 'email' : '\a' ,
  646. 'emailMark' : '\a (\a)' ,
  647. 'img' : '[\a]' ,
  648. },
  649. 'html': {
  650. 'paragraph' : '<P>' ,
  651. 'title1' : '<H1>\a</H1>' ,
  652. 'title2' : '<H2>\a</H2>' ,
  653. 'title3' : '<H3>\a</H3>' ,
  654. 'title4' : '<H4>\a</H4>' ,
  655. 'title5' : '<H5>\a</H5>' ,
  656. 'areaPreOpen' : '<PRE>' ,
  657. 'areaPreClose' : '</PRE>' ,
  658. 'areaQuoteOpen' : '<BLOCKQUOTE>' ,
  659. 'areaQuoteClose' : '</BLOCKQUOTE>' ,
  660. 'fontMonoOpen' : '<CODE>' ,
  661. 'fontMonoClose' : '</CODE>' ,
  662. 'fontBoldOpen' : '<B>' ,
  663. 'fontBoldClose' : '</B>' ,
  664. 'fontItalicOpen' : '<I>' ,
  665. 'fontItalicClose' : '</I>' ,
  666. 'fontBolditalicOpen' : '<B><I>' ,
  667. 'fontBolditalicClose' : '</I></B>' ,
  668. 'fontUnderlineOpen' : '<U>' ,
  669. 'fontUnderlineClose' : '</U>' ,
  670. 'listOpen' : '<UL>' ,
  671. 'listClose' : '</UL>' ,
  672. 'listItem' : '<LI>' ,
  673. 'numlistOpen' : '<OL>' ,
  674. 'numlistClose' : '</OL>' ,
  675. 'numlistItem' : '<LI>' ,
  676. 'deflistOpen' : '<DL>' ,
  677. 'deflistClose' : '</DL>' ,
  678. 'deflistItem1' : '<DT>\a</DT>' ,
  679. 'deflistItem2' : '<DD>' ,
  680. 'bar1' : '<HR NOSHADE SIZE=1>' ,
  681. 'bar2' : '<HR NOSHADE SIZE=5>' ,
  682. 'url' : '<A HREF="\a">\a</A>' ,
  683. 'urlMark' : '<A HREF="\a">\a</A>' ,
  684. 'email' : '<A HREF="mailto:\a">\a</A>' ,
  685. 'emailMark' : '<A HREF="mailto:\a">\a</A>' ,
  686. 'img' : '<IMG ALIGN="\a" SRC="\a" BORDER="0">',
  687. 'imgsolo' : '<P ALIGN="center">\a</P>' ,
  688. 'tableOpen' : '<table\a cellpadding=4 border=\a>',
  689. 'tableClose' : '</table>' ,
  690. 'tableLineOpen' : '<tr>' ,
  691. 'tableLineClose' : '</tr>' ,
  692. 'tableCellOpen' : '<td\a>' ,
  693. 'tableCellClose' : '</td>' ,
  694. 'tableTitleCellOpen' : '<th>' ,
  695. 'tableTitleCellClose' : '</th>' ,
  696. 'tableAlignLeft' : '' ,
  697. 'tableAlignCenter' : ' align="center"',
  698. 'tableCellAlignLeft' : '' ,
  699. 'tableCellAlignRight' : ' align="right"' ,
  700. 'tableCellAlignCenter': ' align="center"',
  701. 'anchor' : '<a name="\a"></a>',
  702. 'comment' : '<!-- \a -->' ,
  703. 'EOD' : '</BODY></HTML>'
  704. },
  705. 'sgml': {
  706. 'paragraph' : '<p>' ,
  707. 'title1' : '<sect>\a<p>' ,
  708. 'title2' : '<sect1>\a<p>' ,
  709. 'title3' : '<sect2>\a<p>' ,
  710. 'title4' : '<sect3>\a<p>' ,
  711. 'title5' : '<sect4>\a<p>' ,
  712. 'areaPreOpen' : '<tscreen><verb>' ,
  713. 'areaPreClose' : '</verb></tscreen>' ,
  714. 'areaQuoteOpen' : '<quote>' ,
  715. 'areaQuoteClose' : '</quote>' ,
  716. 'fontMonoOpen' : '<tt>' ,
  717. 'fontMonoClose' : '</tt>' ,
  718. 'fontBoldOpen' : '<bf>' ,
  719. 'fontBoldClose' : '</bf>' ,
  720. 'fontItalicOpen' : '<em>' ,
  721. 'fontItalicClose' : '</em>' ,
  722. 'fontBolditalicOpen' : '<bf><em>' ,
  723. 'fontBolditalicClose' : '</em></bf>' ,
  724. 'fontUnderlineOpen' : '<bf><em>' ,
  725. 'fontUnderlineClose' : '</em></bf>' ,
  726. 'listOpen' : '<itemize>' ,
  727. 'listClose' : '</itemize>' ,
  728. 'listItem' : '<item>' ,
  729. 'numlistOpen' : '<enum>' ,
  730. 'numlistClose' : '</enum>' ,
  731. 'numlistItem' : '<item>' ,
  732. 'deflistOpen' : '<descrip>' ,
  733. 'deflistClose' : '</descrip>' ,
  734. 'deflistItem1' : '<tag>\a</tag>' ,
  735. 'bar1' : '<!-- \a -->' ,
  736. 'bar2' : '<!-- \a -->' ,
  737. 'url' : '<htmlurl url="\a" name="\a">' ,
  738. 'urlMark' : '<htmlurl url="\a" name="\a">' ,
  739. 'email' : '<htmlurl url="mailto:\a" name="\a">' ,
  740. 'emailMark' : '<htmlurl url="mailto:\a" name="\a">' ,
  741. 'img' : '<figure><ph vspace=""><img src="\a">'+\
  742. '</figure>' ,
  743. 'tableOpen' : '<table><tabular ca="\a">' ,
  744. 'tableClose' : '</tabular></table>' ,
  745. 'tableLineClose' : '<rowsep>' ,
  746. 'tableCellClose' : '<colsep>' ,
  747. 'tableTitleCellClose' : '<colsep>' ,
  748. 'tableColAlignLeft' : 'l' ,
  749. 'tableColAlignRight' : 'r' ,
  750. 'tableColAlignCenter' : 'c' ,
  751. 'comment' : '<!-- \a -->' ,
  752. 'TOC' : '<toc>' ,
  753. 'EOD' : '</article>'
  754. },
  755. 'tex': {
  756. 'title1' : '\n\\newpage\section*{\a}',
  757. 'title2' : '\\subsection*{\a}' ,
  758. 'title3' : '\\subsubsection*{\a}' ,
  759. # title 4/5: DIRTY: para+BF+\\+\n
  760. 'title4' : '\\paragraph{}\\textbf{\a}\\\\\n',
  761. 'title5' : '\\paragraph{}\\textbf{\a}\\\\\n',
  762. 'numtitle1' : '\n\\newpage\section{\a}',
  763. 'numtitle2' : '\\subsection{\a}' ,
  764. 'numtitle3' : '\\subsubsection{\a}' ,
  765. 'areaPreOpen' : '\\begin{verbatim}' ,
  766. 'areaPreClose' : '\\end{verbatim}' ,
  767. 'areaQuoteOpen' : '\\begin{quotation}' ,
  768. 'areaQuoteClose' : '\\end{quotation}' ,
  769. 'fontMonoOpen' : '\\texttt{' ,
  770. 'fontMonoClose' : '}' ,
  771. 'fontBoldOpen' : '\\textbf{' ,
  772. 'fontBoldClose' : '}' ,
  773. 'fontItalicOpen' : '\\textit{' ,
  774. 'fontItalicClose' : '}' ,
  775. 'fontBolditalicOpen' : '\\textbf{\\textit{' ,
  776. 'fontBolditalicClose' : '}}' ,
  777. 'fontUnderlineOpen' : '\\underline{' ,
  778. 'fontUnderlineClose' : '}' ,
  779. 'listOpen' : '\\begin{itemize}' ,
  780. 'listClose' : '\\end{itemize}' ,
  781. 'listItem' : '\\item ' ,
  782. 'numlistOpen' : '\\begin{enumerate}' ,
  783. 'numlistClose' : '\\end{enumerate}' ,
  784. 'numlistItem' : '\\item ' ,
  785. 'deflistOpen' : '\\begin{description}',
  786. 'deflistClose' : '\\end{description}' ,
  787. 'deflistItem1' : '\\item[\a]' ,
  788. 'bar1' : '\n\\hrulefill{}\n' ,
  789. 'bar2' : '\n\\rule{\linewidth}{1mm}\n',
  790. 'url' : '\\url{\a}' ,
  791. 'urlMark' : '\\textit{\a} (\\url{\a})' ,
  792. 'email' : '\\url{\a}' ,
  793. 'emailMark' : '\\textit{\a} (\\url{\a})' ,
  794. 'img' : '\\begin{figure}\\includegraphics{\a}'+\
  795. '\\end{figure}',
  796. 'tableOpen' : '\\begin{center}\\begin{tabular}{\a|}',
  797. 'tableClose' : '\\end{tabular}\\end{center}',
  798. 'tableLineOpen' : '\\hline ' ,
  799. 'tableLineClose' : ' \\\\' ,
  800. 'tableCellClose' : ' & ' ,
  801. 'tableTitleCellOpen' : '\\textbf{',
  802. 'tableTitleCellClose' : '} & ' ,
  803. 'tableColAlignLeft' : '|l' ,
  804. 'tableColAlignRight' : '|r' ,
  805. 'tableColAlignCenter' : '|c' ,
  806. 'comment' : '% \a' ,
  807. 'TOC' : '\\newpage\\tableofcontents',
  808. 'EOD' : '\\end{document}'
  809. },
  810. 'moin': {
  811. 'title1' : '= \a =' ,
  812. 'title2' : '== \a ==' ,
  813. 'title3' : '=== \a ===' ,
  814. 'title4' : '==== \a ====' ,
  815. 'title5' : '===== \a =====',
  816. 'areaPreOpen' : '{{{' ,
  817. 'areaPreClose' : '}}}' ,
  818. 'areaQuoteOpen' : ' ' ,
  819. 'fontMonoOpen' : '{{{' ,
  820. 'fontMonoClose' : '}}}' ,
  821. 'fontBoldOpen' : "'''" ,
  822. 'fontBoldClose' : "'''" ,
  823. 'fontItalicOpen' : "''" ,
  824. 'fontItalicClose' : "''" ,
  825. 'fontBolditalicOpen' : "'''''" ,
  826. 'fontBolditalicClose' : "'''''" ,
  827. 'fontUnderlineOpen' : "'''''" ,
  828. 'fontUnderlineClose' : "'''''" ,
  829. 'listItem' : '* ' ,
  830. 'numlistItem' : '\a. ' ,
  831. 'bar1' : '----' ,
  832. 'bar2' : '----' ,
  833. 'url' : '[\a]' ,
  834. 'urlMark' : '[\a \a]' ,
  835. 'email' : '[\a]' ,
  836. 'emailMark' : '[\a \a]' ,
  837. 'img' : '[\a]' ,
  838. 'tableLineOpen' : '||' ,
  839. 'tableCellClose' : '||' ,
  840. 'tableTitleCellClose' : '||'
  841. },
  842. 'mgp': {
  843. 'paragraph' : '%font "normal", size 5\n' ,
  844. 'title1' : '%page\n\n\a' ,
  845. 'title2' : '%page\n\n\a' ,
  846. 'title3' : '%page\n\n\a' ,
  847. 'title4' : '%page\n\n\a' ,
  848. 'title5' : '%page\n\n\a' ,
  849. 'areaPreOpen' : '\n%font "mono"' ,
  850. 'areaPreClose' : '%font "normal"' ,
  851. 'areaQuoteOpen' : '%prefix " "' ,
  852. 'areaQuoteClose' : '%prefix " "' ,
  853. 'fontMonoOpen' : '\n%cont, font "mono"\n' ,
  854. 'fontMonoClose' : '\n%cont, font "normal"\n' ,
  855. 'fontBoldOpen' : '\n%cont, font "normal-b"\n' ,
  856. 'fontBoldClose' : '\n%cont, font "normal"\n' ,
  857. 'fontItalicOpen' : '\n%cont, font "normal-i"\n' ,
  858. 'fontItalicClose' : '\n%cont, font "normal"\n' ,
  859. 'fontBolditalicOpen' : '\n%cont, font "normal-bi"\n',
  860. 'fontBolditalicClose' : '\n%cont, font "normal"\n' ,
  861. 'fontUnderlineOpen' : '\n%cont, fore "cyan"\n' ,
  862. 'fontUnderlineClose' : '\n%cont, fore "white"\n' ,
  863. 'numlistItem' : '\a. ' ,
  864. 'bar1' : '%bar "white" 5' ,
  865. 'bar2' : '%pause' ,
  866. 'url' : '\n%cont, fore "cyan"\n\a' +\
  867. '\n%cont, fore "white"\n' ,
  868. 'urlMark' : '\a \n%cont, fore "cyan"\n\a'+\
  869. '\n%cont, fore "white"\n' ,
  870. 'email' : '\n%cont, fore "cyan"\n\a' +\
  871. '\n%cont, fore "white"\n' ,
  872. 'emailMark' : '\a \n%cont, fore "cyan"\n\a'+\
  873. '\n%cont, fore "white"\n' ,
  874. 'img' : '\n%center\n%newimage "\a", left\n',
  875. 'comment' : '%% \a' ,
  876. 'EOD' : '%%EOD'
  877. },
  878. 'man': {
  879. 'paragraph' : '.P' ,
  880. 'title1' : '.SH \a' ,
  881. 'title2' : '.SS \a' ,
  882. 'title3' : '.SS \a' ,
  883. 'title4' : '.SS \a' ,
  884. 'title5' : '.SS \a' ,
  885. 'areaPreOpen' : '.nf' ,
  886. 'areaPreClose' : '.fi\n' ,
  887. 'areaQuoteOpen' : '\n' ,
  888. 'areaQuoteClose' : '\n' ,
  889. 'fontBoldOpen' : '\\fB' ,
  890. 'fontBoldClose' : '\\fP' ,
  891. 'fontItalicOpen' : '\\fI' ,
  892. 'fontItalicClose' : '\\fP' ,
  893. 'fontBolditalicOpen' : '\\fI' ,
  894. 'fontBolditalicClose' : '\\fP' ,
  895. 'listOpen' : '\n.nf' , # pre
  896. 'listClose' : '.fi\n' ,
  897. 'listItem' : '* ' ,
  898. 'numlistOpen' : '\n.nf' , # pre
  899. 'numlistClose' : '.fi\n' ,
  900. 'numlistItem' : '\a. ' ,
  901. 'bar1' : '\n\n' ,
  902. 'bar2' : '\n\n' ,
  903. 'url' : '\a' ,
  904. 'urlMark' : '\a (\a)',
  905. 'email' : '\a' ,
  906. 'emailMark' : '\a (\a)',
  907. 'img' : '\a' ,
  908. 'comment' : '.\\" \a'
  909. },
  910. 'pm6': {
  911. 'paragraph' : '<@Normal:>' ,
  912. 'title1' : '\n<@Title1:>\a',
  913. 'title2' : '\n<@Title2:>\a',
  914. 'title3' : '\n<@Title3:>\a',
  915. 'title4' : '\n<@Title4:>\a',
  916. 'title5' : '\n<@Title5:>\a',
  917. 'areaPreOpen' : '<@PreFormat:>' ,
  918. 'areaQuoteOpen' : '<@Quote:>' ,
  919. 'fontMonoOpen' : '<FONT "Lucida Console"><SIZE 9>' ,
  920. 'fontMonoClose' : '<SIZE$><FONT$>',
  921. 'fontBoldOpen' : '<B>' ,
  922. 'fontBoldClose' : '<P>' ,
  923. 'fontItalicOpen' : '<I>' ,
  924. 'fontItalicClose' : '<P>' ,
  925. 'fontBolditalicOpen' : '<B><I>' ,
  926. 'fontBolditalicClose' : '<P>' ,
  927. 'fontUnderlineOpen' : '<U>' ,
  928. 'fontUnderlineClose' : '<P>' ,
  929. 'listOpen' : '<@Bullet:>' ,
  930. 'listItem' : '\x95 ' , # \x95 == ~U
  931. 'numlistOpen' : '<@Bullet:>' ,
  932. 'numlistItem' : '\x95 ' ,
  933. 'bar1' : '\a' ,
  934. 'bar2' : '\a' ,
  935. 'url' : '<U>\a<P>' , # underline
  936. 'urlMark' : '\a <U>\a<P>' ,
  937. 'email' : '\a' ,
  938. 'emailMark' : '\a \a' ,
  939. 'img' : '\a'
  940. }
  941. }
  942. # compose the target tags dictionary
  943. tags = {}
  944. target_tags = alltags[doctype]
  945. for key in keys: tags[key] = '' # create empty keys
  946. for key in target_tags.keys():
  947. tags[key] = maskEscapeChar(target_tags[key]) # populate
  948. return tags
  949. def getRules(doctype):
  950. ret = {}
  951. allrules = [
  952. # target rules (ON/OFF)
  953. 'linkable', # target supports external links
  954. 'tableable', # target supports tables
  955. 'imglinkable', # target supports images as links
  956. 'imgalignable', # target supports image alignment
  957. 'imgasdefterm', # target supports image as definition term
  958. 'tablealignable', # target supports table alignment
  959. 'autonumberlist', # target supports numbered lists natively
  960. 'autonumbertitle', # target supports numbered titles natively
  961. 'tablecellsplit', # place delimiters only *between* cells
  962. 'listnotnested', # lists cannot be nested
  963. 'quotenotnested', # quotes cannot be nested
  964. 'preareanotescaped', # don't escape specials in PRE area
  965. 'escapeurl', # escape special in link URL
  966. # target code beautify (ON/OFF)
  967. 'indentprearea', # add leading spaces to PRE area lines
  968. 'breaktablecell', # break lines after any table cell
  969. 'breaktablelineopen', # break line after opening table line
  970. 'keepquoteindent', # don't remove the leading TABs on quotes
  971. # value settings
  972. 'listmaxdepth', # maximum depth for lists
  973. 'tablecellaligntype' # type of table cell align: cell, column
  974. ]
  975. rules = {
  976. 'txt' : {
  977. 'indentprearea':1
  978. },
  979. 'html': {
  980. 'indentprearea':1,
  981. 'linkable':1,
  982. 'imglinkable':1,
  983. 'imgalignable':1,
  984. 'imgasdefterm':1,
  985. 'autonumberlist':1,
  986. 'tableable':1,
  987. 'breaktablecell':1,
  988. 'breaktablelineopen':1,
  989. 'keepquoteindent':1,
  990. 'tablealignable':1,
  991. 'tablecellaligntype':'cell'
  992. },
  993. 'sgml': {
  994. 'linkable':1,
  995. 'escapeurl':1,
  996. 'autonumberlist':1,
  997. 'tableable':1,
  998. 'tablecellsplit':1,
  999. 'quotenotnested':1,
  1000. 'keepquoteindent':1,
  1001. 'tablecellaligntype':'column'
  1002. },
  1003. 'mgp' : {
  1004. },
  1005. 'tex' : {
  1006. 'autonumberlist':1,
  1007. 'autonumbertitle':1,
  1008. 'tableable':1,
  1009. 'tablecellsplit':1,
  1010. 'preareanotescaped':1,
  1011. 'listmaxdepth':4,
  1012. 'tablecellaligntype':'column'
  1013. },
  1014. 'moin': {
  1015. 'linkable':1,
  1016. 'tableable':1
  1017. },
  1018. 'man' : {
  1019. 'indentprearea':1,
  1020. 'listnotnested':1
  1021. },
  1022. 'pm6' : {
  1023. }
  1024. }
  1025. # populate return dictionary
  1026. myrules = rules[doctype]
  1027. for key in allrules : ret[key] = 0 # reset all
  1028. for key in myrules.keys(): ret[key] = myrules[key] # turn ON
  1029. return ret
  1030. def getRegexes():
  1031. regex = {
  1032. # extra at end: (\[(?P<label>\w+)\])?
  1033. 'title':
  1034. re.compile(r'^\s*(?P<id>={1,5})(?P<txt>[^=].*[^=])\1\s*$'),
  1035. 'numtitle':
  1036. re.compile(r'^\s*(?P<id>\+{1,5})(?P<txt>[^+].*[^+])\1\s*$'),
  1037. 'areaPreOpen':
  1038. re.compile(r'^---$'),
  1039. 'areaPreClose':
  1040. re.compile(r'^---$'),
  1041. 'quote':
  1042. re.compile(r'^\t+'),
  1043. '1linePre':
  1044. re.compile(r'^--- (?=.)'),
  1045. 'fontMono':
  1046. re.compile(r'`([^`]+)`'),
  1047. 'fontBold':
  1048. re.compile(r'\*\*([^\s*].*?)\*\*'),
  1049. 'fontItalic':
  1050. re.compile(r'(^|[^:])//([^ /].*?)//'),
  1051. 'fontUnderline':
  1052. re.compile(r'__([^_].*?)__'), # underline lead/trailing blank
  1053. 'fontBolditalic':
  1054. re.compile(r'\*/([^/].*?)/\*'),
  1055. 'list':
  1056. re.compile(r'^( *)([+-]) ([^ ])'),
  1057. 'deflist':
  1058. re.compile(r'^( *)(=) ([^:]+):'),
  1059. 'bar':
  1060. re.compile(r'^\s*([_=-]{20,})\s*$'),
  1061. 'table':
  1062. re.compile(r'^ *\|\|? '),
  1063. 'blankline':
  1064. re.compile(r'^\s*$'),
  1065. 'comment':
  1066. re.compile(r'^%'),
  1067. 'raw':
  1068. re.compile(r'``(.+?)``')
  1069. }
  1070. # special char to place data on TAGs contents (\a == bell)
  1071. regex['x'] = re.compile('\a')
  1072. # %%date [ (formatting) ]
  1073. regex['date'] = re.compile(r'%%date\b(\((?P<fmt>.*?)\))?', re.I)
  1074. ### complicated regexes begin here ;)
  1075. #
  1076. # textual descriptions on --help's style: [...] is optional, | is OR
  1077. ### first, some auxiliar variables
  1078. #
  1079. # [image.EXT]
  1080. patt_img = r'\[([\w_,.+%$#@!?+~/-]+\.(png|jpe?g|gif|eps|bmp))\]'
  1081. # link things
  1082. urlskel = {
  1083. 'proto' : r'(https?|ftp|news|telnet|gopher|wais)://',
  1084. 'guess' : r'(www[23]?|ftp)\.', # w/out proto, try to guess
  1085. 'login' : r'A-Za-z0-9_.-', # for ftp://login@domain.com
  1086. 'pass' : r'[^ @]*', # for ftp://login:password@dom.com
  1087. 'chars' : r'A-Za-z0-9%._/~:,=$@-',# %20(space), :80(port)
  1088. 'anchor': r'A-Za-z0-9%._-', # %nn(encoded)
  1089. 'form' : r'A-Za-z0-9/%&=+.,@*_-',# .,@*_-(as is)
  1090. 'punct' : r'.,;:!?'
  1091. }
  1092. # username [ :password ] @
  1093. patt_url_login = r'([%s]+(:%s)?@)?'%(urlskel['login'],urlskel['pass'])
  1094. # [ http:// ] [ username:password@ ] domain.com [ / ]
  1095. # [ #anchor | ?form=data ]
  1096. retxt_url = r'\b(%s%s|%s)[%s]+\b/*(\?[%s]+)?(#[%s]+)?'%(
  1097. urlskel['proto'],patt_url_login, urlskel['guess'],
  1098. urlskel['chars'],urlskel['form'],urlskel['anchor'])
  1099. # filename | [ filename ] #anchor
  1100. retxt_url_local = r'[%s]+|[%s]*(#[%s]+)'%(
  1101. urlskel['chars'],urlskel['chars'],urlskel['anchor'])
  1102. # user@domain [ ?form=data ]
  1103. patt_email = r'\b[%s]+@([A-Za-z0-9_-]+\.)+[A-Za-z]{2,4}\b(\?[%s]+)?'%(
  1104. urlskel['login'],urlskel['form'])
  1105. # saving for future use
  1106. regex['_urlskel'] = urlskel
  1107. ### and now the real regexes
  1108. #
  1109. regex['email'] = re.compile(patt_email,re.I)
  1110. # email | url
  1111. regex['link'] = \
  1112. re.compile(r'%s|%s'%(retxt_url,patt_email), re.I)
  1113. # \[ label | imagetag url | email | filename \]
  1114. regex['linkmark'] = \
  1115. re.compile(r'\[(?P<label>%s|[^]]+) (?P<link>%s|%s|%s)\]'%(
  1116. patt_img, retxt_url, patt_email, retxt_url_local),
  1117. re.L+re.I)
  1118. # image
  1119. regex['img'] = re.compile(patt_img, re.L+re.I)
  1120. # all macros
  1121. regex['macro'] = regex['date']
  1122. # special things
  1123. regex['special'] = re.compile(r'^%!\s*')
  1124. regex['command'] = re.compile(r'(Include)\s*:\s*(.+)\s*$',re.I)
  1125. return regex
  1126. ### END OF regex nightmares
  1127. class SubareaMaster:
  1128. def __init__(self) : self.x = []
  1129. def __call__(self) :
  1130. if not self.x: return ''
  1131. return self.x[-1]
  1132. def add(self, area):
  1133. if not self.x or (self.x and self.x[-1] != area):
  1134. self.x.append(area)
  1135. Debug('subarea ++ (%s): %s' % (area,self.x), 1)
  1136. def pop(self, area=None):
  1137. if area and self.x[-1] == area: self.x.pop()
  1138. Debug('subarea -- (%s): %s' % (area,self.x), 1)
  1139. def doHeader(headers, CONF):
  1140. if CONF['noheaders']: return []
  1141. doctype = CONF['type']
  1142. if not HEADER_TEMPLATE.has_key(doctype):
  1143. Error("doheader: Unknow doctype '%s'"%doctype)
  1144. template = string.split(HEADER_TEMPLATE[doctype], '\n')
  1145. head_data = {'STYLE':'', 'ENCODING':''}
  1146. for key in head_data.keys():
  1147. val = CONF.get(string.lower(key))
  1148. if key == 'ENCODING': val = get_encoding_string(val, doctype)
  1149. head_data[key] = val
  1150. # parse header contents
  1151. for i in 0,1,2:
  1152. contents = doDateMacro(headers[i]) # expand %%date
  1153. # Escapes - on tex, just do it if any \tag{} present
  1154. if doctype != 'tex' or \
  1155. (doctype == 'tex' and re.search(r'\\\w+{', contents)):
  1156. contents = doEscape(doctype, contents)
  1157. head_data['HEADER%d'%(i+1)] = contents
  1158. Debug("Header Data: %s"%head_data, 1)
  1159. # scan for empty dictionary keys
  1160. # if found, scan template lines for that key reference
  1161. # if found, remove the reference
  1162. # if there isn't any other key reference on the same line, remove it
  1163. for key in head_data.keys():
  1164. if head_data.get(key): continue
  1165. for line in template:
  1166. if string.count(line, '%%(%s)s'%key):
  1167. sline = string.replace(line, '%%(%s)s'%key, '')
  1168. if not re.search(r'%\([A-Z0-9]+\)s', sline):
  1169. template.remove(line)
  1170. # populate template with data
  1171. template = string.join(template, '\n') % head_data
  1172. ### post processing
  1173. #
  1174. # let tex format today
  1175. if doctype == 'tex' and head_data['HEADER3'] == currdate:
  1176. template = re.sub(r'\\date\{.*?}', r'\date', template)
  1177. return string.split(template, '\n')
  1178. def doDateMacro(line):
  1179. re_date = getRegexes()['date']
  1180. while re_date.search(line):
  1181. m = re_date.search(line)
  1182. fmt = m.group('fmt') or ''
  1183. dateme = currdate
  1184. if fmt: dateme = strftime(fmt,localtime(time()))
  1185. line = re_date.sub(dateme,line,1)
  1186. return line
  1187. def doCommentLine(txt):
  1188. # the -- string ends a sgml/html comment :(
  1189. if string.count(TAGS['comment'], '--') and \
  1190. string.count(txt, '--'):
  1191. txt = re.sub('-(?=-)', r'-\\', txt)
  1192. if TAGS['comment']:
  1193. return regex['x'].sub(txt, TAGS['comment'])
  1194. return ''
  1195. def doFooter(CONF):
  1196. ret = []
  1197. doctype = CONF['type']
  1198. cmdline = CONF['cmdline']
  1199. typename = doctype
  1200. if doctype == 'tex': typename = 'LaTeX2e'
  1201. ppgd = '%s code generated by txt2tags %s (%s)'%(
  1202. typename,my_version,my_url)
  1203. cmdline = 'cmdline: txt2tags %s'%string.join(cmdline[1:], ' ')
  1204. ret.append('\n'+doCommentLine(ppgd))
  1205. ret.append(doCommentLine(cmdline))
  1206. ret.append(TAGS['EOD'])
  1207. return ret
  1208. # TODO mgp: any line (header or not) can't begin with % (add a space before)
  1209. def doEscape(doctype,txt):
  1210. if doctype in ['html','sgml']:
  1211. txt = re.sub('&','&amp;',txt)
  1212. txt = re.sub('<','&lt;',txt)
  1213. txt = re.sub('>','&gt;',txt)
  1214. if doctype == 'sgml':
  1215. txt = re.sub('\xff','&yuml;',txt) # "+y
  1216. elif doctype == 'pm6':
  1217. txt = re.sub('<','<\#60>',txt)
  1218. elif doctype == 'mgp':
  1219. txt = re.sub('^%',' %',txt) # add leading blank to avoid parse
  1220. elif doctype == 'man':
  1221. txt = re.sub("^([.'])", '\\&\\1',txt) # command ID
  1222. txt = string.replace(txt,ESCCHAR, ESCCHAR+'e') # \e
  1223. elif doctype == 'tex':
  1224. txt = string.replace(txt, ESCCHAR, '@@LaTeX-escaping-SUX@@')
  1225. txt = re.sub('([#$&%{}])', r'\\\1', txt)
  1226. txt = string.replace(txt, '~', maskEscapeChar(r'\~{}'))
  1227. txt = string.replace(txt, '^', maskEscapeChar(r'\^{}'))
  1228. txt = string.replace(txt, '@@LaTeX-escaping-SUX@@',
  1229. maskEscapeChar(r'$\backslash$'))
  1230. # TIP the _ is escaped at the end
  1231. return txt
  1232. def doFinalEscape(doctype, txt):
  1233. "Last escapes of each line"
  1234. if doctype == 'pm6' : txt = string.replace(txt,ESCCHAR+'<',r'<\#92><')
  1235. elif doctype == 'man' : txt = string.replace(txt, '-', r'\-')
  1236. elif doctype == 'tex' : txt = string.replace(txt, '_', r'\_')
  1237. elif doctype == 'sgml': txt = string.replace(txt, '[', '&lsqb;')
  1238. return txt
  1239. def EscapeCharHandler(action, data):
  1240. "Mask/Unmask the Escape Char on the given string"
  1241. if not string.strip(data): return data
  1242. if action not in ['mask','unmask']:
  1243. Error("EscapeCharHandler: Invalid action '%s'"%action)
  1244. if action == 'mask': return string.replace(data,'\\',ESCCHAR)
  1245. else: return string.replace(data,ESCCHAR,'\\')
  1246. def maskEscapeChar(data):
  1247. "Replace any Escape Char \ with a text mask (Input: str or list)"
  1248. if type(data) == type([]):
  1249. return map(lambda x: EscapeCharHandler('mask', x), data)
  1250. return EscapeCharHandler('mask',data)
  1251. def unmaskEscapeChar(data):
  1252. "Undo the Escape char \ masking (Input: str or list)"
  1253. if type(data) == type([]):
  1254. return map(lambda x: EscapeCharHandler('unmask', x), data)
  1255. return EscapeCharHandler('unmask',data)
  1256. def addLineBreaks(list):
  1257. "use LB to respect sys.platform"
  1258. ret = []
  1259. for line in list:
  1260. line = string.replace(line,'\n',LB) # embedded \n's
  1261. ret.append(line+LB) # add final line break
  1262. return ret
  1263. def doPreLine(doctype,line):
  1264. "Parsing procedures for preformatted (verbatim) lines"
  1265. if not rules['preareanotescaped']: line = doEscape(doctype,line)
  1266. if rules['indentprearea']: line = ' '+line
  1267. if doctype == 'pm6': line = doFinalEscape(doctype, line)
  1268. return line
  1269. def doCloseTable(doctype):
  1270. global subarea, tableborder
  1271. ret = ''
  1272. if rules['tableable']:
  1273. if doctype == 'tex' and tableborder:
  1274. ret = TAGS['tableLineOpen']+TAGS['tableClose']+'\n'
  1275. else:
  1276. ret = TAGS['tableClose']+'\n'
  1277. else:
  1278. ret = TAGS['areaPreClose']
  1279. tableborder = 0
  1280. subarea.pop('table')
  1281. return ret
  1282. def doCloseQuote(howmany=None):
  1283. global quotedepth
  1284. ret = []
  1285. if not howmany: howmany = len(quotedepth)
  1286. for i in range(howmany):
  1287. quotedepth.pop()
  1288. #TODO align open/close tag -> FREE_ALING_TAG = 1 (man not)
  1289. ret.append(TAGS['areaQuoteClose'])
  1290. if not quotedepth: subarea.pop('quote')
  1291. return string.join(ret,'\n')
  1292. def doCloseList(howmany=None):
  1293. global listindent, listids
  1294. ret = []
  1295. if not howmany: howmany = len(listindent)
  1296. for i in range(howmany):
  1297. if listids[-1] == '-': tag = TAGS['listClose']
  1298. elif listids[-1] == '+': tag = TAGS['numlistClose']
  1299. elif listids[-1] == '=': tag = TAGS['deflistClose']
  1300. if not tag: tag = TAGS['listClose'] # default
  1301. if tag:
  1302. # unnested lists are only closed at mother-list
  1303. if rules['listnotnested']:
  1304. if len(listindent) == 1:
  1305. ret.append(tag)
  1306. else:
  1307. ret.append(listindent[-1]+tag)
  1308. del listindent[-1]
  1309. del listids[-1]
  1310. if not listindent: subarea.pop('list')
  1311. return string.join(ret,'\n')
  1312. def beautify_me(name, line):
  1313. "where name is: bold, italic, underline or bolditalic"
  1314. name = 'font%s' % string.capitalize(name)
  1315. open = TAGS['%sOpen'%name]
  1316. close = TAGS['%sClose'%name]
  1317. txt = r'%s\1%s'%(open, close)
  1318. if name == 'fontItalic':
  1319. txt = r'\1%s\2%s'%(open, close)
  1320. line = regex[name].sub(txt,line)
  1321. return line
  1322. def get_tagged_link(label, url, CONF):
  1323. ret = ''
  1324. doctype = CONF['type']
  1325. # set link type
  1326. if regex['email'].match(url):
  1327. linktype = 'email'
  1328. else:
  1329. linktype = 'url';
  1330. # escape specials from TEXT parts
  1331. label = doEscape(doctype,label)
  1332. # escape specials from link URL
  1333. if rules['linkable'] and rules['escapeurl']:
  1334. url = doEscape(doctype, url)
  1335. # if not linkable, the URL is plain text, that needs escape
  1336. if not rules['linkable']:
  1337. if doctype == 'tex':
  1338. url = re.sub('^#', '\#', url) # ugly, but compile
  1339. else:
  1340. url = doEscape(doctype,url)
  1341. # adding protocol to guessed link
  1342. guessurl = ''
  1343. if linktype == 'url' and \
  1344. re.match(regex['_urlskel']['guess'], url):
  1345. if url[0] == 'w': guessurl = 'http://' +url
  1346. else : guessurl = 'ftp://' +url
  1347. # not link aware targets -> protocol is useless
  1348. if not rules['linkable']: guessurl = ''
  1349. # simple link (not guessed)
  1350. if not label and not guessurl:
  1351. if CONF['maskemail'] and linktype == 'email':
  1352. # do the email mask feature (no TAGs, just text)
  1353. url = string.replace(url,'@',' (a) ')
  1354. url = string.replace(url,'.',' ')
  1355. url = "<%s>" % url
  1356. if rules['linkable']: url = doEscape(doctype, url)
  1357. ret = url
  1358. else:
  1359. # just add link data to tag
  1360. tag = TAGS[linktype]
  1361. ret = regex['x'].sub(url,tag)
  1362. # named link or guessed simple link
  1363. else:
  1364. # adjusts for guessed link
  1365. if not label: label = url # no protocol
  1366. if guessurl : url = guessurl # with protocol
  1367. # change image tag for !supported img+link targets
  1368. if regex['img'].match(label) and not rules['imglinkable']:
  1369. label = "(%s)"%regex['img'].match(label).group(1)
  1370. # putting data on the right appearance order
  1371. if rules['linkable']:
  1372. urlorder = [url, label] # link before label
  1373. else:
  1374. urlorder = [label, url] # label before link
  1375. # add link data to tag (replace \a's)
  1376. ret = TAGS["%sMark"%linktype]
  1377. for data in urlorder:
  1378. ret = regex['x'].sub(data,ret,1)
  1379. return ret
  1380. def get_image_align(line):
  1381. align = ''
  1382. line = string.strip(line)
  1383. m = regex['img'].search(line)
  1384. ini = m.start() ; head = 0
  1385. end = m.end() ; tail = len(line)
  1386. align = 'middle' # default align # ^text +img +text$
  1387. if ini == head and end == tail: align = 'para' # ^img$
  1388. elif ini == head: align = 'left' # ^img + text$
  1389. elif end == tail: align = 'right' # ^text + img$
  1390. return align
  1391. def get_tablecell_align(cells):
  1392. ret = []
  1393. for cell in cells:
  1394. align = 'Left'
  1395. if string.strip(cell):
  1396. if cell[0] == ' ' and cell[-1] == ' ': align = 'Center'
  1397. elif cell[0] == ' ': align = 'Right'
  1398. ret.append(align)
  1399. return ret
  1400. def get_table_prop(line):
  1401. # default table proprierties
  1402. ret = {'border':0,'header':0,'align':'Left','cells':[],'cellalign':[]}
  1403. # detect table align (and remove spaces mark)
  1404. if line[0] == ' ': ret['align'] = 'Center'
  1405. line = string.lstrip(line)
  1406. # detect header (title) mark
  1407. if line[1] == '|':
  1408. ret['header'] = 1
  1409. # delete trailing spaces after last cell border
  1410. line = re.sub('\|\s*$','|', line)
  1411. # detect (and delete) border mark (and leading space)
  1412. if line[-1] == '|':
  1413. ret['border'] = 1 ; line = line[:-2]
  1414. # delete table mark
  1415. line = regex['table'].sub('', line)
  1416. # split cells
  1417. ret['cells'] = string.split(line, ' | ')
  1418. # find cells align
  1419. ret['cellalign'] = get_tablecell_align(ret['cells'])
  1420. Debug('Table Prop: %s' % ret, 1)
  1421. return ret
  1422. def tag_table_cells(table, doctype):
  1423. ret = ''
  1424. open, close = TAGS['tableCellOpen'], TAGS['tableCellClose']
  1425. # title cell
  1426. if table['header']:
  1427. open = TAGS['tableTitleCellOpen']
  1428. close = TAGS['tableTitleCellClose']
  1429. # should we break the line?
  1430. if rules['breaktablecell']: close = close+'\n'
  1431. # here we go
  1432. while table['cells']:
  1433. openalign = open
  1434. cel = table['cells'].pop(0)
  1435. # set each cell align
  1436. if rules['tablecellaligntype'] == 'cell':
  1437. align = table['cellalign'].pop(0)
  1438. align = TAGS['tableCellAlign%s'%align]
  1439. openalign = string.replace(open,'\a',align)
  1440. # show empty cell on HTML
  1441. if not cel and doctype == 'html': cel = '&nbsp;'
  1442. # last cell gotchas
  1443. if not table['cells']:
  1444. # don't need cell separator
  1445. if rules['tablecellsplit']: close = ''
  1446. # close beautifier for last title cell
  1447. if doctype == 'tex' and table['header']: close = '}'
  1448. # join it all
  1449. newcell = openalign + string.strip(cel) + close
  1450. ret = ret + newcell
  1451. return ret
  1452. def get_tableopen_tag(table_prop, doctype):
  1453. global tableborder
  1454. open = TAGS['tableOpen'] # the default one
  1455. # the first line defines if table has border or not
  1456. tableborder = table_prop['border']
  1457. # align full table
  1458. if rules['tablealignable']:
  1459. talign = TAGS['tableAlign'+table_prop['align']]
  1460. open = regex['x'].sub(talign, open, 1)
  1461. # set the columns alignment
  1462. if rules['tablecellaligntype'] == 'column':
  1463. calign = map(lambda x: TAGS['tableColAlign%s'%x],
  1464. table_prop['cellalign'])
  1465. calign = string.join(calign,'')
  1466. open = regex['x'].sub(calign, open, 1)
  1467. # tex table spec, border or not: {|l|c|r|} , {lcr}
  1468. if doctype == 'tex' and not tableborder:
  1469. open = string.replace(open,'|','')
  1470. # we're almost done, just border left
  1471. tag = regex['x'].sub(`tableborder`, open)
  1472. return tag
  1473. # reference: http://www.iana.org/assignments/character-sets
  1474. # http://www.drclue.net/F1.cgi/HTML/META/META.html
  1475. def get_encoding_string(enc, doctype):
  1476. if not enc: return ''
  1477. # target specific translation table
  1478. translate = {
  1479. 'tex': {
  1480. # missing: ansinew , applemac , cp437 , cp437de , cp865
  1481. 'us-ascii' : 'ascii',
  1482. 'windows-1250': 'cp1250',
  1483. 'windows-1252': 'cp1252',
  1484. 'ibm850' : 'cp850',
  1485. 'ibm852' : 'cp852',
  1486. 'iso-8859-1' : 'latin1',
  1487. 'iso-8859-2' : 'latin2',
  1488. 'iso-8859-3' : 'latin3',
  1489. 'iso-8859-4' : 'latin4',
  1490. 'iso-8859-5' : 'latin5',
  1491. 'iso-8859-9' : 'latin9',
  1492. 'koi8-r' : 'koi8-r'
  1493. }
  1494. }
  1495. # normalization
  1496. enc = re.sub('(?i)(us[-_]?)?ascii|us|ibm367','us-ascii' , enc)
  1497. enc = re.sub('(?i)(ibm|cp)?85([02])' ,'ibm85\\2' , enc)
  1498. enc = re.sub('(?i)(iso[_-]?)?8859[_-]?' ,'iso-8859-' , enc)
  1499. enc = re.sub('iso-8859-($|[^1-9]).*' ,'iso-8859-1', enc)
  1500. # apply translation table
  1501. try: enc = translate[doctype][string.lower(enc)]
  1502. except: pass
  1503. return enc
  1504. ################################################################################
  1505. ###MerryChristmas,IdontwanttofighttonightwithyouImissyourbodyandIneedyourlove###
  1506. ################################################################################
  1507. def getAllConf(cmdlinelist, nocheck=0):
  1508. """
  1509. Returns a list of (File Configuration, File Proprierties) tuples
  1510. for all the given Input files. The Configuration is the merge of
  1511. command line options and %!cmdline settings.
  1512. """
  1513. all_confs = []
  1514. # parse command line to get input files list
  1515. cmdline = Cmdline(cmdlinelist, nocheck)
  1516. infiles = cmdline.cmdline_conf.get('infiles')
  1517. if not infiles: return []
  1518. for infile in infiles: # multifile support
  1519. # the first file doesn't need to recall Cmdline()
  1520. if all_confs: cmdline = Cmdline(cmdlinelist, nocheck)
  1521. # extract file Headers and Config
  1522. prop = Proprierties(infile)
  1523. # decide to use generic or target specfic (if any) %!cmdline:
  1524. cmdline_target = cmdline.cmdline_conf.get('type')
  1525. if cmdline_target and cmdline_target in targets and \
  1526. prop.config[cmdline_target].get('cmdline'):
  1527. cfgcmdline_target = cmdline_target
  1528. else:
  1529. cfgcmdline_target = 'all'
  1530. # merge %!cmdline contents (if any) into original cmdline
  1531. cmdline.merge(prop.config[cfgcmdline_target].get('cmdline'))
  1532. # force infile
  1533. cmdline.cmdline_conf['infile'] = infile
  1534. # get all the configuration (flags/options) for this file
  1535. # it saves general AND specific config (not OR as in %!cmdline)
  1536. myconf = cmdline.merge_conf(prop.config['all'])
  1537. myconf = cmdline.merge_conf(prop.config.get(myconf['type']), override=1)
  1538. # adding %!cmdline contents to config (used by GUI)
  1539. myconf['%!cmdline'] = prop.config[cfgcmdline_target].get('cmdline')
  1540. # ensure the configuration has ALL keys defined
  1541. for key in FLAGS.keys() + OPTIONS.keys() + CONFIG_KEYWORDS:
  1542. if not myconf.has_key(key): myconf[key] = ''
  1543. # append the (configuration, proprierties) tuple
  1544. all_confs.append((myconf,prop))
  1545. # remove what has left
  1546. del cmdline, prop
  1547. return all_confs
  1548. def convertAllFiles(confs, gui=0):
  1549. if not confs: Quit(usage, 1)
  1550. header = []
  1551. for myconf,prop in confs: # multifile support
  1552. # compose the target file Headers
  1553. #TODO escape line before?
  1554. #TODO see exceptions by tex and mgp
  1555. header = doHeader(prop.headers, myconf)
  1556. # get the marked file BODY that has left
  1557. body = prop.bodylines
  1558. # parse the full marked body into tagged target
  1559. doc,toc = convert(body, myconf, firstlinenr=prop.arearef[-1])
  1560. # make TOC (if needed)
  1561. toc = toc_maker(toc,myconf)
  1562. # finally, we have our document
  1563. outlist = header + toc + doc
  1564. # break here if Gui - it has some more processing to do
  1565. if gui: return outlist, myconf
  1566. # write results to file or STDOUT
  1567. finish_him(outlist, myconf)
  1568. def reallydoitall(cmdlinelist, gui=0):
  1569. confs = getAllConf(cmdlinelist)
  1570. return convertAllFiles(confs, gui)
  1571. def convert(bodylines, CONF, firstlinenr=1):
  1572. # global vars for doClose*()
  1573. global TAGS, regex, rules, quotedepth, listindent, listids
  1574. global subarea, tableborder
  1575. doctype = CONF['type']
  1576. outfile = CONF['outfile']
  1577. TAGS = getTags(doctype)
  1578. rules = getRules(doctype)
  1579. regex = getRegexes()
  1580. # the defaults
  1581. linkmask = '@@_link_@@'
  1582. monomask = '@@_mono_@@'
  1583. macromask = '@@_macro_@@'
  1584. rawmask = '@@_raw_@@'
  1585. subarea = SubareaMaster()
  1586. ret = []
  1587. toclist = []
  1588. f_tt = 0
  1589. listindent = []
  1590. listids = []
  1591. listcount = []
  1592. titlecount = ['',0,0,0,0,0]
  1593. f_lastwasblank = 0
  1594. holdspace = ''
  1595. listholdspace = ''
  1596. quotedepth = []
  1597. tableborder = 0
  1598. if outfile != STDOUT:
  1599. if not CONF['gui']:
  1600. print "--- %s..."%doctype
  1601. # if TOC is a header tag
  1602. if CONF['toc'] and TAGS['TOC']:
  1603. ret.append(TAGS['TOC']+'\n')
  1604. # let's put the opening paragraph
  1605. if doctype != 'pm6':
  1606. ret.append(TAGS['paragraph'])
  1607. # let's mark it up!
  1608. linenr = firstlinenr-1
  1609. for lineref in range(len(bodylines)):
  1610. skip_continue = 0
  1611. linkbank = []
  1612. monobank = []
  1613. macrobank = []
  1614. rawbank = []
  1615. untouchedline = bodylines[lineref]
  1616. line = re.sub('[\n\r]+$','',untouchedline) # del line break
  1617. # apply PreProc rules
  1618. if CONF['preproc']:
  1619. for targ,patt,repl in CONF['preproc']:
  1620. if targ not in [CONF['type'], 'all']: continue
  1621. line = re.sub(patt, repl, line)
  1622. line = maskEscapeChar(line) # protect \ char
  1623. linenr = linenr +1
  1624. Debug('LINE %04d: %s'%(linenr,repr(line)), 1) # heavy debug
  1625. # we need (not really) to mark each paragraph
  1626. #TODO check if this is really needed
  1627. if doctype == 'pm6' and f_lastwasblank:
  1628. if f_tt or listindent:
  1629. holdspace = ''
  1630. else:
  1631. holdspace = TAGS['paragraph']+'\n'
  1632. # any NOT table line (or comment), closes an open table
  1633. #if subarea() == 'table' and not regex['table'].search(line):
  1634. if subarea() == 'table' \
  1635. and not regex['table'].search(line) \
  1636. and not regex['comment'].search(line):
  1637. ret.append(doCloseTable(doctype))
  1638. #---------------------[ PRE formatted ]----------------------
  1639. #TIP we'll never support beautifiers inside pre-formatted
  1640. # we're already on a PRE area
  1641. if f_tt:
  1642. # closing PRE
  1643. if regex['areaPreClose'].search(line):
  1644. if doctype != 'pm6':
  1645. ret.append(TAGS['areaPreClose'])
  1646. f_tt = 0
  1647. continue
  1648. # normal PRE-inside line
  1649. line = doPreLine(doctype, line)
  1650. ret.append(line)
  1651. continue
  1652. # detecting PRE area init
  1653. if regex['areaPreOpen'].search(line):
  1654. ret.append(TAGS['areaPreOpen'])
  1655. f_lastwasblank = 0
  1656. f_tt = 1
  1657. continue
  1658. # one line PRE-formatted text
  1659. if regex['1linePre'].search(line):
  1660. f_lastwasblank = 0
  1661. line = regex['1linePre'].sub('',line)
  1662. line = doPreLine(doctype, line)
  1663. t1, t2 = TAGS['areaPreOpen'],TAGS['areaPreClose']
  1664. ret.append('%s\n%s\n%s'%(t1,line,t2))
  1665. continue
  1666. #---------------------[ blank lines ]-----------------------
  1667. #TODO "holdspace" to save <p> to not show in closelist
  1668. if regex['blankline'].search(line):
  1669. # closing all open quotes
  1670. if quotedepth:
  1671. ret.append(doCloseQuote())
  1672. # closing all open lists
  1673. if f_lastwasblank: # 2nd consecutive blank line
  1674. if listindent: # closes list (if any)
  1675. ret.append(doCloseList())
  1676. holdspace = ''
  1677. continue # consecutive blanks are trash
  1678. # normal blank line
  1679. if doctype != 'pm6':
  1680. # paragraph (if any) is wanted inside lists also
  1681. if listindent:
  1682. para = TAGS['paragraph'] + '\n'
  1683. holdspace = holdspace + para
  1684. elif doctype == 'html':
  1685. ret.append(TAGS['paragraph'])
  1686. # sgml: quote close tag must not be \n\n</quote>
  1687. elif doctype == 'sgml' and quotedepth:
  1688. skip_continue = 1
  1689. # otherwise we just show a blank line
  1690. else:
  1691. ret.append('')
  1692. f_lastwasblank = 1
  1693. if not skip_continue: continue
  1694. #---------------------[ special ]------------------------
  1695. # duh! nothing has left!
  1696. # if regex['special'].search(line):
  1697. # special = line[2:]
  1698. # m = regex['command'].match(special)
  1699. # if m:
  1700. # name = string.lower(m.group(1))
  1701. # val = m.group(2)
  1702. # Debug("Found config '%s', value '%s'"%(
  1703. # name,val),1,linenr)
  1704. # else:
  1705. # Debug('Bogus Special Line',1,linenr)
  1706. #---------------------[ comments ]-----------------------
  1707. # just skip them (if not macro or config)
  1708. if regex['comment'].search(line) and not \
  1709. regex['date'].match(line):
  1710. continue
  1711. f_lastwasblank = 0 # reset blank status
  1712. #---------------------[ Title ]-----------------------
  1713. #TODO set next blank and set f_lastwasblank or f_lasttitle
  1714. if (regex['title'].search(line) or
  1715. regex['numtitle'].search(line)) and not listindent:
  1716. if string.lstrip(line)[0] == '=':
  1717. titletype = 'title'
  1718. else:
  1719. titletype = 'numtitle'
  1720. m = regex[titletype].search(line)
  1721. level = len(m.group('id'))
  1722. tag = TAGS['title%s'%level]
  1723. txt = string.strip(m.group('txt'))
  1724. ### numbered title
  1725. if CONF['enumtitle'] or titletype == 'numtitle':
  1726. if rules['autonumbertitle']:
  1727. tag = TAGS['numtitle%s'%level] or tag
  1728. idtxt = txt
  1729. else:
  1730. # add count manually
  1731. id = '' ; n = level
  1732. titlecount[n] = titlecount[n] +1
  1733. if n < len(titlecount)-1: # reset sublevels count
  1734. for i in range(n+1, len(titlecount)):
  1735. titlecount[i] = 0
  1736. for i in range(n): # compose id from hierarchy
  1737. id = "%s%d."%(id,titlecount[i+1])
  1738. idtxt = "%s %s"%(id, txt) # add id to title
  1739. else:
  1740. idtxt = txt
  1741. anchorid = 'toc%d'%(len(toclist)+1)
  1742. if TAGS['anchor'] and CONF['toc'] \
  1743. and level <= CONF['toclevel']:
  1744. ret.append(regex['x'].sub(anchorid,TAGS['anchor']))
  1745. # place title tag overriding line
  1746. line = regex[titletype].sub(tag,line)
  1747. ### escape title text (unescaped text is used for TOC)
  1748. #
  1749. esctxt = doEscape(doctype,idtxt)
  1750. # sgml: [ is special on title (and lists) - here bcos 'continue'
  1751. if doctype in ['sgml','tex']:
  1752. esctxt = doFinalEscape(doctype, esctxt)
  1753. # txt: blank before
  1754. if doctype == 'txt': ret.append('')
  1755. # finish title line
  1756. ret.append(regex['x'].sub(esctxt,line))
  1757. # add "underline" to text titles
  1758. if doctype == 'txt':
  1759. ret.append(regex['x'].sub('='*len(idtxt),tag))
  1760. ret.append('') # blank line after
  1761. # let's do some TOC!
  1762. if not CONF['toc'] and not CONF['toconly']: continue
  1763. if level > CONF['toclevel']: continue # max level
  1764. if TAGS['TOC']: continue # TOC is a tag
  1765. if TAGS['anchor']:
  1766. # tocitemid = '#toc%d'%(len(toclist)+1)
  1767. # TOC more readable with master topics not
  1768. # linked at number stoled idea from windows .CHM
  1769. # files (help system)
  1770. if CONF['enumtitle'] and level == 1:
  1771. tocitem = '%s+ [``%s`` #%s]'%(' '*level,txt,anchorid)
  1772. else:
  1773. tocitem = '%s- [``%s`` #%s]'%(' '*level,idtxt,anchorid)
  1774. else:
  1775. tocitem = '%s- ``%s``'%(' '*level,idtxt)
  1776. if doctype in ['txt', 'man']:
  1777. tocitem = '%s``%s``' %(' '*level,idtxt)
  1778. toclist.append(tocitem)
  1779. continue
  1780. #TODO! labeltxt = ''
  1781. # label = m.group('label')
  1782. # if label: labeltxt = '<label id="%s">' %label
  1783. #---------------------[ apply masks ]-----------------------
  1784. ### protect important structures from escaping and formatting
  1785. while regex['raw'].search(line):
  1786. txt = regex['raw'].search(line).group(1)
  1787. txt = doEscape(doctype,txt)
  1788. rawbank.append(txt)
  1789. line = regex['raw'].sub(rawmask,line,1)
  1790. # protect pre-formatted font text
  1791. while regex['fontMono'].search(line):
  1792. txt = regex['fontMono'].search(line).group(1)
  1793. txt = doEscape(doctype,txt)
  1794. monobank.append(txt)
  1795. line = regex['fontMono'].sub(monomask,line,1)
  1796. # protect macros
  1797. while regex['macro'].search(line):
  1798. txt = regex['macro'].search(line).group()
  1799. macrobank.append(txt)
  1800. line = regex['macro'].sub(macromask,line,1)
  1801. # protect URLs and emails
  1802. while regex['linkmark'].search(line) or regex['link'].search(line):
  1803. # try to match plain or named links
  1804. match_link = regex['link'].search(line)
  1805. match_named = regex['linkmark'].search(line)
  1806. # define the current match
  1807. if match_link and match_named:
  1808. # both types found, which is the first?
  1809. m = match_link
  1810. if match_named.start() < match_link.start():
  1811. m = match_named
  1812. else:
  1813. # just one type found, we're fine
  1814. m = match_link or match_named
  1815. # extract link data and apply mask
  1816. if m == match_link: # plain link
  1817. label = ''
  1818. link = m.group()
  1819. line = regex['link'].sub(linkmask,line,1)
  1820. else: # named link
  1821. label = string.rstrip(m.group('label'))
  1822. link = m.group('link')
  1823. line = regex['linkmark'].sub(linkmask,line,1)
  1824. # save link data to the link bank
  1825. linkbank.append((label, link))
  1826. #---------------------[ do Escapes ]-----------------------
  1827. # the target-specific special char escapes for body lines
  1828. line = doEscape(doctype,line)
  1829. #---------------------[ Horizontal Bar ]--------------------
  1830. if regex['bar'].search(line):
  1831. txt = regex['bar'].search(line).group(1)
  1832. if txt[0] == '=': bar = TAGS['bar2']
  1833. else : bar = TAGS['bar1']
  1834. # to avoid comment tag confusion
  1835. if doctype == 'sgml':
  1836. txt = string.replace(txt,'--','__')
  1837. line = regex['bar'].sub(bar,line)
  1838. ret.append(regex['x'].sub(txt,line))
  1839. continue
  1840. #---------------------[ Quote ]-----------------------
  1841. if regex['quote'].search(line):
  1842. subarea.add('quote')
  1843. # store number of leading TABS
  1844. currquotedepth = len(regex['quote'].search(line).group(0))
  1845. # SGML doesn't support nested quotes
  1846. if rules['quotenotnested']:
  1847. if quotedepth and currquotedepth > quotedepth[-1]:
  1848. currquotedepth = quotedepth[-1]
  1849. # for don't-close-me quote tags
  1850. if not TAGS['areaQuoteClose']:
  1851. line = regex['quote'].sub(TAGS['areaQuoteOpen']*currquotedepth, line)
  1852. else:
  1853. # new (sub)quote
  1854. if not quotedepth or currquotedepth > quotedepth[-1]:
  1855. quotedepth.append(currquotedepth)
  1856. ret.append(TAGS['areaQuoteOpen'])
  1857. # remove leading TABs
  1858. if not rules['keepquoteindent']:
  1859. line = regex['quote'].sub('', line)
  1860. # closing quotes
  1861. while currquotedepth < quotedepth[-1]:
  1862. ret.append(doCloseQuote(1))
  1863. else:
  1864. # closing all quotes (not quote line)
  1865. if quotedepth: ret.append(doCloseQuote())
  1866. #---------------------[ Lists ]-----------------------
  1867. if (regex['list'].search(line) or regex['deflist'].search(line)):
  1868. subarea.add('list')
  1869. if regex['list'].search(line): rgx = regex['list']
  1870. else: rgx = regex['deflist']
  1871. m = rgx.search(line)
  1872. listitemindent = m.group(1)
  1873. listtype = m.group(2)
  1874. extra = m.group(3) # regex anchor char
  1875. if listtype == '=':
  1876. listdefterm = m.group(3)
  1877. extra = ''
  1878. if doctype == 'tex':
  1879. # on tex, brackets are term delimiters
  1880. # TODO escape ] at list definition
  1881. # \], \rbrack{} and \verb!]! don't work :(
  1882. #listdefterm = string.replace(listdefterm, ']', '???')
  1883. pass
  1884. if not rules['imgasdefterm'] and \
  1885. regex['img'].search(listdefterm):
  1886. while regex['img'].search(listdefterm):
  1887. img = regex['img'].search(listdefterm).group(1)
  1888. masked = '(%s)'%img
  1889. listdefterm = regex['img'].sub(masked,listdefterm,1)
  1890. # don't cross depth limit
  1891. maxdepth = rules['listmaxdepth']
  1892. if maxdepth and len(listindent) == maxdepth:
  1893. if len(listitemindent) > len(listindent[-1]):
  1894. listitemindent = listindent[-1]
  1895. # new sublist
  1896. if not listindent or len(listitemindent) > len(listindent[-1]):
  1897. listindent.append(listitemindent)
  1898. listids.append(listtype)
  1899. if listids[-1] == '-': tag = TAGS['listOpen']
  1900. elif listids[-1] == '+': tag = TAGS['numlistOpen']
  1901. elif listids[-1] == '=': tag = TAGS['deflistOpen']
  1902. if not tag: tag = TAGS['listOpen'] # default
  1903. # no need to reopen <pre> tag on man sublists
  1904. if rules['listnotnested'] and len(listindent) != 1:
  1905. tag = ''
  1906. openlist = listindent[-1]+tag
  1907. if doctype == 'pm6':
  1908. listholdspace = openlist
  1909. else:
  1910. if string.strip(openlist): ret.append(openlist)
  1911. # reset item manual count
  1912. listcount.append(0)
  1913. # closing sublists
  1914. while len(listitemindent) < len(listindent[-1]):
  1915. close = doCloseList(1)
  1916. if close: ret.append(close)
  1917. if listcount: del listcount[-1]
  1918. # normal item
  1919. listid = listindent[-1]
  1920. if listids[-1] == '-':
  1921. tag = TAGS['listItem']
  1922. elif listids[-1] == '+':
  1923. tag = TAGS['numlistItem']
  1924. listcount[-1] = listcount[-1] +1
  1925. if not rules['autonumberlist']:
  1926. tag = regex['x'].sub(str(listcount[-1]), tag)
  1927. elif listids[-1] == '=':
  1928. if not TAGS['deflistItem1']:
  1929. # emulate def list, with <li><b>def</b>:
  1930. tag = TAGS['listItem'] +TAGS['fontBoldOpen'] +listdefterm
  1931. tag = tag +TAGS['fontBoldClose'] +':'
  1932. else:
  1933. tag = regex['x'].sub(listdefterm, TAGS['deflistItem1'])
  1934. tag = tag + TAGS['deflistItem2'] # open <DD>
  1935. if doctype == 'mgp': listid = len(listindent)*'\t'
  1936. line = rgx.sub(listid+tag+extra,line)
  1937. if listholdspace:
  1938. line = listholdspace+line
  1939. listholdspace = ''
  1940. #---------------------[ Table ]-----------------------
  1941. #TODO escape undesired format inside table
  1942. #TODO add man, pm6 targets
  1943. if regex['table'].search(line):
  1944. table = get_table_prop(line)
  1945. if subarea() != 'table':
  1946. subarea.add('table') # first table line!
  1947. if rules['tableable']: # table-aware target
  1948. ret.append(get_tableopen_tag(table,doctype))
  1949. else: # if not, use verb
  1950. ret.append(TAGS['areaPreOpen'])
  1951. if rules['tableable']:
  1952. # setting line tags
  1953. tl1 = TAGS['tableLineOpen']
  1954. tl2 = TAGS['tableLineClose']
  1955. # little table gotchas
  1956. if rules['breaktablelineopen']:
  1957. tl1 = tl1+'\n'
  1958. if doctype == 'tex' and not tableborder:
  1959. tl1 = ''
  1960. # do cells and finish
  1961. cells = tag_table_cells(table, doctype)
  1962. line = tl1 + cells + tl2
  1963. ### BEGIN of at-any-part-of-the-line/various-per-line TAGs.
  1964. for beauti in ['Bold', 'Italic', 'Bolditalic', 'Underline']:
  1965. if regex['font%s'%beauti].search(line):
  1966. line = beautify_me(beauti, line)
  1967. #---------------------[ URL & E-mail ]-----------------------
  1968. for label,url in linkbank:
  1969. link = get_tagged_link(label, url, CONF)
  1970. line = string.replace(line, linkmask, link, 1)
  1971. #---------------------[ Image ]-----------------------
  1972. #TODO fix smart align when image is a link label
  1973. while regex['img'].search(line) and TAGS['img'] != '[\a]':
  1974. txt = regex['img'].search(line).group(1)
  1975. tag = TAGS['img']
  1976. # HTML is the only align-aware target for now
  1977. if rules['imgalignable']:
  1978. align = get_image_align(line)
  1979. if align == 'para':
  1980. align = 'center'
  1981. tag= regex['x'].sub(tag,TAGS['imgsolo'])
  1982. # add align on tag
  1983. tag = regex['x'].sub(align, tag, 1)
  1984. if doctype == 'tex': tag = re.sub(r'\\b',r'\\\\b',tag)
  1985. line = regex['img'].sub(tag,line,1)
  1986. line = regex['x'].sub(txt,line,1)
  1987. #---------------------[ Expand Macros ]-----------------------
  1988. if macrobank:
  1989. for macro in macrobank:
  1990. line = string.replace(line, macromask, macro,1)
  1991. # now the line is full of macros again
  1992. line = doDateMacro(line)
  1993. #---------------------[ Expand PREs ]-----------------------
  1994. for mono in monobank:
  1995. open,close = TAGS['fontMonoOpen'],TAGS['fontMonoClose']
  1996. tagged = open+mono+close
  1997. line = string.replace(line,monomask,tagged,1)
  1998. #---------------------[ Expand raw ]-----------------------
  1999. for raw in rawbank:
  2000. line = string.replace(line,rawmask,raw,1)
  2001. #---------------------[ Final Escapes ]-----------------------
  2002. line = doFinalEscape(doctype, line)
  2003. ret.append(holdspace+line)
  2004. holdspace = ''
  2005. # EOF: close any open lists/tables/quotes
  2006. #TODO take table exception out when self.doctype
  2007. while subarea():
  2008. func = eval("doClose%s" % string.capitalize(subarea()))
  2009. parm = None
  2010. if subarea() == 'table': parm = doctype
  2011. txt = func(parm)
  2012. if txt: ret.append(txt)
  2013. # add footer
  2014. if not CONF['noheaders']:
  2015. ret.extend(doFooter(CONF))
  2016. if CONF['toconly']: ret = []
  2017. return ret, toclist
  2018. ################################################################################
  2019. ##################################### GUI ######################################
  2020. ################################################################################
  2021. # tk help: http://python.org/topics/tkinter/
  2022. class Gui:
  2023. "Graphical Tk Interface"
  2024. def __init__(self, conf={}):
  2025. self.bg = 'orange'
  2026. self.root = Tkinter.Tk()
  2027. self.root.config(bd=15,bg=self.bg)
  2028. self.root.title("txt2tags")
  2029. self.frame1 = Tkinter.Frame(self.root,bg=self.bg)
  2030. self.frame1.pack(fill='x')
  2031. self.frame2 = Tkinter.Frame(self.root,bg=self.bg)
  2032. self.frame2.pack()
  2033. self.frame3 = Tkinter.Frame(self.root,bg=self.bg)
  2034. self.frame3.pack(fill='x')
  2035. self.frame = self.root
  2036. self.conf = conf
  2037. self.infile = self.setvar('')
  2038. #self.infile = self.setvar('C:/aurelio/a.txt')
  2039. self.doctype = self.setvar('')
  2040. self.checks = ['noheaders','enumtitle','toc','toconly','stdout']
  2041. # creating variables
  2042. for check in self.checks:
  2043. setattr(self, 'f_'+check, self.setvar(''))
  2044. ### config as dic for python 1.5 compat (**opts don't work :( )
  2045. def entry(self, **opts): return Tkinter.Entry(self.frame, opts)
  2046. def label(self, txt='', **opts):
  2047. opts.update({'text':txt,'bg':self.bg})
  2048. return Tkinter.Label(self.frame, opts)
  2049. def button(self,name,cmd,**opts):
  2050. opts.update({'text':name,'command':cmd})
  2051. return Tkinter.Button(self.frame, opts)
  2052. def check(self,name,val,checked=0,**opts):
  2053. opts.update( {'text':name, 'onvalue':val, 'offvalue':'',
  2054. 'anchor':'w', 'bg':self.bg, 'activebackground':self.bg} )
  2055. chk = Tkinter.Checkbutton(self.frame, opts)
  2056. if checked: chk.select()
  2057. chk.pack(fill='x',padx=10)
  2058. def exit(self): self.root.destroy(); sys.exit()
  2059. def setvar(self, val): z = Tkinter.StringVar() ; z.set(val) ; return z
  2060. def menu(self,sel,items):
  2061. return apply(Tkinter.OptionMenu,(self.frame,sel)+tuple(items))
  2062. def askfile(self):
  2063. ftypes= [("txt2tags files",("*.t2t","*.txt")),("All files","*")]
  2064. newfile = askopenfilename(filetypes=ftypes)
  2065. if newfile:
  2066. self.infile.set(newfile)
  2067. newconf = getAllConf(['foo',newfile], nocheck=1)
  2068. if newconf: newconf = newconf[0][0]
  2069. # restate all checkboxes after file selection
  2070. # TODO how to make a refresh without killing it?
  2071. self.root.destroy()
  2072. self.__init__(newconf)
  2073. self.mainwindow()
  2074. def scrollwindow(self,txt='no text!',title=''):
  2075. win = Tkinter.Toplevel() ; win.title(title)
  2076. scroll = Tkinter.Scrollbar(win)
  2077. text = Tkinter.Text(win,yscrollcommand=scroll.set)
  2078. scroll.config(command=text.yview)
  2079. text.insert(Tkinter.END, string.join(txt,'\n'))
  2080. text.pack(side='left',fill='both')
  2081. scroll.pack(side='right',fill='y')
  2082. def runprogram(self):
  2083. # prepare
  2084. infile, doctype = self.infile.get(), self.doctype.get()
  2085. if not infile:
  2086. showwarning('txt2tags',\
  2087. "You must provide the source file location!")
  2088. return
  2089. # compose cmdline
  2090. guiflags = []
  2091. for flag in self.checks:
  2092. flag = getattr(self, 'f_%s'%flag).get()
  2093. if flag: guiflags.append(flag)
  2094. cmdline = ['txt2tags', '-t', doctype] +guiflags +[infile]
  2095. Debug('Gui/Tk cmdline: %s'%cmdline,1)
  2096. # run!
  2097. try:
  2098. outlist, CONF = reallydoitall(cmdline, gui=1)
  2099. outfile = CONF['outfile']
  2100. infile = CONF['infile']
  2101. if outfile == STDOUT:
  2102. outlist = unmaskEscapeChar(outlist)
  2103. title = 'txt2tags: %s converted to %s'%(
  2104. os.path.basename(infile),
  2105. string.upper(CONF['type']))
  2106. self.scrollwindow(outlist, title)
  2107. else:
  2108. finish_him(outlist,CONF)
  2109. msg = "Conversion done!\n\n" +\
  2110. "FROM:\n\t%s\n"%infile +\
  2111. "TO:\n\t%s"%outfile
  2112. showinfo('txt2tags', msg)
  2113. except ZeroDivisionError: # common error, not quit
  2114. pass
  2115. except: # fatal error
  2116. traceback.print_exc()
  2117. print '\nSorry! txt2tags-Tk Fatal Error.'
  2118. errmsg = 'Unknown error occurred.\n\n'+\
  2119. 'Please send the Error Traceback '+\
  2120. 'dumped to the author:\n %s'%my_email
  2121. showerror('txt2tags FATAL ERROR!',errmsg)
  2122. self.exit()
  2123. def mainwindow(self):
  2124. #TODO show outfile somewhere
  2125. #TODO redraw GUI only using grid() because pack() sux
  2126. self.infile.set(self.conf.get('infile') or '')
  2127. self.doctype.set(self.conf.get('type') or 'html')
  2128. if self.conf.get('outfile') == STDOUT: # map -o-
  2129. self.conf['stdout'] = 1
  2130. action1 = " \nChoose the target document type:"
  2131. action2 = "\n\nEnter the tagged source file location:"
  2132. action3 = "\n\nSome options you may check:"
  2133. checks_txt = {
  2134. 'noheaders': "Suppress headers from output",
  2135. 'enumtitle': "Number titles (1, 1.1, 1.1.1, etc)",
  2136. 'toc' : "Do TOC also (Table of Contents)",
  2137. 'toconly' : "Just do TOC, nothing more",
  2138. 'stdout' : "Dump to screen (Don't save target file)"
  2139. }
  2140. self.frame = self.frame1
  2141. self.label("TXT2TAGS\n%s\nv%s"%(my_url,my_version)).pack()
  2142. self.label(action1, anchor='w').pack(fill='x')
  2143. self.menu(self.doctype, targets).pack()
  2144. self.label(action2, anchor='w').pack(fill='x')
  2145. self.frame = self.frame2
  2146. self.entry(textvariable=self.infile).grid(row=0, column=0)
  2147. self.button("Browse", self.askfile
  2148. ).grid(row=0, column=1, padx=10)
  2149. if self.conf.get('%!cmdline'):
  2150. txt = '%%!cmdline: %s' % self.conf['%!cmdline']
  2151. self.label(txt,fg='brown'
  2152. ).grid(row=1, column=0, columnspan=2, sticky='w')
  2153. self.frame = self.frame3
  2154. self.label(action3, anchor='w').pack(fill='x')
  2155. # compose options check boxes, example:
  2156. # self.check(checks_txt['toc'], '--toc', 1, variable=self.f_toc)
  2157. for check in self.checks:
  2158. txt = checks_txt[check]
  2159. opt = '--'+check
  2160. var = getattr(self, 'f_'+check)
  2161. onoff = self.conf.get(check)
  2162. self.check(txt,opt,onoff,variable=var)
  2163. self.label('\n').pack()
  2164. self.button("Quit", self.exit).pack(side='left',padx=40)
  2165. self.button("Convert!", self.runprogram
  2166. ).pack(side='right',padx=40)
  2167. # as documentation told me
  2168. if sys.platform[:3] == 'win':
  2169. self.root.iconify()
  2170. self.root.update()
  2171. self.root.deiconify()
  2172. self.root.mainloop()
  2173. ################################################################################
  2174. ################################################################################
  2175. if __name__ == '__main__':
  2176. # set debug and remove option from cmdline
  2177. if sys.argv.count('--debug'):
  2178. DEBUG = 1
  2179. sys.argv.remove('--debug')
  2180. ### check if we will enter on GUI mode
  2181. CONF['gui'] = 0
  2182. # GUI is default on this platforms, when called alone
  2183. if len(sys.argv) == 1 and sys.platform[:3] in ['mac','cyg','win']:
  2184. CONF['gui'] = 1
  2185. # user specified GUI mode
  2186. if sys.argv.count('--gui'): CONF['gui'] = 1
  2187. # check for GUI mode ressorces
  2188. if CONF['gui'] == 1:
  2189. try:
  2190. from tkFileDialog import askopenfilename
  2191. from tkMessageBox import showinfo,showwarning,showerror
  2192. import Tkinter
  2193. except:
  2194. # if GUI was forced, show the error message
  2195. if len(sys.argv) > 1 and sys.argv[1] == '--gui':
  2196. traceback.print_exc()
  2197. sys.exit()
  2198. # or just abandon GUI mode, and continue
  2199. else:
  2200. CONF['gui'] = 0
  2201. Debug("system platform: %s"%sys.platform,1)
  2202. Debug("line break char: %s"%repr(LB),1)
  2203. nocheck = CONF['gui'] # if GUI, no cmdline checking
  2204. CONFS = getAllConf(sys.argv, nocheck) # get all infiles config (if any)
  2205. if CONF['gui'] == 1:
  2206. if len(CONFS) > 1:
  2207. Error("GUI doesn't support multiple Input files.")
  2208. # remove proprierties, get just config
  2209. if CONFS: conf = CONFS[0][0]
  2210. else : conf = {}
  2211. # redefine Error function to raise exception instead sys.exit()
  2212. def Error(msg):
  2213. showerror('txt2tags ERROR!', msg)
  2214. raise ZeroDivisionError
  2215. Gui(conf).mainwindow()
  2216. else:
  2217. # console mode rocks forever!
  2218. convertAllFiles(CONFS)
  2219. sys.exit(0)
  2220. # vim: ts=4