/asciidoc-8.4.5/asciidoc.py

# · Python · 5315 lines · 4939 code · 65 blank · 311 comment · 538 complexity · 3d8c49106162df3e11f65d3c3dd23451 MD5 · raw file

  1. #!/usr/bin/env python
  2. """
  3. asciidoc - converts an AsciiDoc text file to DocBook, HTML or LinuxDoc
  4. Copyright (C) 2002-2009 Stuart Rackham. Free use of this software is granted
  5. under the terms of the GNU General Public License (GPL).
  6. """
  7. import sys, os, re, time, traceback, tempfile, subprocess, codecs, locale
  8. ### Used by asciidocapi.py ###
  9. VERSION = '8.4.5' # See CHANGLOG file for version history.
  10. MIN_PYTHON_VERSION = 2.4 # Require this version of Python or better.
  11. #---------------------------------------------------------------------------
  12. # Program constants.
  13. #---------------------------------------------------------------------------
  14. DEFAULT_BACKEND = 'xhtml11'
  15. DEFAULT_DOCTYPE = 'article'
  16. # Allowed substitution options for List, Paragraph and DelimitedBlock
  17. # definition subs entry.
  18. SUBS_OPTIONS = ('specialcharacters','quotes','specialwords',
  19. 'replacements', 'attributes','macros','callouts','normal','verbatim',
  20. 'none','replacements2')
  21. # Default value for unspecified subs and presubs configuration file entries.
  22. SUBS_NORMAL = ('specialcharacters','quotes','attributes',
  23. 'specialwords','replacements','macros','replacements2')
  24. SUBS_VERBATIM = ('specialcharacters','callouts')
  25. NAME_RE = r'(?u)[^\W\d][-\w]*' # Valid section or attrbibute name.
  26. #---------------------------------------------------------------------------
  27. # Utility functions and classes.
  28. #---------------------------------------------------------------------------
  29. class EAsciiDoc(Exception): pass
  30. class OrderedDict(dict):
  31. """
  32. Dictionary ordered by insertion order.
  33. Python Cookbook: Ordered Dictionary, Submitter: David Benjamin.
  34. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747
  35. """
  36. def __init__(self, d=None, **kwargs):
  37. self._keys = []
  38. if d is None: d = kwargs
  39. dict.__init__(self, d)
  40. def __delitem__(self, key):
  41. dict.__delitem__(self, key)
  42. self._keys.remove(key)
  43. def __setitem__(self, key, item):
  44. dict.__setitem__(self, key, item)
  45. if key not in self._keys: self._keys.append(key)
  46. def clear(self):
  47. dict.clear(self)
  48. self._keys = []
  49. def copy(self):
  50. d = dict.copy(self)
  51. d._keys = self._keys[:]
  52. return d
  53. def items(self):
  54. return zip(self._keys, self.values())
  55. def keys(self):
  56. return self._keys
  57. def popitem(self):
  58. try:
  59. key = self._keys[-1]
  60. except IndexError:
  61. raise KeyError('dictionary is empty')
  62. val = self[key]
  63. del self[key]
  64. return (key, val)
  65. def setdefault(self, key, failobj = None):
  66. dict.setdefault(self, key, failobj)
  67. if key not in self._keys: self._keys.append(key)
  68. def update(self, d=None, **kwargs):
  69. if d is None:
  70. d = kwargs
  71. dict.update(self, d)
  72. for key in d.keys():
  73. if key not in self._keys: self._keys.append(key)
  74. def values(self):
  75. return map(self.get, self._keys)
  76. class AttrDict(dict):
  77. """
  78. Like a dictionary except values can be accessed as attributes i.e. obj.foo
  79. can be used in addition to obj['foo'].
  80. If an item is not present None is returned.
  81. """
  82. def __getattr__(self, key):
  83. try: return self[key]
  84. except KeyError, k: return None
  85. def __setattr__(self, key, value):
  86. self[key] = value
  87. def __delattr__(self, key):
  88. try: del self[key]
  89. except KeyError, k: raise AttributeError, k
  90. def __repr__(self):
  91. return '<AttrDict ' + dict.__repr__(self) + '>'
  92. def __getstate__(self):
  93. return dict(self)
  94. def __setstate__(self,value):
  95. for k,v in value.items(): self[k]=v
  96. class Trace(object):
  97. """
  98. Used in conjunction with the 'trace' attribute to generate diagnostic
  99. output. There is a single global instance of this class named trace.
  100. """
  101. SUBS_NAMES = ('specialcharacters','quotes','specialwords',
  102. 'replacements', 'attributes','macros','callouts',
  103. 'replacements2')
  104. def __init__(self):
  105. self.name_re = '' # Regexp pattern to match trace names.
  106. self.linenos = True
  107. self.offset = 0
  108. def __call__(self, name, before, after=None):
  109. """
  110. Print trace message if tracing is on and the trace 'name' matches the
  111. document 'trace' attribute (treated as a regexp).
  112. The 'before' and 'after' messages are only printed if they differ.
  113. """
  114. name_re = document.attributes.get('trace')
  115. if name_re == 'subs': # Alias for all the inline substitutions.
  116. name_re = '|'.join(self.SUBS_NAMES)
  117. self.name_re = name_re
  118. if self.name_re is not None:
  119. msg = message.format(name, 'TRACE: ', self.linenos, offset=self.offset)
  120. if before != after and re.match(self.name_re,name):
  121. if is_array(before):
  122. before = '\n'.join(before)
  123. if after is None:
  124. msg += '\n%s\n' % before
  125. else:
  126. if is_array(after):
  127. after = '\n'.join(after)
  128. msg += '\n<<<\n%s\n>>>\n%s\n' % (before,after)
  129. message.stderr(msg)
  130. class Message:
  131. """
  132. Message functions.
  133. """
  134. def __init__(self):
  135. self.linenos = None # Used to globally override line numbers.
  136. self.messages = []
  137. def stderr(self,line=''):
  138. self.messages.append(line)
  139. if __name__ == '__main__':
  140. sys.stderr.write(line+os.linesep)
  141. def verbose(self, msg,linenos=True):
  142. if config.verbose:
  143. msg = self.format(msg,linenos=linenos)
  144. self.stderr(msg)
  145. def warning(self, msg,linenos=True,offset=0):
  146. msg = self.format(msg,'WARNING: ',linenos,offset=offset)
  147. document.has_warnings = True
  148. self.stderr(msg)
  149. def deprecated(self, msg, linenos=True):
  150. msg = self.format(msg, 'DEPRECATED: ', linenos)
  151. self.stderr(msg)
  152. def format(self, msg, prefix='', linenos=True, cursor=None, offset=0):
  153. """Return formatted message string."""
  154. if self.linenos is not False and ((linenos or self.linenos) and reader.cursor):
  155. if cursor is None:
  156. cursor = reader.cursor
  157. prefix += '%s: line %d: ' % (os.path.basename(cursor[0]),cursor[1]+offset)
  158. return prefix + msg
  159. def error(self, msg, cursor=None, halt=False):
  160. """
  161. Report fatal error.
  162. If halt=True raise EAsciiDoc exception.
  163. If halt=False don't exit application, continue in the hope of reporting
  164. all fatal errors finishing with a non-zero exit code.
  165. """
  166. if halt:
  167. raise EAsciiDoc, self.format(msg,linenos=False,cursor=cursor)
  168. else:
  169. msg = self.format(msg,'ERROR: ',cursor=cursor)
  170. self.stderr(msg)
  171. document.has_errors = True
  172. def unsafe(self, msg):
  173. self.error('unsafe: '+msg)
  174. def file_in(fname, directory):
  175. """Return True if file fname resides inside directory."""
  176. assert os.path.isfile(fname)
  177. # Empty directory (not to be confused with None) is the current directory.
  178. if directory == '':
  179. directory = os.getcwd()
  180. else:
  181. assert os.path.isdir(directory)
  182. directory = os.path.realpath(directory)
  183. fname = os.path.realpath(fname)
  184. return os.path.commonprefix((directory, fname)) == directory
  185. def safe():
  186. return document.safe
  187. def is_safe_file(fname, directory=None):
  188. # A safe file must reside in directory directory (defaults to the source
  189. # file directory).
  190. if directory is None:
  191. if document.infile == '<stdin>':
  192. return not safe()
  193. directory = os.path.dirname(document.infile)
  194. elif directory == '':
  195. directory = '.'
  196. return not safe() or file_in(fname, directory)
  197. def safe_filename(fname, parentdir):
  198. """
  199. Return file name which must reside in the parent file directory.
  200. Return None if file is not found or not safe.
  201. """
  202. if not os.path.isabs(fname):
  203. # Include files are relative to parent document
  204. # directory.
  205. fname = os.path.join(parentdir,fname)
  206. if not os.path.isfile(fname):
  207. message.warning('include file not found: %s' % fname)
  208. return None
  209. if not is_safe_file(fname, parentdir):
  210. message.unsafe('include file: %s' % fname)
  211. return None
  212. return fname
  213. def assign(dst,src):
  214. """Assign all attributes from 'src' object to 'dst' object."""
  215. for a,v in src.__dict__.items():
  216. setattr(dst,a,v)
  217. def strip_quotes(s):
  218. """Trim white space and, if necessary, quote characters from s."""
  219. s = s.strip()
  220. # Strip quotation mark characters from quoted strings.
  221. if len(s) >= 3 and s[0] == '"' and s[-1] == '"':
  222. s = s[1:-1]
  223. return s
  224. def is_re(s):
  225. """Return True if s is a valid regular expression else return False."""
  226. try: re.compile(s)
  227. except: return False
  228. else: return True
  229. def re_join(relist):
  230. """Join list of regular expressions re1,re2,... to single regular
  231. expression (re1)|(re2)|..."""
  232. if len(relist) == 0:
  233. return None
  234. result = []
  235. # Delete named groups to avoid ambiguity.
  236. for s in relist:
  237. result.append(re.sub(r'\?P<\S+?>','',s))
  238. result = ')|('.join(result)
  239. result = '('+result+')'
  240. return result
  241. def validate(value,rule,errmsg):
  242. """Validate value against rule expression. Throw EAsciiDoc exception with
  243. errmsg if validation fails."""
  244. try:
  245. if not eval(rule.replace('$',str(value))):
  246. raise EAsciiDoc,errmsg
  247. except Exception:
  248. raise EAsciiDoc,errmsg
  249. return value
  250. def lstrip_list(s):
  251. """
  252. Return list with empty items from start of list removed.
  253. """
  254. for i in range(len(s)):
  255. if s[i]: break
  256. else:
  257. return []
  258. return s[i:]
  259. def rstrip_list(s):
  260. """
  261. Return list with empty items from end of list removed.
  262. """
  263. for i in range(len(s)-1,-1,-1):
  264. if s[i]: break
  265. else:
  266. return []
  267. return s[:i+1]
  268. def strip_list(s):
  269. """
  270. Return list with empty items from start and end of list removed.
  271. """
  272. s = lstrip_list(s)
  273. s = rstrip_list(s)
  274. return s
  275. def is_array(obj):
  276. """
  277. Return True if object is list or tuple type.
  278. """
  279. return isinstance(obj,list) or isinstance(obj,tuple)
  280. def dovetail(lines1, lines2):
  281. """
  282. Append list or tuple of strings 'lines2' to list 'lines1'. Join the last
  283. non-blank item in 'lines1' with the first non-blank item in 'lines2' into a
  284. single string.
  285. """
  286. assert is_array(lines1)
  287. assert is_array(lines2)
  288. lines1 = strip_list(lines1)
  289. lines2 = strip_list(lines2)
  290. if not lines1 or not lines2:
  291. return list(lines1) + list(lines2)
  292. result = list(lines1[:-1])
  293. result.append(lines1[-1] + lines2[0])
  294. result += list(lines2[1:])
  295. return result
  296. def dovetail_tags(stag,content,etag):
  297. """Merge the end tag with the first content line and the last
  298. content line with the end tag. This ensures verbatim elements don't
  299. include extraneous opening and closing line breaks."""
  300. return dovetail(dovetail(stag,content), etag)
  301. def parse_attributes(attrs,dict):
  302. """Update a dictionary with name/value attributes from the attrs string.
  303. The attrs string is a comma separated list of values and keyword name=value
  304. pairs. Values must preceed keywords and are named '1','2'... The entire
  305. attributes list is named '0'. If keywords are specified string values must
  306. be quoted. Examples:
  307. attrs: ''
  308. dict: {}
  309. attrs: 'hello,world'
  310. dict: {'2': 'world', '0': 'hello,world', '1': 'hello'}
  311. attrs: '"hello", planet="earth"'
  312. dict: {'planet': 'earth', '0': '"hello",planet="earth"', '1': 'hello'}
  313. """
  314. def f(*args,**keywords):
  315. # Name and add aguments '1','2'... to keywords.
  316. for i in range(len(args)):
  317. if not str(i+1) in keywords:
  318. keywords[str(i+1)] = args[i]
  319. return keywords
  320. if not attrs:
  321. return
  322. dict['0'] = attrs
  323. # Replace line separators with spaces so line spanning works.
  324. s = re.sub(r'\s', ' ', attrs)
  325. try:
  326. d = eval('f('+s+')')
  327. # Attributes must evaluate to strings, numbers or None.
  328. for v in d.values():
  329. if not (isinstance(v,str) or isinstance(v,int) or isinstance(v,float) or v is None):
  330. raise
  331. except Exception:
  332. s = s.replace('"','\\"')
  333. s = s.split(',')
  334. s = map(lambda x: '"' + x.strip() + '"', s)
  335. s = ','.join(s)
  336. try:
  337. d = eval('f('+s+')')
  338. except Exception:
  339. return # If there's a syntax error leave with {0}=attrs.
  340. for k in d.keys(): # Drop any empty positional arguments.
  341. if d[k] == '': del d[k]
  342. dict.update(d)
  343. assert len(d) > 0
  344. def parse_named_attributes(s,attrs):
  345. """Update a attrs dictionary with name="value" attributes from the s string.
  346. Returns False if invalid syntax.
  347. Example:
  348. attrs: 'star="sun",planet="earth"'
  349. dict: {'planet':'earth', 'star':'sun'}
  350. """
  351. def f(**keywords): return keywords
  352. try:
  353. d = eval('f('+s+')')
  354. attrs.update(d)
  355. return True
  356. except Exception:
  357. return False
  358. def parse_list(s):
  359. """Parse comma separated string of Python literals. Return a tuple of of
  360. parsed values."""
  361. try:
  362. result = eval('tuple(['+s+'])')
  363. except Exception:
  364. raise EAsciiDoc,'malformed list: '+s
  365. return result
  366. def parse_options(options,allowed,errmsg):
  367. """Parse comma separated string of unquoted option names and return as a
  368. tuple of valid options. 'allowed' is a list of allowed option values.
  369. If allowed=() then all legitimate names are allowed.
  370. 'errmsg' is an error message prefix if an illegal option error is thrown."""
  371. result = []
  372. if options:
  373. for s in re.split(r'\s*,\s*',options):
  374. if (allowed and s not in allowed) or not is_name(s):
  375. raise EAsciiDoc,'%s: %s' % (errmsg,s)
  376. result.append(s)
  377. return tuple(result)
  378. def symbolize(s):
  379. """Drop non-symbol characters and convert to lowercase."""
  380. return re.sub(r'(?u)[^\w\-_]', '', s).lower()
  381. def is_name(s):
  382. """Return True if s is valid attribute, macro or tag name
  383. (starts with alpha containing alphanumeric and dashes only)."""
  384. return re.match(r'^'+NAME_RE+r'$',s) is not None
  385. def subs_quotes(text):
  386. """Quoted text is marked up and the resulting text is
  387. returned."""
  388. keys = config.quotes.keys()
  389. for q in keys:
  390. i = q.find('|')
  391. if i != -1 and q != '|' and q != '||':
  392. lq = q[:i] # Left quote.
  393. rq = q[i+1:] # Right quote.
  394. else:
  395. lq = rq = q
  396. tag = config.quotes[q]
  397. # Unconstrained quotes prefix the tag name with a hash.
  398. if tag[0] == '#':
  399. tag = tag[1:]
  400. # Unconstrained quotes can appear anywhere.
  401. reo = re.compile(r'(?msu)(^|.)(\[(?P<attrlist>[^[\]]+?)\])?' \
  402. + r'(?:' + re.escape(lq) + r')' \
  403. + r'(?P<content>.+?)(?:'+re.escape(rq)+r')')
  404. else:
  405. # The text within constrained quotes must be bounded by white space.
  406. # Non-word (\W) characters are allowed at boundaries to accomodate
  407. # enveloping quotes.
  408. reo = re.compile(r'(?msu)(^|\W)(\[(?P<attrlist>[^[\]]+?)\])?' \
  409. + r'(?:' + re.escape(lq) + r')' \
  410. + r'(?P<content>\S|\S.*?\S)(?:'+re.escape(rq)+r')(?=\W|$)')
  411. pos = 0
  412. while True:
  413. mo = reo.search(text,pos)
  414. if not mo: break
  415. if text[mo.start()] == '\\':
  416. # Delete leading backslash.
  417. text = text[:mo.start()] + text[mo.start()+1:]
  418. # Skip past start of match.
  419. pos = mo.start() + 1
  420. else:
  421. attrlist = {}
  422. parse_attributes(mo.group('attrlist'), attrlist)
  423. stag,etag = config.tag(tag, attrlist)
  424. s = mo.group(1) + stag + mo.group('content') + etag
  425. text = text[:mo.start()] + s + text[mo.end():]
  426. pos = mo.start() + len(s)
  427. return text
  428. def subs_tag(tag,dict={}):
  429. """Perform attribute substitution and split tag string returning start, end
  430. tag tuple (c.f. Config.tag())."""
  431. if not tag:
  432. return [None,None]
  433. s = subs_attrs(tag,dict)
  434. if not s:
  435. message.warning('tag \'%s\' dropped: contains undefined attribute' % tag)
  436. return [None,None]
  437. result = s.split('|')
  438. if len(result) == 1:
  439. return result+[None]
  440. elif len(result) == 2:
  441. return result
  442. else:
  443. raise EAsciiDoc,'malformed tag: %s' % tag
  444. def parse_entry(entry, dict=None, unquote=False, unique_values=False,
  445. allow_name_only=False, escape_delimiter=True):
  446. """Parse name=value entry to dictionary 'dict'. Return tuple (name,value)
  447. or None if illegal entry.
  448. If name= then value is set to ''.
  449. If name and allow_name_only=True then value is set to ''.
  450. If name! and allow_name_only=True then value is set to None.
  451. Leading and trailing white space is striped from 'name' and 'value'.
  452. 'name' can contain any printable characters.
  453. If the '=' delimiter character is allowed in the 'name' then
  454. it must be escaped with a backslash and escape_delimiter must be True.
  455. If 'unquote' is True leading and trailing double-quotes are stripped from
  456. 'name' and 'value'.
  457. If unique_values' is True then dictionary entries with the same value are
  458. removed before the parsed entry is added."""
  459. if escape_delimiter:
  460. mo = re.search(r'(?:[^\\](=))',entry)
  461. else:
  462. mo = re.search(r'(=)',entry)
  463. if mo: # name=value entry.
  464. if mo.group(1):
  465. name = entry[:mo.start(1)]
  466. if escape_delimiter:
  467. name = name.replace(r'\=','=') # Unescape \= in name.
  468. value = entry[mo.end(1):]
  469. elif allow_name_only and entry: # name or name! entry.
  470. name = entry
  471. if name[-1] == '!':
  472. name = name[:-1]
  473. value = None
  474. else:
  475. value = ''
  476. else:
  477. return None
  478. if unquote:
  479. name = strip_quotes(name)
  480. if value is not None:
  481. value = strip_quotes(value)
  482. else:
  483. name = name.strip()
  484. if value is not None:
  485. value = value.strip()
  486. if not name:
  487. return None
  488. if dict is not None:
  489. if unique_values:
  490. for k,v in dict.items():
  491. if v == value: del dict[k]
  492. dict[name] = value
  493. return name,value
  494. def parse_entries(entries, dict, unquote=False, unique_values=False,
  495. allow_name_only=False,escape_delimiter=True):
  496. """Parse name=value entries from from lines of text in 'entries' into
  497. dictionary 'dict'. Blank lines are skipped."""
  498. entries = config.expand_templates(entries)
  499. for entry in entries:
  500. if entry and not parse_entry(entry, dict, unquote, unique_values,
  501. allow_name_only, escape_delimiter):
  502. raise EAsciiDoc,'malformed section entry: %s' % entry
  503. def load_conf_file(sections, fname, dir, namepat=NAME_RE):
  504. """Loads sections dictionary with sections from file fname.
  505. Existing sections are overlaid. Silently skips missing configuration
  506. files."""
  507. if dir:
  508. fname = os.path.join(dir, fname)
  509. # Sliently skip missing configuration file.
  510. if not os.path.isfile(fname):
  511. return
  512. reo = re.compile(r'^\[(?P<section>'+namepat+')\]\s*$')
  513. section,contents = '',[]
  514. for line in open(fname):
  515. if line and line[0] == '#': # Skip comment lines.
  516. continue
  517. line = line.rstrip()
  518. found = reo.findall(line)
  519. if found:
  520. if section: # Store previous section.
  521. sections[section] = contents
  522. section = found[0].lower()
  523. contents = []
  524. else:
  525. contents.append(line)
  526. if section and contents: # Store last section.
  527. sections[section] = contents
  528. def dump_section(name,dict,f=sys.stdout):
  529. """Write parameters in 'dict' as in configuration file section format with
  530. section 'name'."""
  531. f.write('[%s]%s' % (name,writer.newline))
  532. for k,v in dict.items():
  533. k = str(k)
  534. k = k.replace('=',r'\=') # Escape = in name.
  535. # Quote if necessary.
  536. if len(k) != len(k.strip()):
  537. k = '"'+k+'"'
  538. if v and len(v) != len(v.strip()):
  539. v = '"'+v+'"'
  540. if v is None:
  541. # Don't dump undefined attributes.
  542. continue
  543. else:
  544. s = k+'='+v
  545. if s[0] == '#':
  546. s = '\\' + s # Escape so not treated as comment lines.
  547. f.write('%s%s' % (s,writer.newline))
  548. f.write(writer.newline)
  549. def update_attrs(attrs,dict):
  550. """Update 'attrs' dictionary with parsed attributes in dictionary 'dict'."""
  551. for k,v in dict.items():
  552. if not is_name(k):
  553. raise EAsciiDoc,'illegal attribute name: %s' % k
  554. attrs[k] = v
  555. def filter_lines(filter_cmd, lines, attrs={}):
  556. """
  557. Run 'lines' through the 'filter_cmd' shell command and return the result.
  558. The 'attrs' dictionary contains additional filter attributes.
  559. """
  560. def findfilter(name,dir,filter):
  561. """Find filter file 'fname' with style name 'name' in directory
  562. 'dir'. Return found file path or None if not found."""
  563. if name:
  564. result = os.path.join(dir,'filters',name,filter)
  565. if os.path.isfile(result):
  566. return result
  567. result = os.path.join(dir,'filters',filter)
  568. if os.path.isfile(result):
  569. return result
  570. return None
  571. # Return input lines if there's not filter.
  572. if not filter_cmd or not filter_cmd.strip():
  573. return lines
  574. # Perform attributes substitution on the filter command.
  575. s = subs_attrs(filter_cmd, attrs)
  576. if not s:
  577. raise EAsciiDoc,'undefined filter attribute in command: %s' % filter_cmd
  578. filter_cmd = s.strip()
  579. # Parse for quoted and unquoted command and command tail.
  580. # Double quoted.
  581. mo = re.match(r'^"(?P<cmd>[^"]+)"(?P<tail>.*)$', filter_cmd)
  582. if not mo:
  583. # Single quoted.
  584. mo = re.match(r"^'(?P<cmd>[^']+)'(?P<tail>.*)$", filter_cmd)
  585. if not mo:
  586. # Unquoted catch all.
  587. mo = re.match(r'^(?P<cmd>\S+)(?P<tail>.*)$', filter_cmd)
  588. cmd = mo.group('cmd').strip()
  589. found = None
  590. if not os.path.dirname(cmd):
  591. # Filter command has no directory path so search filter directories.
  592. filtername = attrs.get('style')
  593. if USER_DIR:
  594. found = findfilter(filtername, USER_DIR, cmd)
  595. if not found:
  596. found = findfilter(filtername, CONF_DIR, cmd)
  597. if not found:
  598. found = findfilter(filtername, DATA_DIR, cmd)
  599. if not found:
  600. found = findfilter(filtername, APP_DIR, cmd)
  601. else:
  602. if os.path.isfile(cmd):
  603. found = cmd
  604. else:
  605. message.warning('filter not found: %s' % cmd)
  606. if found:
  607. filter_cmd = '"' + found + '"' + mo.group('tail')
  608. if sys.platform == 'win32':
  609. # Windows doesn't like running scripts directly so explicitly
  610. # specify interpreter.
  611. if found:
  612. if cmd.endswith('.py'):
  613. filter_cmd = 'python ' + filter_cmd
  614. elif cmd.endswith('.rb'):
  615. filter_cmd = 'ruby ' + filter_cmd
  616. message.verbose('filtering: ' + filter_cmd)
  617. try:
  618. p = subprocess.Popen(filter_cmd, shell=True,
  619. stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  620. output = p.communicate(os.linesep.join(lines))[0]
  621. except Exception:
  622. raise EAsciiDoc,'filter error: %s: %s' % (filter_cmd, sys.exc_info()[1])
  623. if output:
  624. result = [s.rstrip() for s in output.split(os.linesep)]
  625. else:
  626. result = []
  627. filter_status = p.wait()
  628. if filter_status:
  629. message.warning('filter non-zero exit code: %s: returned %d' %
  630. (filter_cmd, filter_status))
  631. if lines and not result:
  632. message.warning('no output from filter: %s' % filter_cmd)
  633. return result
  634. def system(name, args, is_macro=False):
  635. """
  636. Evaluate a system attribute ({name:args}) or system block macro
  637. (name::[args]). If is_macro is True then we are processing a system
  638. block macro otherwise it's a system attribute.
  639. NOTE: The include1 attribute is used internally by the include1::[] macro
  640. and is not for public use.
  641. """
  642. if is_macro:
  643. syntax = '%s::[%s]'
  644. separator = '\n'
  645. else:
  646. syntax = '{%s:%s}'
  647. separator = writer.newline
  648. if name not in ('eval','sys','sys2','include','include1'):
  649. if is_macro:
  650. msg = 'illegal system macro name: %s' % name
  651. else:
  652. msg = 'illegal system attribute name: %s' % name
  653. message.warning(msg)
  654. return None
  655. if is_macro:
  656. s = subs_attrs(args)
  657. if s is None:
  658. message.warning('skipped %s: undefined attribute in: %s' % (name,args))
  659. return None
  660. args = s
  661. if name != 'include1':
  662. message.verbose(('evaluating: '+syntax) % (name,args))
  663. if safe() and name not in ('include','include1'):
  664. message.unsafe(syntax % (name,args))
  665. return None
  666. result = None
  667. if name == 'eval':
  668. try:
  669. result = eval(args)
  670. if result is True:
  671. result = ''
  672. elif result is False:
  673. result = None
  674. elif result is not None:
  675. result = str(result)
  676. except Exception:
  677. message.warning((syntax+': expression evaluation error') % (name,args))
  678. elif name in ('sys','sys2'):
  679. result = ''
  680. fd,tmp = tempfile.mkstemp()
  681. os.close(fd)
  682. try:
  683. cmd = args
  684. cmd = cmd + (' > %s' % tmp)
  685. if name == 'sys2':
  686. cmd = cmd + ' 2>&1'
  687. if os.system(cmd):
  688. message.warning((syntax+': non-zero exit status') % (name,args))
  689. try:
  690. if os.path.isfile(tmp):
  691. lines = [s.rstrip() for s in open(tmp)]
  692. else:
  693. lines = []
  694. except Exception:
  695. raise EAsciiDoc,(syntax+': temp file read error') % (name,args)
  696. result = separator.join(lines)
  697. finally:
  698. if os.path.isfile(tmp):
  699. os.remove(tmp)
  700. elif name == 'include':
  701. if not os.path.exists(args):
  702. message.warning((syntax+': file does not exist') % (name,args))
  703. elif not is_safe_file(args):
  704. message.unsafe(syntax % (name,args))
  705. else:
  706. result = [s.rstrip() for s in open(args)]
  707. if result:
  708. result = subs_attrs(result)
  709. result = separator.join(result)
  710. result = result.expandtabs(reader.tabsize)
  711. else:
  712. result = ''
  713. elif name == 'include1':
  714. result = separator.join(config.include1[args])
  715. else:
  716. assert False
  717. return result
  718. def subs_attrs(lines, dictionary=None):
  719. """Substitute 'lines' of text with attributes from the global
  720. document.attributes dictionary and from 'dictionary' ('dictionary'
  721. entries take precedence). Return a tuple of the substituted lines. 'lines'
  722. containing undefined attributes are deleted. If 'lines' is a string then
  723. return a string.
  724. - Attribute references are substituted in the following order: simple,
  725. conditional, system.
  726. - Attribute references inside 'dictionary' entry values are substituted.
  727. """
  728. def end_brace(text,start):
  729. """Return index following end brace that matches brace at start in
  730. text."""
  731. assert text[start] == '{'
  732. n = 0
  733. result = start
  734. for c in text[start:]:
  735. # Skip braces that are followed by a backslash.
  736. if result == len(text)-1 or text[result+1] != '\\':
  737. if c == '{': n = n + 1
  738. elif c == '}': n = n - 1
  739. result = result + 1
  740. if n == 0: break
  741. return result
  742. if type(lines) == str:
  743. string_result = True
  744. lines = [lines]
  745. else:
  746. string_result = False
  747. lines = list(lines)
  748. if dictionary is None:
  749. attrs = document.attributes
  750. else:
  751. # Remove numbered document attributes so they don't clash with
  752. # attribute list positional attributes.
  753. attrs = {}
  754. for k,v in document.attributes.items():
  755. if not re.match(r'^\d+$', k):
  756. attrs[k] = v
  757. # Substitute attribute references inside dictionary values.
  758. dictionary = dictionary.copy()
  759. for k,v in dictionary.items():
  760. if v is None:
  761. del dictionary[k]
  762. else:
  763. v = subs_attrs(str(v))
  764. if v is None:
  765. del dictionary[k]
  766. else:
  767. dictionary[k] = v
  768. attrs.update(dictionary)
  769. # Substitute all attributes in all lines.
  770. for i in range(len(lines)-1,-1,-1): # Reverse iterate lines.
  771. text = lines[i]
  772. # Make it easier for regular expressions.
  773. text = text.replace('\\{','{\\')
  774. text = text.replace('\\}','}\\')
  775. # Expand simple attributes ({name}).
  776. # Nested attributes not allowed.
  777. reo = re.compile(r'(?su)\{(?P<name>[^\\\W][-\w]*?)\}(?!\\)')
  778. pos = 0
  779. while True:
  780. mo = reo.search(text,pos)
  781. if not mo: break
  782. s = attrs.get(mo.group('name'))
  783. if s is None:
  784. pos = mo.end()
  785. else:
  786. s = str(s)
  787. text = text[:mo.start()] + s + text[mo.end():]
  788. pos = mo.start() + len(s)
  789. # Expand conditional attributes.
  790. reo = re.compile(r'(?su)\{(?P<name>[^\\\W][-\w]*?)' \
  791. r'(?P<op>\=|\?|!|#|%|@|\$)' \
  792. r'(?P<value>.*?)\}(?!\\)')
  793. pos = 0
  794. while True:
  795. mo = reo.search(text,pos)
  796. if not mo: break
  797. attr = mo.group()
  798. name = mo.group('name')
  799. lval = attrs.get(name)
  800. op = mo.group('op')
  801. # mo.end() is not good enough because '{x={y}}' matches '{x={y}'.
  802. end = end_brace(text,mo.start())
  803. rval = text[mo.start('value'):end-1]
  804. if lval is None:
  805. if op == '=': s = rval
  806. elif op == '?': s = ''
  807. elif op == '!': s = rval
  808. elif op == '#': s = '{'+name+'}' # So the line is dropped.
  809. elif op == '%': s = rval
  810. elif op in ('@','$'):
  811. s = '{'+name+'}' # So the line is dropped.
  812. else:
  813. assert False, 'illegal attribute: %s' % attr
  814. else:
  815. if op == '=': s = lval
  816. elif op == '?': s = rval
  817. elif op == '!': s = ''
  818. elif op == '#': s = rval
  819. elif op == '%': s = '{zzzzz}' # So the line is dropped.
  820. elif op in ('@','$'):
  821. v = re.split(r'(?<!\\):',rval)
  822. if len(v) not in (2,3):
  823. message.error('illegal attribute syntax: %s' % attr)
  824. s = ''
  825. elif not is_re('^'+v[0]+'$'):
  826. message.error('illegal attribute regexp: %s' % attr)
  827. s = ''
  828. else:
  829. v = [s.replace('\\:',':') for s in v]
  830. re_mo = re.match('^'+v[0]+'$',lval)
  831. if op == '@':
  832. if re_mo:
  833. s = v[1] # {<name>@<re>:<v1>[:<v2>]}
  834. else:
  835. if len(v) == 3: # {<name>@<re>:<v1>:<v2>}
  836. s = v[2]
  837. else: # {<name>@<re>:<v1>}
  838. s = ''
  839. else:
  840. if re_mo:
  841. if len(v) == 2: # {<name>$<re>:<v1>}
  842. s = v[1]
  843. elif v[1] == '': # {<name>$<re>::<v2>}
  844. s = '{zzzzz}' # So the line is dropped.
  845. else: # {<name>$<re>:<v1>:<v2>}
  846. s = v[1]
  847. else:
  848. if len(v) == 2: # {<name>$<re>:<v1>}
  849. s = '{zzzzz}' # So the line is dropped.
  850. else: # {<name>$<re>:<v1>:<v2>}
  851. s = v[2]
  852. else:
  853. assert False, 'illegal attribute: %s' % attr
  854. s = str(s)
  855. text = text[:mo.start()] + s + text[end:]
  856. pos = mo.start() + len(s)
  857. # Drop line if it contains unsubstituted {name} references.
  858. skipped = re.search(r'(?su)\{[^\\\W][-\w]*?\}(?!\\)', text)
  859. if skipped:
  860. del lines[i]
  861. continue;
  862. # Expand system attributes.
  863. reo = re.compile(r'(?su)\{(?P<action>[^\\\W][-\w]*?):(?P<expr>.*?)\}(?!\\)')
  864. skipped = False
  865. pos = 0
  866. while True:
  867. mo = reo.search(text,pos)
  868. if not mo: break
  869. expr = mo.group('expr')
  870. expr = expr.replace('{\\','{')
  871. expr = expr.replace('}\\','}')
  872. s = system(mo.group('action'),expr)
  873. if s is None:
  874. skipped = True
  875. break
  876. text = text[:mo.start()] + s + text[mo.end():]
  877. pos = mo.start() + len(s)
  878. # Drop line if the action returns None.
  879. if skipped:
  880. del lines[i]
  881. continue;
  882. # Remove backslash from escaped entries.
  883. text = text.replace('{\\','{')
  884. text = text.replace('}\\','}')
  885. lines[i] = text
  886. if string_result:
  887. if lines:
  888. return '\n'.join(lines)
  889. else:
  890. return None
  891. else:
  892. return tuple(lines)
  893. def char_encoding():
  894. encoding = document.attributes.get('encoding')
  895. if encoding:
  896. try:
  897. codecs.lookup(encoding)
  898. except LookupError,e:
  899. raise EAsciiDoc,str(e)
  900. return encoding
  901. def char_len(s):
  902. return len(char_decode(s))
  903. def char_decode(s):
  904. if char_encoding():
  905. try:
  906. return s.decode(char_encoding())
  907. except Exception:
  908. raise EAsciiDoc, \
  909. "'%s' codec can't decode \"%s\"" % (char_encoding(), s)
  910. else:
  911. return s
  912. def char_encode(s):
  913. if char_encoding():
  914. return s.encode(char_encoding())
  915. else:
  916. return s
  917. def time_str(t):
  918. """Convert seconds since the Epoch to formatted local time string."""
  919. t = time.localtime(t)
  920. s = time.strftime('%H:%M:%S',t)
  921. if time.daylight:
  922. result = s + ' ' + time.tzname[1]
  923. else:
  924. result = s + ' ' + time.tzname[0]
  925. # Attempt to convert the localtime to the output encoding.
  926. try:
  927. result = char_encode(result.decode(locale.getdefaultlocale()[1]))
  928. except Exception:
  929. pass
  930. return result
  931. def date_str(t):
  932. """Convert seconds since the Epoch to formatted local date string."""
  933. t = time.localtime(t)
  934. return time.strftime('%Y-%m-%d',t)
  935. class Lex:
  936. """Lexical analysis routines. Static methods and attributes only."""
  937. prev_element = None
  938. prev_cursor = None
  939. def __init__(self):
  940. raise AssertionError,'no class instances allowed'
  941. @staticmethod
  942. def next():
  943. """Returns class of next element on the input (None if EOF). The
  944. reader is assumed to be at the first line following a previous element,
  945. end of file or line one. Exits with the reader pointing to the first
  946. line of the next element or EOF (leading blank lines are skipped)."""
  947. reader.skip_blank_lines()
  948. if reader.eof(): return None
  949. # Optimization: If we've already checked for an element at this
  950. # position return the element.
  951. if Lex.prev_element and Lex.prev_cursor == reader.cursor:
  952. return Lex.prev_element
  953. if AttributeEntry.isnext():
  954. result = AttributeEntry
  955. elif AttributeList.isnext():
  956. result = AttributeList
  957. elif Title.isnext():
  958. result = Title
  959. elif macros.isnext():
  960. result = macros.current
  961. elif lists.isnext():
  962. result = lists.current
  963. elif blocks.isnext():
  964. result = blocks.current
  965. elif tables_OLD.isnext():
  966. result = tables_OLD.current
  967. elif tables.isnext():
  968. result = tables.current
  969. elif BlockTitle.isnext():
  970. result = BlockTitle
  971. else:
  972. if not paragraphs.isnext():
  973. raise EAsciiDoc,'paragraph expected'
  974. result = paragraphs.current
  975. # Optimization: Cache answer.
  976. Lex.prev_cursor = reader.cursor
  977. Lex.prev_element = result
  978. return result
  979. @staticmethod
  980. def canonical_subs(options):
  981. """Translate composite subs values."""
  982. if len(options) == 1:
  983. if options[0] == 'none':
  984. options = ()
  985. elif options[0] == 'normal':
  986. options = config.subsnormal
  987. elif options[0] == 'verbatim':
  988. options = config.subsverbatim
  989. return options
  990. @staticmethod
  991. def subs_1(s,options):
  992. """Perform substitution specified in 'options' (in 'options' order) on
  993. Does not process 'attributes' substitutions."""
  994. if not s:
  995. return s
  996. result = s
  997. options = Lex.canonical_subs(options)
  998. for o in options:
  999. if o == 'specialcharacters':
  1000. result = config.subs_specialchars(result)
  1001. elif o == 'attributes':
  1002. result = subs_attrs(result)
  1003. elif o == 'quotes':
  1004. result = subs_quotes(result)
  1005. elif o == 'specialwords':
  1006. result = config.subs_specialwords(result)
  1007. elif o in ('replacements','replacements2'):
  1008. result = config.subs_replacements(result,o)
  1009. elif o == 'macros':
  1010. result = macros.subs(result)
  1011. elif o == 'callouts':
  1012. result = macros.subs(result,callouts=True)
  1013. else:
  1014. raise EAsciiDoc,'illegal substitution option: %s' % o
  1015. trace(o, s, result)
  1016. if not result:
  1017. break
  1018. return result
  1019. @staticmethod
  1020. def subs(lines,options):
  1021. """Perform inline processing specified by 'options' (in 'options'
  1022. order) on sequence of 'lines'."""
  1023. if not lines or not options:
  1024. return lines
  1025. options = Lex.canonical_subs(options)
  1026. # Join lines so quoting can span multiple lines.
  1027. para = '\n'.join(lines)
  1028. if 'macros' in options:
  1029. para = macros.extract_passthroughs(para)
  1030. for o in options:
  1031. if o == 'attributes':
  1032. # If we don't substitute attributes line-by-line then a single
  1033. # undefined attribute will drop the entire paragraph.
  1034. lines = subs_attrs(para.split('\n'))
  1035. para = '\n'.join(lines)
  1036. else:
  1037. para = Lex.subs_1(para,(o,))
  1038. if 'macros' in options:
  1039. para = macros.restore_passthroughs(para)
  1040. return para.splitlines()
  1041. @staticmethod
  1042. def set_margin(lines, margin=0):
  1043. """Utility routine that sets the left margin to 'margin' space in a
  1044. block of non-blank lines."""
  1045. # Calculate width of block margin.
  1046. lines = list(lines)
  1047. width = len(lines[0])
  1048. for s in lines:
  1049. i = re.search(r'\S',s).start()
  1050. if i < width: width = i
  1051. # Strip margin width from all lines.
  1052. for i in range(len(lines)):
  1053. lines[i] = ' '*margin + lines[i][width:]
  1054. return lines
  1055. #---------------------------------------------------------------------------
  1056. # Document element classes parse AsciiDoc reader input and write DocBook writer
  1057. # output.
  1058. #---------------------------------------------------------------------------
  1059. class Document:
  1060. def __init__(self):
  1061. self.doctype = None # 'article','manpage' or 'book'.
  1062. self.backend = None # -b option argument.
  1063. self.infile = None # Source file name.
  1064. self.outfile = None # Output file name.
  1065. self.attributes = {}
  1066. self.level = 0 # 0 => front matter. 1,2,3 => sect1,2,3.
  1067. self.has_errors = False # Set true if processing errors were flagged.
  1068. self.has_warnings = False # Set true if warnings were flagged.
  1069. self.safe = False # Default safe mode.
  1070. def update_attributes(self):
  1071. # Set implicit attributes.
  1072. if self.infile and os.path.exists(self.infile):
  1073. t = os.path.getmtime(self.infile)
  1074. elif self.infile == '<stdin>':
  1075. t = time.time()
  1076. else:
  1077. t = None
  1078. if t:
  1079. self.attributes['doctime'] = time_str(t)
  1080. self.attributes['docdate'] = date_str(t)
  1081. t = time.time()
  1082. self.attributes['localtime'] = time_str(t)
  1083. self.attributes['localdate'] = date_str(t)
  1084. self.attributes['asciidoc-version'] = VERSION
  1085. self.attributes['backend'] = document.backend
  1086. self.attributes['doctype'] = document.doctype
  1087. self.attributes['backend-'+document.backend] = ''
  1088. self.attributes['doctype-'+document.doctype] = ''
  1089. self.attributes[document.backend+'-'+document.doctype] = ''
  1090. self.attributes['asciidoc-file'] = APP_FILE
  1091. self.attributes['asciidoc-dir'] = APP_DIR
  1092. self.attributes['user-dir'] = USER_DIR
  1093. if self.infile != '<stdin>':
  1094. self.attributes['infile'] = self.infile
  1095. self.attributes['indir'] = os.path.dirname(self.infile)
  1096. self.attributes['docfile'] = self.infile
  1097. self.attributes['docdir'] = os.path.dirname(self.infile)
  1098. self.attributes['docname'] = os.path.splitext(
  1099. os.path.basename(self.infile))[0]
  1100. if config.verbose:
  1101. self.attributes['verbose'] = ''
  1102. # Update with configuration file attributes.
  1103. self.attributes.update(config.conf_attrs)
  1104. # Update with command-line attributes.
  1105. self.attributes.update(config.cmd_attrs)
  1106. # Extract miscellaneous configuration section entries from attributes.
  1107. config.load_miscellaneous(config.conf_attrs)
  1108. config.load_miscellaneous(config.cmd_attrs)
  1109. self.attributes['newline'] = config.newline
  1110. if self.outfile:
  1111. if self.outfile != '<stdout>':
  1112. self.attributes['outfile'] = self.outfile
  1113. self.attributes['outdir'] = os.path.dirname(self.outfile)
  1114. self.attributes['docname'] = os.path.splitext(
  1115. os.path.basename(self.outfile))[0]
  1116. ext = os.path.splitext(self.outfile)[1][1:]
  1117. elif config.outfilesuffix:
  1118. ext = config.outfilesuffix[1:]
  1119. else:
  1120. ext = ''
  1121. if ext:
  1122. self.attributes['filetype'] = ext
  1123. self.attributes['filetype-'+ext] = ''
  1124. def load_lang(self,linenos=False):
  1125. """
  1126. Load language configuration file.
  1127. """
  1128. lang = self.attributes.get('lang')
  1129. message.linenos = linenos
  1130. if lang:
  1131. if not config.load_lang(lang):
  1132. message.error('missing language conf file: lang-%s.conf' % lang)
  1133. self.attributes['lang'] = lang # Reinstate new lang attribute.
  1134. else:
  1135. message.error('language attribute (lang) is not defined')
  1136. message.linenos = None # Restore default line number behavior.
  1137. def set_deprecated_attribute(self,old,new):
  1138. """
  1139. Ensures the 'old' name of an attribute that was renamed to 'new' is
  1140. still honored.
  1141. """
  1142. if self.attributes.get(new) is None:
  1143. if self.attributes.get(old) is not None:
  1144. self.attributes[new] = self.attributes[old]
  1145. else:
  1146. self.attributes[old] = self.attributes[new]
  1147. def translate(self):
  1148. assert self.doctype in ('article','manpage','book'), \
  1149. 'illegal document type'
  1150. assert self.level == 0
  1151. config.expand_all_templates()
  1152. self.load_lang()
  1153. # Skip leading comments and attribute entries.
  1154. finished = False
  1155. attr_count = 0
  1156. while not finished:
  1157. finished = True
  1158. if blocks.isnext() and 'skip' in blocks.current.options:
  1159. finished = False
  1160. blocks.current.translate()
  1161. if macros.isnext() and macros.current.name == 'comment':
  1162. finished = False
  1163. macros.current.translate()
  1164. if AttributeEntry.isnext():
  1165. finished = False
  1166. AttributeEntry.translate()
  1167. if AttributeEntry.name == 'lang':
  1168. self.load_lang(linenos=True)
  1169. if attr_count > 0:
  1170. message.error('lang attribute should be first entry')
  1171. attr_count += 1
  1172. message.verbose('writing: '+writer.fname,False)
  1173. # Process document header.
  1174. has_header = Lex.next() is Title and Title.level == 0
  1175. if self.doctype == 'manpage' and not has_header:
  1176. message.error('manpage document title is mandatory')
  1177. if has_header:
  1178. Header.translate()
  1179. # Command-line entries override header derived entries.
  1180. self.attributes.update(config.cmd_attrs)
  1181. # DEPRECATED: revision renamed to revnumber.
  1182. self.set_deprecated_attribute('revision','revnumber')
  1183. # DEPRECATED: date renamed to revdate.
  1184. self.set_deprecated_attribute('date','revdate')
  1185. if config.header_footer:
  1186. hdr = config.subs_section('header',{})
  1187. writer.write(hdr,trace='header')
  1188. if self.doctype in ('article','book'):
  1189. # Translate 'preamble' (untitled elements between header
  1190. # and first section title).
  1191. if Lex.next() is not Title:
  1192. stag,etag = config.section2tags('preamble')
  1193. writer.write(stag,trace='preamble open')
  1194. Section.translate_body()
  1195. writer.write(etag,trace='preamble close')
  1196. else:
  1197. document.process_author_names()
  1198. if config.header_footer:
  1199. hdr = config.subs_section('header',{})
  1200. writer.write(hdr,trace='header')
  1201. if Lex.next() is not Title:
  1202. Section.translate_body()
  1203. # Process remaining sections.
  1204. while not reader.eof():
  1205. if Lex.next() is not Title:
  1206. raise EAsciiDoc,'section title expected'
  1207. Section.translate()
  1208. Section.setlevel(0) # Write remaining unwritten section close tags.
  1209. # Substitute document parameters and write document footer.
  1210. if config.header_footer:
  1211. ftr = config.subs_section('footer',{})
  1212. writer.write(ftr,trace='footer')
  1213. def parse_author(self,s):
  1214. """ Return False if the author is malformed."""
  1215. attrs = self.attributes # Alias for readability.
  1216. s = s.strip()
  1217. mo = re.match(r'^(?P<name1>[^<>\s]+)'
  1218. '(\s+(?P<name2>[^<>\s]+))?'
  1219. '(\s+(?P<name3>[^<>\s]+))?'
  1220. '(\s+<(?P<email>\S+)>)?$',s)
  1221. if not mo:
  1222. message.error('malformed author: %s' % s)
  1223. return False
  1224. firstname = mo.group('name1')
  1225. if mo.group('name3'):
  1226. middlename = mo.group('name2')
  1227. lastname = mo.group('name3')
  1228. else:
  1229. middlename = None
  1230. lastname = mo.group('name2')
  1231. firstname = firstname.replace('_',' ')
  1232. if middlename:
  1233. middlename = middlename.replace('_',' ')
  1234. if lastname:
  1235. lastname = lastname.replace('_',' ')
  1236. email = mo.group('email')
  1237. if firstname:
  1238. attrs['firstname'] = firstname
  1239. if middlename:
  1240. attrs['middlename'] = middlename
  1241. if lastname:
  1242. attrs['lastname'] = lastname
  1243. if email:
  1244. attrs['email'] = email
  1245. return True
  1246. def process_author_names(self):
  1247. """ Calculate any missing author related attributes."""
  1248. attrs = self.attributes # Alias for readability.
  1249. firstname = attrs.get('firstname','')
  1250. middlename = attrs.get('middlename','')
  1251. lastname = attrs.get('lastname','')
  1252. author = attrs.get('author')
  1253. initials = attrs.get('authorinitials')
  1254. if author and not (firstname or middlename or lastname):
  1255. if not self.parse_author(author):
  1256. return
  1257. attrs['author'] = author.replace('_',' ')
  1258. self.process_author_names()
  1259. return
  1260. if not author:
  1261. author = '%s %s %s' % (firstname, middlename, lastname)
  1262. author = author.strip()
  1263. author = re.sub(r'\s+',' ', author)
  1264. if not initials:
  1265. initials = firstname[:1] + middlename[:1] + lastname[:1]
  1266. initials = initials.upper()
  1267. names = [firstname,middlename,lastname,author,initials]
  1268. for i,v in enumerate(names):
  1269. v = config.subs_specialchars(v)
  1270. v = subs_attrs(v)
  1271. names[i] = v
  1272. firstname,middlename,lastname,author,initials = names
  1273. if firstname:
  1274. attrs['firstname'] = firstname
  1275. if middlename:
  1276. attrs['middlename'] = middlename
  1277. if lastname:
  1278. attrs['lastname'] = lastname
  1279. if author:
  1280. attrs['author'] = author
  1281. if initials:
  1282. attrs['authorinitials'] = initials
  1283. if author:
  1284. attrs['authored'] = ''
  1285. class Header:
  1286. """Static methods and attributes only."""
  1287. REV_LINE_RE = r'^(\D*(?P<revnumber>.*?),)?(?P<revdate>.*?)(:\s*(?P<revremark>.*))?$'
  1288. RCS_ID_RE = r'^\$Id: \S+ (?P<revnumber>\S+) (?P<revdate>\S+) \S+ (?P<author>\S+) (\S+ )?\$$'
  1289. def __init__(self):
  1290. raise AssertionError,'no class instances allowed'
  1291. @staticmethod
  1292. def translate():
  1293. assert Lex.next() is Title and Title.level == 0
  1294. Title.translate()
  1295. attrs = document.attributes # Alias for readability.
  1296. attrs['doctitle'] = Title.attributes['title']
  1297. if document.doctype == 'manpage':
  1298. # manpage title formatted like mantitle(manvolnum).
  1299. mo = re.match(r'^(?P<mantitle>.*)\((?P<manvolnum>.*)\)$',
  1300. attrs['doctitle'])
  1301. if not mo:
  1302. message.error('malformed manpage title')
  1303. else:
  1304. mantitle = mo.group('mantitle').strip()
  1305. # mantitle is lowered only if in ALL CAPS
  1306. if mantitle == mantitle.upper():
  1307. mantitle = mantitle.lower()
  1308. attrs['mantitle'] = mantitle;
  1309. attrs['manvolnum'] = mo.group('manvolnum').strip()
  1310. AttributeEntry.translate_all()
  1311. s = reader.read_next()
  1312. mo = None
  1313. if s:
  1314. s = reader.read()
  1315. mo = re.match(Header.RCS_ID_RE,s)
  1316. if not mo:
  1317. document.parse_author(s)
  1318. AttributeEntry.translate_all()
  1319. if reader.read_next():
  1320. # Parse revision line.
  1321. s = reader.read()
  1322. s = subs_attrs(s)
  1323. if s:
  1324. mo = re.match(Header.RCS_ID_RE,s)
  1325. if not mo:
  1326. mo = re.match(Header.REV_LINE_RE,s)
  1327. AttributeEntry.translate_all()
  1328. s = attrs.get('revnumber')
  1329. if s:
  1330. mo = re.match(Header.RCS_ID_RE,s)
  1331. if mo:
  1332. revnumber = mo.group('revnumber')
  1333. if revnumber:
  1334. attrs['revnumber'] = revnumber.strip()
  1335. author = mo.groupdict().get('author')
  1336. if author and 'firstname' not in attrs:
  1337. document.parse_author(author)
  1338. revremark = mo.groupdict().get('revremark')
  1339. if revremark is not None:
  1340. revremark = [revremark]
  1341. # Revision remarks can continue on following lines.
  1342. while reader.read_next() and not AttributeEntry.isnext():
  1343. revremark.append(reader.read())
  1344. revremark = Lex.subs(revremark,['normal'])
  1345. revremark = '\n'.join(revremark).strip()
  1346. attrs['revremark'] = revremark
  1347. AttributeEntry.translate_all()
  1348. revdate = mo.group('revdate')
  1349. if revdate:
  1350. attrs['revdate'] = revdate.strip()
  1351. elif revnumber or revremark:
  1352. # Set revision date to ensure valid DocBook revision.
  1353. attrs['revdate'] = attrs['docdate']
  1354. if document.doctype == 'manpage':
  1355. # Translate mandatory NAME section.
  1356. if Lex.next() is not Title:
  1357. message.error('name section expected')
  1358. else:
  1359. Title.translate()
  1360. if Title.level != 1:
  1361. message.error('name section title must be at level 1')
  1362. if not isinstance(Lex.next(),Paragraph):
  1363. message.error('malformed name section body')
  1364. lines = reader.read_until(r'^$')
  1365. s = ' '.join(lines)
  1366. mo = re.match(r'^(?P<manname>.*?)\s+-\s+(?P<manpurpose>.*)$',s)
  1367. if not mo:
  1368. message.error('malformed name section body')
  1369. attrs['manname'] = mo.group('manname').strip()
  1370. attrs['manpurpose'] = mo.group('manpurpose').strip()
  1371. names = [s.strip() for s in attrs['manname'].split(',')]
  1372. if len(names) > 9:
  1373. message.warning('to many manpage names')
  1374. for i,name in enumerate(names):
  1375. attrs['manname%d' % (i+1)] = name
  1376. document.process_author_names()
  1377. class AttributeEntry:
  1378. """Static methods and attributes only."""
  1379. pattern = None
  1380. subs = None
  1381. name = None
  1382. name2 = None
  1383. value = None
  1384. def __init__(self):
  1385. raise AssertionError,'no class instances allowed'
  1386. @staticmethod
  1387. def isnext():
  1388. result = False # Assume not next.
  1389. if not AttributeEntry.pattern:
  1390. pat = document.attributes.get('attributeentry-pattern')
  1391. if not pat:
  1392. message.error("[attributes] missing 'attributeentry-pattern' entry")
  1393. AttributeEntry.pattern = pat
  1394. if not AttributeEntry.subs:
  1395. subs = document.attributes.get('attributeentry-subs')
  1396. if subs:
  1397. subs = parse_options(subs,SUBS_OPTIONS,
  1398. 'illegal [%s] %s: %s' % ('attributes','attributeentry-subs',subs))
  1399. else:
  1400. subs = ('specialcharacters','attributes')
  1401. AttributeEntry.subs = subs
  1402. line = reader.read_next()
  1403. if line:
  1404. # Attribute entry formatted like :<name>[.<name2>]:[ <value>]
  1405. mo = re.match(AttributeEntry.pattern,line)
  1406. if mo:
  1407. AttributeEntry.name = mo.group('attrname')
  1408. AttributeEntry.name2 = mo.group('attrname2')
  1409. AttributeEntry.value = mo.group('attrvalue') or ''
  1410. AttributeEntry.value = AttributeEntry.value.strip()
  1411. result = True
  1412. return result
  1413. @staticmethod
  1414. def translate():
  1415. assert Lex.next() is AttributeEntry
  1416. attr = AttributeEntry # Alias for brevity.
  1417. reader.read() # Discard attribute entry from reader.
  1418. if AttributeEntry.name2: # The entry is a conf file entry.
  1419. section = {}
  1420. # Some sections can have name! syntax.
  1421. if attr.name in ('attributes','miscellaneous') and attr.name2[-1] == '!':
  1422. section[attr.name] = [attr.name2]
  1423. else:
  1424. section[attr.name] = ['%s=%s' % (attr.name2,attr.value)]
  1425. config.load_sections(section)
  1426. config.validate()
  1427. else: # The entry is an attribute.
  1428. if attr.name[-1] == '!':
  1429. # Names like name! undefine the attribute.
  1430. attr.name = attr.name[:-1]
  1431. attr.value = None
  1432. # Strip white space and illegal name chars.
  1433. attr.name = re.sub(r'(?u)[^\w\-_]', '', attr.name).lower()
  1434. # Don't override command-line attributes (the exception is the
  1435. # system 'trace' attribute).
  1436. if attr.name in config.cmd_attrs and attr.name != 'trace':
  1437. return
  1438. # Update document.attributes from previously parsed attribute.
  1439. if attr.name == 'attributeentry-subs':
  1440. AttributeEntry.subs = None # Force update in isnext().
  1441. elif attr.value:
  1442. attr.value = Lex.subs((attr.value,), attr.subs)
  1443. attr.value = writer.newline.join(attr.value)
  1444. if attr.value is not None:
  1445. document.attributes[attr.name] = attr.value
  1446. elif attr.name in document.attributes:
  1447. del document.attributes[attr.name]
  1448. @staticmethod
  1449. def translate_all():
  1450. """ Process all contiguous attribute lines on reader."""
  1451. while AttributeEntry.isnext():
  1452. AttributeEntry.translate()
  1453. class AttributeList:
  1454. """Static methods and attributes only."""
  1455. pattern = None
  1456. match = None
  1457. attrs = {}
  1458. def __init__(self):
  1459. raise AssertionError,'no class instances allowed'
  1460. @staticmethod
  1461. def initialize():
  1462. if not 'attributelist-pattern' in document.attributes:
  1463. message.error("[attributes] missing 'attributelist-pattern' entry")
  1464. AttributeList.pattern = document.attributes['attributelist-pattern']
  1465. @staticmethod
  1466. def isnext():
  1467. result = False # Assume not next.
  1468. line = reader.read_next()
  1469. if line:
  1470. mo = re.match(AttributeList.pattern, line)
  1471. if mo:
  1472. AttributeList.match = mo
  1473. result = True
  1474. return result
  1475. @staticmethod
  1476. def translate():
  1477. assert Lex.next() is AttributeList
  1478. reader.read() # Discard attribute list from reader.
  1479. attrlist = {}
  1480. d = AttributeList.match.groupdict()
  1481. for k,v in d.items():
  1482. if v is not None:
  1483. if k == 'attrlist':
  1484. v = subs_attrs(v)
  1485. if v:
  1486. parse_attributes(v, attrlist)
  1487. else:
  1488. AttributeList.attrs[k] = v
  1489. # Substitute single quoted attribute values.
  1490. reo = re.compile(r"^'.*'$")
  1491. for k,v in attrlist.items():
  1492. if reo.match(str(v)):
  1493. attrlist[k] = Lex.subs_1(v[1:-1],SUBS_NORMAL)
  1494. AttributeList.attrs.update(attrlist)
  1495. @staticmethod
  1496. def style():
  1497. return AttributeList.attrs.get('style') or AttributeList.attrs.get('1')
  1498. @staticmethod
  1499. def consume(d):
  1500. """Add attribute list to the dictionary 'd' and reset the
  1501. list."""
  1502. if AttributeList.attrs:
  1503. d.update(AttributeList.attrs)
  1504. AttributeList.attrs = {}
  1505. # Generate option attributes.
  1506. if 'options' in d:
  1507. options = parse_options(d['options'], (), 'illegal option name')
  1508. for option in options:
  1509. d[option+'-option'] = ''
  1510. class BlockTitle:
  1511. """Static methods and attributes only."""
  1512. title = None
  1513. pattern = None
  1514. def __init__(self):
  1515. raise AssertionError,'no class instances allowed'
  1516. @staticmethod
  1517. def isnext():
  1518. result = False # Assume not next.
  1519. line = reader.read_next()
  1520. if line:
  1521. mo = re.match(BlockTitle.pattern,line)
  1522. if mo:
  1523. BlockTitle.title = mo.group('title')
  1524. result = True
  1525. return result
  1526. @staticmethod
  1527. def translate():
  1528. assert Lex.next() is BlockTitle
  1529. reader.read() # Discard title from reader.
  1530. # Perform title substitutions.
  1531. if not Title.subs:
  1532. Title.subs = config.subsnormal
  1533. s = Lex.subs((BlockTitle.title,), Title.subs)
  1534. s = writer.newline.join(s)
  1535. if not s:
  1536. message.warning('blank block title')
  1537. BlockTitle.title = s
  1538. @staticmethod
  1539. def consume(d):
  1540. """If there is a title add it to dictionary 'd' then reset title."""
  1541. if BlockTitle.title:
  1542. d['title'] = BlockTitle.title
  1543. BlockTitle.title = None
  1544. class Title:
  1545. """Processes Header and Section titles. Static methods and attributes
  1546. only."""
  1547. # Class variables
  1548. underlines = ('==','--','~~','^^','++') # Levels 0,1,2,3,4.
  1549. subs = ()
  1550. pattern = None
  1551. level = 0
  1552. attributes = {}
  1553. sectname = None
  1554. section_numbers = [0]*len(underlines)
  1555. dump_dict = {}
  1556. linecount = None # Number of lines in title (1 or 2).
  1557. def __init__(self):
  1558. raise AssertionError,'no class instances allowed'
  1559. @staticmethod
  1560. def translate():
  1561. """Parse the Title.attributes and Title.level from the reader. The
  1562. real work has already been done by parse()."""
  1563. assert Lex.next() is Title
  1564. # Discard title from reader.
  1565. for i in range(Title.linecount):
  1566. reader.read()
  1567. Title.setsectname()
  1568. # Perform title substitutions.
  1569. if not Title.subs:
  1570. Title.subs = config.subsnormal
  1571. s = Lex.subs((Title.attributes['title'],), Title.subs)
  1572. s = writer.newline.join(s)
  1573. if not s:
  1574. message.warning('blank section title')
  1575. Title.attributes['title'] = s
  1576. @staticmethod
  1577. def isnext():
  1578. lines = reader.read_ahead(2)
  1579. return Title.parse(lines)
  1580. @staticmethod
  1581. def parse(lines):
  1582. """Parse title at start of lines tuple."""
  1583. if len(lines) == 0: return False
  1584. if len(lines[0]) == 0: return False # Title can't be blank.
  1585. # Check for single-line titles.
  1586. result = False
  1587. for level in range(len(Title.underlines)):
  1588. k = 'sect%s' % level
  1589. if k in Title.dump_dict:
  1590. mo = re.match(Title.dump_dict[k], lines[0])
  1591. if mo:
  1592. Title.attributes = mo.groupdict()
  1593. Title.level = level
  1594. Title.linecount = 1
  1595. result = True
  1596. break
  1597. if not result:
  1598. # Check for double-line titles.
  1599. if not Title.pattern: return False # Single-line titles only.
  1600. if len(lines) < 2: return False
  1601. title,ul = lines[:2]
  1602. title_len = char_len(title)
  1603. ul_len = char_len(ul)
  1604. if ul_len < 2: return False
  1605. # Fast elimination check.
  1606. if ul[:2] not in Title.underlines: return False
  1607. # Length of underline must be within +-3 of title.
  1608. if not (ul_len-3 < title_len < ul_len+3): return False
  1609. # Check for valid repetition of underline character pairs.
  1610. s = ul[:2]*((ul_len+1)/2)
  1611. if ul != s[:ul_len]: return False
  1612. # Don't be fooled by back-to-back delimited blocks, require at
  1613. # least one alphanumeric character in title.
  1614. if not re.search(r'(?u)\w',title): return False
  1615. mo = re.match(Title.pattern, title)
  1616. if mo:
  1617. Title.attributes = mo.groupdict()
  1618. Title.level = list(Title.underlines).index(ul[:2])
  1619. Title.linecount = 2
  1620. result = True
  1621. # Check for expected pattern match groups.
  1622. if result:
  1623. if not 'title' in Title.attributes:
  1624. message.warning('[titles] entry has no <title> group')
  1625. Title.attributes['title'] = lines[0]
  1626. for k,v in Title.attributes.items():
  1627. if v is None: del Title.attributes[k]
  1628. Title.attributes['level'] = str(Title.level)
  1629. return result
  1630. @staticmethod
  1631. def load(entries):
  1632. """Load and validate [titles] section entries dictionary."""
  1633. if 'underlines' in entries:
  1634. errmsg = 'malformed [titles] underlines entry'
  1635. try:
  1636. underlines = parse_list(entries['underlines'])
  1637. except Exception:
  1638. raise EAsciiDoc,errmsg
  1639. if len(underlines) != len(Title.underlines):
  1640. raise EAsciiDoc,errmsg
  1641. for s in underlines:
  1642. if len(s) !=2:
  1643. raise EAsciiDoc,errmsg
  1644. Title.underlines = tuple(underlines)
  1645. Title.dump_dict['underlines'] = entries['underlines']
  1646. if 'subs' in entries:
  1647. Title.subs = parse_options(entries['subs'], SUBS_OPTIONS,
  1648. 'illegal [titles] subs entry')
  1649. Title.dump_dict['subs'] = entries['subs']
  1650. if 'sectiontitle' in entries:
  1651. pat = entries['sectiontitle']
  1652. if not pat or not is_re(pat):
  1653. raise EAsciiDoc,'malformed [titles] sectiontitle entry'
  1654. Title.pattern = pat
  1655. Title.dump_dict['sectiontitle'] = pat
  1656. if 'blocktitle' in entries:
  1657. pat = entries['blocktitle']
  1658. if not pat or not is_re(pat):
  1659. raise EAsciiDoc,'malformed [titles] blocktitle entry'
  1660. BlockTitle.pattern = pat
  1661. Title.dump_dict['blocktitle'] = pat
  1662. # Load single-line title patterns.
  1663. for k in ('sect0','sect1','sect2','sect3','sect4'):
  1664. if k in entries:
  1665. pat = entries[k]
  1666. if not pat or not is_re(pat):
  1667. raise EAsciiDoc,'malformed [titles] %s entry' % k
  1668. Title.dump_dict[k] = pat
  1669. # TODO: Check we have either a Title.pattern or at least one
  1670. # single-line title pattern -- can this be done here or do we need
  1671. # check routine like the other block checkers?
  1672. @staticmethod
  1673. def dump():
  1674. dump_section('titles',Title.dump_dict)
  1675. @staticmethod
  1676. def setsectname():
  1677. """
  1678. Set Title section name:
  1679. If the first positional or 'template' attribute is set use it,
  1680. next search for section title in [specialsections],
  1681. if not found use default 'sect<level>' name.
  1682. """
  1683. sectname = AttributeList.attrs.get('1')
  1684. if sectname and sectname != 'float':
  1685. Title.sectname = sectname
  1686. elif 'template' in AttributeList.attrs:
  1687. Title.sectname = AttributeList.attrs['template']
  1688. else:
  1689. for pat,sect in config.specialsections.items():
  1690. mo = re.match(pat,Title.attributes['title'])
  1691. if mo:
  1692. title = mo.groupdict().get('title')
  1693. if title is not None:
  1694. Title.attributes['title'] = title.strip()
  1695. else:
  1696. Title.attributes['title'] = mo.group().strip()
  1697. Title.sectname = sect
  1698. break
  1699. else:
  1700. Title.sectname = 'sect%d' % Title.level
  1701. @staticmethod
  1702. def getnumber(level):
  1703. """Return next section number at section 'level' formatted like
  1704. 1.2.3.4."""
  1705. number = ''
  1706. for l in range(len(Title.section_numbers)):
  1707. n = Title.section_numbers[l]
  1708. if l == 0:
  1709. continue
  1710. elif l < level:
  1711. number = '%s%d.' % (number, n)
  1712. elif l == level:
  1713. number = '%s%d.' % (number, n + 1)
  1714. Title.section_numbers[l] = n + 1
  1715. elif l > level:
  1716. # Reset unprocessed section levels.
  1717. Title.section_numbers[l] = 0
  1718. return number
  1719. class Section:
  1720. """Static methods and attributes only."""
  1721. endtags = [] # Stack of currently open section (level,endtag) tuples.
  1722. ids = [] # List of already used ids.
  1723. def __init__(self):
  1724. raise AssertionError,'no class instances allowed'
  1725. @staticmethod
  1726. def savetag(level,etag):
  1727. """Save section end."""
  1728. Section.endtags.append((level,etag))
  1729. @staticmethod
  1730. def setlevel(level):
  1731. """Set document level and write open section close tags up to level."""
  1732. while Section.endtags and Section.endtags[-1][0] >= level:
  1733. writer.write(Section.endtags.pop()[1],trace='section close')
  1734. document.level = level
  1735. @staticmethod
  1736. def gen_id(title):
  1737. """
  1738. The normalized value of the id attribute is an NCName according to
  1739. the 'Namespaces in XML' Recommendation:
  1740. NCName ::= NCNameStartChar NCNameChar*
  1741. NCNameChar ::= NameChar - ':'
  1742. NCNameStartChar ::= Letter | '_'
  1743. NameChar ::= Letter | Digit | '.' | '-' | '_' | ':'
  1744. """
  1745. base_ident = re.sub(r'[^a-zA-Z0-9]+', '_', title).strip('_').lower()
  1746. # Prefix the ID name with idprefix attribute or underscore if not
  1747. # defined. Prefix ensures the ID does not clash with existing IDs.
  1748. idprefix = document.attributes.get('idprefix','_')
  1749. base_ident = idprefix + base_ident
  1750. i = 1
  1751. while True:
  1752. if i == 1:
  1753. ident = base_ident
  1754. else:
  1755. ident = '%s_%d' % (base_ident, i)
  1756. if ident not in Section.ids:
  1757. Section.ids.append(ident)
  1758. return ident
  1759. else:
  1760. ident = base_ident
  1761. i += 1
  1762. @staticmethod
  1763. def set_id():
  1764. if not document.attributes.get('sectids') is None \
  1765. and 'id' not in AttributeList.attrs:
  1766. # Generate ids for sections.
  1767. AttributeList.attrs['id'] = Section.gen_id(Title.attributes['title'])
  1768. @staticmethod
  1769. def translate():
  1770. assert Lex.next() is Title
  1771. prev_sectname = Title.sectname
  1772. Title.translate()
  1773. if Title.level == 0 and document.doctype != 'book':
  1774. message.error('only book doctypes can contain level 0 sections')
  1775. if Title.level > document.level \
  1776. and document.backend == 'docbook' \
  1777. and prev_sectname in ('colophon','abstract', \
  1778. 'dedication','glossary','bibliography'):
  1779. message.error('%s section cannot contain sub-sections' % prev_sectname)
  1780. if Title.level > document.level+1:
  1781. # Sub-sections of multi-part book level zero Preface and Appendices
  1782. # are meant to be out of sequence.
  1783. if document.doctype == 'book' \
  1784. and document.level == 0 \
  1785. and Title.level == 2 \
  1786. and prev_sectname in ('preface','appendix'):
  1787. pass
  1788. else:
  1789. message.warning('section title out of sequence: '
  1790. 'expected level %d, got level %d'
  1791. % (document.level+1, Title.level))
  1792. Section.set_id()
  1793. Section.setlevel(Title.level)
  1794. Title.attributes['sectnum'] = Title.getnumber(document.level)
  1795. AttributeList.consume(Title.attributes)
  1796. stag,etag = config.section2tags(Title.sectname,Title.attributes)
  1797. Section.savetag(Title.level,etag)
  1798. writer.write(stag,trace='section open')
  1799. Section.translate_body()
  1800. @staticmethod
  1801. def translate_body(terminator=Title):
  1802. isempty = True
  1803. next = Lex.next()
  1804. while next and next is not terminator:
  1805. if (isinstance(terminator,DelimitedBlock)
  1806. and next is Title and AttributeList.style() != 'float'):
  1807. message.error('section title not permitted in delimited block')
  1808. next.translate()
  1809. next = Lex.next()
  1810. if next is Title and AttributeList.style() == 'float':
  1811. # Process floating titles.
  1812. template = 'floatingtitle'
  1813. if template in config.sections:
  1814. Title.translate()
  1815. Section.set_id()
  1816. AttributeList.consume(Title.attributes)
  1817. stag,etag = config.section2tags(template,Title.attributes)
  1818. writer.write(stag,trace='floating title')
  1819. next = Lex.next()
  1820. else:
  1821. message.warning('missing template section: [%s]' % template)
  1822. isempty = False
  1823. # The section is not empty if contains a subsection.
  1824. if next and isempty and Title.level > document.level:
  1825. isempty = False
  1826. # Report empty sections if invalid markup will result.
  1827. if isempty:
  1828. if document.backend == 'docbook' and Title.sectname != 'index':
  1829. message.error('empty section is not valid')
  1830. class AbstractBlock:
  1831. def __init__(self):
  1832. # Configuration parameter names common to all blocks.
  1833. self.CONF_ENTRIES = ('delimiter','options','subs','presubs','postsubs',
  1834. 'posattrs','style','.*-style','template','filter')
  1835. self.start = None # File reader cursor at start delimiter.
  1836. self.name=None # Configuration file section name.
  1837. # Configuration parameters.
  1838. self.delimiter=None # Regular expression matching block delimiter.
  1839. self.delimiter_reo=None # Compiled delimiter.
  1840. self.template=None # template section entry.
  1841. self.options=() # options entry list.
  1842. self.presubs=None # presubs/subs entry list.
  1843. self.postsubs=() # postsubs entry list.
  1844. self.filter=None # filter entry.
  1845. self.posattrs=() # posattrs entry list.
  1846. self.style=None # Default style.
  1847. self.styles=OrderedDict() # Each entry is a styles dictionary.
  1848. # Before a block is processed it's attributes (from it's
  1849. # attributes list) are merged with the block configuration parameters
  1850. # (by self.merge_attributes()) resulting in the template substitution
  1851. # dictionary (self.attributes) and the block's processing parameters
  1852. # (self.parameters).
  1853. self.attributes={}
  1854. # The names of block parameters.
  1855. self.PARAM_NAMES=('template','options','presubs','postsubs','filter')
  1856. self.parameters=None
  1857. # Leading delimiter match object.
  1858. self.mo=None
  1859. def short_name(self):
  1860. """ Return the text following the last dash in the section namem """
  1861. i = self.name.rfind('-')
  1862. if i == -1:
  1863. return self.name
  1864. else:
  1865. return self.name[i+1:]
  1866. def error(self, msg, cursor=None, halt=False):
  1867. message.error('[%s] %s' % (self.name,msg), cursor, halt)
  1868. def is_conf_entry(self,param):
  1869. """Return True if param matches an allowed configuration file entry
  1870. name."""
  1871. for s in self.CONF_ENTRIES:
  1872. if re.match('^'+s+'$',param):
  1873. return True
  1874. return False
  1875. def load(self,name,entries):
  1876. """Update block definition from section 'entries' dictionary."""
  1877. self.name = name
  1878. self.update_parameters(entries, self, all=True)
  1879. def update_parameters(self, src, dst=None, all=False):
  1880. """
  1881. Parse processing parameters from src dictionary to dst object.
  1882. dst defaults to self.parameters.
  1883. If all is True then copy src entries that aren't parameter names.
  1884. """
  1885. dst = dst or self.parameters
  1886. msg = '[%s] malformed entry %%s: %%s' % self.name
  1887. def copy(obj,k,v):
  1888. if isinstance(obj,dict):
  1889. obj[k] = v
  1890. else:
  1891. setattr(obj,k,v)
  1892. for k,v in src.items():
  1893. if not re.match(r'\d+',k) and not is_name(k):
  1894. raise EAsciiDoc, msg % (k,v)
  1895. if k == 'template':
  1896. if not is_name(v):
  1897. raise EAsciiDoc, msg % (k,v)
  1898. copy(dst,k,v)
  1899. elif k == 'filter':
  1900. copy(dst,k,v)
  1901. elif k == 'options':
  1902. if isinstance(v,str):
  1903. v = parse_options(v, (), msg % (k,v))
  1904. copy(dst,k,v)
  1905. elif k in ('subs','presubs','postsubs'):
  1906. # Subs is an alias for presubs.
  1907. if k == 'subs': k = 'presubs'
  1908. if isinstance(v,str):
  1909. v = parse_options(v, SUBS_OPTIONS, msg % (k,v))
  1910. copy(dst,k,v)
  1911. elif k == 'delimiter':
  1912. if v and is_re(v):
  1913. copy(dst,k,v)
  1914. else:
  1915. raise EAsciiDoc, msg % (k,v)
  1916. elif k == 'style':
  1917. if is_name(v):
  1918. copy(dst,k,v)
  1919. else:
  1920. raise EAsciiDoc, msg % (k,v)
  1921. elif k == 'posattrs':
  1922. v = parse_options(v, (), msg % (k,v))
  1923. copy(dst,k,v)
  1924. else:
  1925. mo = re.match(r'^(?P<style>.*)-style$',k)
  1926. if mo:
  1927. if not v:
  1928. raise EAsciiDoc, msg % (k,v)
  1929. style = mo.group('style')
  1930. if not is_name(style):
  1931. raise EAsciiDoc, msg % (k,v)
  1932. d = {}
  1933. if not parse_named_attributes(v,d):
  1934. raise EAsciiDoc, msg % (k,v)
  1935. if 'subs' in d:
  1936. # Subs is an alias for presubs.
  1937. d['presubs'] = d['subs']
  1938. del d['subs']
  1939. self.styles[style] = d
  1940. elif all or k in self.PARAM_NAMES:
  1941. copy(dst,k,v) # Derived class specific entries.
  1942. def get_param(self,name,params=None):
  1943. """
  1944. Return named processing parameter from params dictionary.
  1945. If the parameter is not in params look in self.parameters.
  1946. """
  1947. if params and name in params:
  1948. return params[name]
  1949. elif name in self.parameters:
  1950. return self.parameters[name]
  1951. else:
  1952. return None
  1953. def get_subs(self,params=None):
  1954. """
  1955. Return (presubs,postsubs) tuple.
  1956. """
  1957. presubs = self.get_param('presubs',params)
  1958. postsubs = self.get_param('postsubs',params)
  1959. return (presubs,postsubs)
  1960. def dump(self):
  1961. """Write block definition to stdout."""
  1962. write = lambda s: sys.stdout.write('%s%s' % (s,writer.newline))
  1963. write('['+self.name+']')
  1964. if self.is_conf_entry('delimiter'):
  1965. write('delimiter='+self.delimiter)
  1966. if self.template:
  1967. write('template='+self.template)
  1968. if self.options:
  1969. write('options='+','.join(self.options))
  1970. if self.presubs:
  1971. if self.postsubs:
  1972. write('presubs='+','.join(self.presubs))
  1973. else:
  1974. write('subs='+','.join(self.presubs))
  1975. if self.postsubs:
  1976. write('postsubs='+','.join(self.postsubs))
  1977. if self.filter:
  1978. write('filter='+self.filter)
  1979. if self.posattrs:
  1980. write('posattrs='+','.join(self.posattrs))
  1981. if self.style:
  1982. write('style='+self.style)
  1983. if self.styles:
  1984. for style,d in self.styles.items():
  1985. s = ''
  1986. for k,v in d.items(): s += '%s=%r,' % (k,v)
  1987. write('%s-style=%s' % (style,s[:-1]))
  1988. def validate(self):
  1989. """Validate block after the complete configuration has been loaded."""
  1990. if self.is_conf_entry('delimiter') and not self.delimiter:
  1991. raise EAsciiDoc,'[%s] missing delimiter' % self.name
  1992. if self.style:
  1993. if not is_name(self.style):
  1994. raise EAsciiDoc, 'illegal style name: %s' % self.style
  1995. if not self.style in self.styles:
  1996. if not isinstance(self,List): # Lists don't have templates.
  1997. message.warning('[%s] \'%s\' style not in %s' % (
  1998. self.name,self.style,self.styles.keys()))
  1999. # Check all styles for missing templates.
  2000. all_styles_have_template = True
  2001. for k,v in self.styles.items():
  2002. t = v.get('template')
  2003. if t and not t in config.sections:
  2004. message.warning('[%s] missing template section' % t)
  2005. if not t:
  2006. all_styles_have_template = False
  2007. # Check we have a valid template entry or alternatively that all the
  2008. # styles have templates.
  2009. if self.is_conf_entry('template') and not 'skip' in self.options:
  2010. if self.template:
  2011. if not self.template in config.sections:
  2012. message.warning('[%s] missing template section' % self.template)
  2013. elif not all_styles_have_template:
  2014. if not isinstance(self,List): # Lists don't have templates.
  2015. message.warning('[%s] styles missing templates' % self.name)
  2016. def isnext(self):
  2017. """Check if this block is next in document reader."""
  2018. result = False
  2019. reader.skip_blank_lines()
  2020. if reader.read_next():
  2021. if not self.delimiter_reo:
  2022. # Cache compiled delimiter optimization.
  2023. self.delimiter_reo = re.compile(self.delimiter)
  2024. mo = self.delimiter_reo.match(reader.read_next())
  2025. if mo:
  2026. self.mo = mo
  2027. result = True
  2028. return result
  2029. def translate(self):
  2030. """Translate block from document reader."""
  2031. if not self.presubs:
  2032. self.presubs = config.subsnormal
  2033. if reader.cursor:
  2034. self.start = reader.cursor[:]
  2035. def merge_attributes(self,attrs,params=[]):
  2036. """
  2037. Use the current blocks attribute list (attrs dictionary) to build a
  2038. dictionary of block processing parameters (self.parameters) and tag
  2039. substitution attributes (self.attributes).
  2040. 1. Copy the default parameters (self.*) to self.parameters.
  2041. self.parameters are used internally to render the current block.
  2042. Optional params array of addtional parameters.
  2043. 2. Copy attrs to self.attributes. self.attributes are used for template
  2044. and tag substitution in the current block.
  2045. 3. If a style attribute was specified update self.parameters with the
  2046. corresponding style parameters; if there are any style parameters
  2047. remaining add them to self.attributes (existing attribute list entries
  2048. take precedence).
  2049. 4. Set named positional attributes in self.attributes if self.posattrs
  2050. was specified.
  2051. 5. Finally self.parameters is updated with any corresponding parameters
  2052. specified in attrs.
  2053. """
  2054. def check_array_parameter(param):
  2055. # Check the parameter is a sequence type.
  2056. if not is_array(self.parameters[param]):
  2057. message.error('malformed presubs attribute: %s' %
  2058. self.parameters[param])
  2059. # Revert to default value.
  2060. self.parameters[param] = getattr(self,param)
  2061. params = list(self.PARAM_NAMES) + params
  2062. self.attributes = {}
  2063. if self.style:
  2064. # If a default style is defined make it available in the template.
  2065. self.attributes['style'] = self.style
  2066. self.attributes.update(attrs)
  2067. # Calculate dynamic block parameters.
  2068. # Start with configuration file defaults.
  2069. self.parameters = AttrDict()
  2070. for name in params:
  2071. self.parameters[name] = getattr(self,name)
  2072. # Load the selected style attributes.
  2073. posattrs = self.posattrs
  2074. if posattrs and posattrs[0] == 'style':
  2075. style = self.attributes.get('1')
  2076. else:
  2077. style = None
  2078. if not style:
  2079. style = self.attributes.get('style',self.style)
  2080. if style:
  2081. if not is_name(style):
  2082. raise EAsciiDoc, 'illegal style name: %s' % style
  2083. if style in self.styles:
  2084. self.attributes['style'] = style
  2085. for k,v in self.styles[style].items():
  2086. if k == 'posattrs':
  2087. posattrs = v
  2088. elif k in params:
  2089. self.parameters[k] = v
  2090. elif not k in self.attributes:
  2091. # Style attributes don't take precedence over explicit.
  2092. self.attributes[k] = v
  2093. # Set named positional attributes.
  2094. for i,v in enumerate(posattrs):
  2095. if str(i+1) in self.attributes:
  2096. self.attributes[v] = self.attributes[str(i+1)]
  2097. # Override config and style attributes with attribute list attributes.
  2098. self.update_parameters(attrs)
  2099. check_array_parameter('options')
  2100. check_array_parameter('presubs')
  2101. check_array_parameter('postsubs')
  2102. class AbstractBlocks:
  2103. """List of block definitions."""
  2104. PREFIX = '' # Conf file section name prefix set in derived classes.
  2105. BLOCK_TYPE = None # Block type set in derived classes.
  2106. def __init__(self):
  2107. self.current=None
  2108. self.blocks = [] # List of Block objects.
  2109. self.default = None # Default Block.
  2110. self.delimiters = None # Combined delimiters regular expression.
  2111. def load(self,sections):
  2112. """Load block definition from 'sections' dictionary."""
  2113. for k in sections.keys():
  2114. if re.match(r'^'+ self.PREFIX + r'.+$',k):
  2115. d = {}
  2116. parse_entries(sections.get(k,()),d)
  2117. for b in self.blocks:
  2118. if b.name == k:
  2119. break
  2120. else:
  2121. b = self.BLOCK_TYPE()
  2122. self.blocks.append(b)
  2123. try:
  2124. b.load(k,d)
  2125. except EAsciiDoc,e:
  2126. raise EAsciiDoc,'[%s] %s' % (k,str(e))
  2127. def dump(self):
  2128. for b in self.blocks:
  2129. b.dump()
  2130. def isnext(self):
  2131. for b in self.blocks:
  2132. if b.isnext():
  2133. self.current = b
  2134. return True;
  2135. return False
  2136. def validate(self):
  2137. """Validate the block definitions."""
  2138. # Validate delimiters and build combined lists delimiter pattern.
  2139. delimiters = []
  2140. for b in self.blocks:
  2141. assert b.__class__ is self.BLOCK_TYPE
  2142. b.validate()
  2143. if b.delimiter:
  2144. delimiters.append(b.delimiter)
  2145. self.delimiters = re_join(delimiters)
  2146. class Paragraph(AbstractBlock):
  2147. def __init__(self):
  2148. AbstractBlock.__init__(self)
  2149. self.text=None # Text in first line of paragraph.
  2150. def load(self,name,entries):
  2151. AbstractBlock.load(self,name,entries)
  2152. def dump(self):
  2153. AbstractBlock.dump(self)
  2154. write = lambda s: sys.stdout.write('%s%s' % (s,writer.newline))
  2155. write('')
  2156. def isnext(self):
  2157. result = AbstractBlock.isnext(self)
  2158. if result:
  2159. self.text = self.mo.groupdict().get('text')
  2160. return result
  2161. def translate(self):
  2162. AbstractBlock.translate(self)
  2163. attrs = self.mo.groupdict().copy()
  2164. if 'text' in attrs: del attrs['text']
  2165. BlockTitle.consume(attrs)
  2166. AttributeList.consume(attrs)
  2167. self.merge_attributes(attrs)
  2168. reader.read() # Discard (already parsed item first line).
  2169. body = reader.read_until(paragraphs.terminators)
  2170. body = [self.text] + list(body)
  2171. presubs = self.parameters.presubs
  2172. postsubs = self.parameters.postsubs
  2173. body = Lex.set_margin(body) # Move body to left margin.
  2174. body = Lex.subs(body,presubs)
  2175. if self.parameters.filter:
  2176. body = filter_lines(self.parameters.filter,body,self.attributes)
  2177. body = Lex.subs(body,postsubs)
  2178. template = self.parameters.template
  2179. stag,etag = config.section2tags(template, self.attributes)
  2180. # Write start tag, content, end tag.
  2181. writer.write(dovetail_tags(stag,body,etag),trace='paragraph')
  2182. class Paragraphs(AbstractBlocks):
  2183. """List of paragraph definitions."""
  2184. BLOCK_TYPE = Paragraph
  2185. PREFIX = 'paradef-'
  2186. def __init__(self):
  2187. AbstractBlocks.__init__(self)
  2188. self.terminators=None # List of compiled re's.
  2189. def initialize(self):
  2190. self.terminators = [
  2191. re.compile(r'^\+$|^$'),
  2192. re.compile(AttributeList.pattern),
  2193. re.compile(blocks.delimiters),
  2194. re.compile(tables.delimiters),
  2195. re.compile(tables_OLD.delimiters),
  2196. ]
  2197. def load(self,sections):
  2198. AbstractBlocks.load(self,sections)
  2199. def validate(self):
  2200. AbstractBlocks.validate(self)
  2201. # Check we have a default paragraph definition, put it last in list.
  2202. for b in self.blocks:
  2203. if b.name == 'paradef-default':
  2204. self.blocks.append(b)
  2205. self.default = b
  2206. self.blocks.remove(b)
  2207. break
  2208. else:
  2209. raise EAsciiDoc,'missing [paradef-default] section'
  2210. class List(AbstractBlock):
  2211. NUMBER_STYLES= ('arabic','loweralpha','upperalpha','lowerroman',
  2212. 'upperroman')
  2213. def __init__(self):
  2214. AbstractBlock.__init__(self)
  2215. self.CONF_ENTRIES += ('type','tags')
  2216. self.PARAM_NAMES += ('tags',)
  2217. # tabledef conf file parameters.
  2218. self.type=None
  2219. self.tags=None # Name of listtags-<tags> conf section.
  2220. # Calculated parameters.
  2221. self.tag=None # Current tags AttrDict.
  2222. self.label=None # List item label (labeled lists).
  2223. self.text=None # Text in first line of list item.
  2224. self.index=None # Matched delimiter 'index' group (numbered lists).
  2225. self.type=None # List type ('numbered','bulleted','labeled').
  2226. self.ordinal=None # Current list item ordinal number (1..)
  2227. self.number_style=None # Current numbered list style ('arabic'..)
  2228. def load(self,name,entries):
  2229. AbstractBlock.load(self,name,entries)
  2230. def dump(self):
  2231. AbstractBlock.dump(self)
  2232. write = lambda s: sys.stdout.write('%s%s' % (s,writer.newline))
  2233. write('type='+self.type)
  2234. write('tags='+self.tags)
  2235. write('')
  2236. def validate(self):
  2237. AbstractBlock.validate(self)
  2238. tags = [self.tags]
  2239. tags += [s['tags'] for s in self.styles.values() if 'tags' in s]
  2240. for t in tags:
  2241. if t not in lists.tags:
  2242. self.error('missing section: [listtags-%s]' % t,halt=True)
  2243. def isnext(self):
  2244. result = AbstractBlock.isnext(self)
  2245. if result:
  2246. self.label = self.mo.groupdict().get('label')
  2247. self.text = self.mo.groupdict().get('text')
  2248. self.index = self.mo.groupdict().get('index')
  2249. return result
  2250. def translate_entry(self):
  2251. assert self.type == 'labeled'
  2252. entrytag = subs_tag(self.tag.entry, self.attributes)
  2253. labeltag = subs_tag(self.tag.label, self.attributes)
  2254. writer.write(entrytag[0],trace='list entry open')
  2255. writer.write(labeltag[0],trace='list label open')
  2256. # Write labels.
  2257. while Lex.next() is self:
  2258. reader.read() # Discard (already parsed item first line).
  2259. writer.write_tag(self.tag.term, [self.label],
  2260. self.presubs, self.attributes,trace='list term')
  2261. if self.text: break
  2262. writer.write(labeltag[1],trace='list label close')
  2263. # Write item text.
  2264. self.translate_item()
  2265. writer.write(entrytag[1],trace='list entry close')
  2266. def translate_item(self):
  2267. if self.type == 'callout':
  2268. self.attributes['coids'] = calloutmap.calloutids(self.ordinal)
  2269. itemtag = subs_tag(self.tag.item, self.attributes)
  2270. writer.write(itemtag[0],trace='list item open')
  2271. # Write ItemText.
  2272. text = reader.read_until(lists.terminators)
  2273. if self.text:
  2274. text = [self.text] + list(text)
  2275. if text:
  2276. writer.write_tag(self.tag.text, text, self.presubs, self.attributes,trace='list text')
  2277. # Process explicit and implicit list item continuations.
  2278. while True:
  2279. continuation = reader.read_next() == '+'
  2280. if continuation: reader.read() # Discard continuation line.
  2281. while Lex.next() in (BlockTitle,AttributeList):
  2282. # Consume continued element title and attributes.
  2283. Lex.next().translate()
  2284. if not continuation and BlockTitle.title:
  2285. # Titled elements terminate the list.
  2286. break
  2287. next = Lex.next()
  2288. if next in lists.open:
  2289. break
  2290. elif isinstance(next,List):
  2291. next.translate()
  2292. elif isinstance(next,Paragraph) and 'listelement' in next.options:
  2293. next.translate()
  2294. elif continuation:
  2295. # This is where continued elements are processed.
  2296. if next is Title:
  2297. message.error('section title not allowed in list item',halt=True)
  2298. next.translate()
  2299. else:
  2300. break
  2301. writer.write(itemtag[1],trace='list item close')
  2302. @staticmethod
  2303. def calc_style(index):
  2304. """Return the numbered list style ('arabic'...) of the list item index.
  2305. Return None if unrecognized style."""
  2306. if re.match(r'^\d+[\.>]$', index):
  2307. style = 'arabic'
  2308. elif re.match(r'^[ivx]+\)$', index):
  2309. style = 'lowerroman'
  2310. elif re.match(r'^[IVX]+\)$', index):
  2311. style = 'upperroman'
  2312. elif re.match(r'^[a-z]\.$', index):
  2313. style = 'loweralpha'
  2314. elif re.match(r'^[A-Z]\.$', index):
  2315. style = 'upperalpha'
  2316. else:
  2317. assert False
  2318. return style
  2319. @staticmethod
  2320. def calc_index(index,style):
  2321. """Return the ordinal number of (1...) of the list item index
  2322. for the given list style."""
  2323. def roman_to_int(roman):
  2324. roman = roman.lower()
  2325. digits = {'i':1,'v':5,'x':10}
  2326. result = 0
  2327. for i in range(len(roman)):
  2328. digit = digits[roman[i]]
  2329. # If next digit is larger this digit is negative.
  2330. if i+1 < len(roman) and digits[roman[i+1]] > digit:
  2331. result -= digit
  2332. else:
  2333. result += digit
  2334. return result
  2335. index = index[:-1]
  2336. if style == 'arabic':
  2337. ordinal = int(index)
  2338. elif style == 'lowerroman':
  2339. ordinal = roman_to_int(index)
  2340. elif style == 'upperroman':
  2341. ordinal = roman_to_int(index)
  2342. elif style == 'loweralpha':
  2343. ordinal = ord(index) - ord('a') + 1
  2344. elif style == 'upperalpha':
  2345. ordinal = ord(index) - ord('A') + 1
  2346. else:
  2347. assert False
  2348. return ordinal
  2349. def check_index(self):
  2350. """Check calculated self.ordinal (1,2,...) against the item number
  2351. in the document (self.index) and check the number style is the same as
  2352. the first item (self.number_style)."""
  2353. assert self.type in ('numbered','callout')
  2354. if self.index:
  2355. style = self.calc_style(self.index)
  2356. if style != self.number_style:
  2357. message.warning('list item style: expected %s got %s' %
  2358. (self.number_style,style), offset=1)
  2359. ordinal = self.calc_index(self.index,style)
  2360. if ordinal != self.ordinal:
  2361. message.warning('list item index: expected %s got %s' %
  2362. (self.ordinal,ordinal), offset=1)
  2363. def check_tags(self):
  2364. """ Check that all necessary tags are present. """
  2365. tags = set(Lists.TAGS)
  2366. if self.type != 'labeled':
  2367. tags = tags.difference(['entry','label','term'])
  2368. missing = tags.difference(self.tag.keys())
  2369. if missing:
  2370. self.error('missing tag(s): %s' % ','.join(missing), halt=True)
  2371. def translate(self):
  2372. AbstractBlock.translate(self)
  2373. if self.short_name() in ('bibliography','glossary','qanda'):
  2374. message.deprecated('old %s list syntax' % self.short_name())
  2375. lists.open.append(self)
  2376. attrs = self.mo.groupdict().copy()
  2377. for k in ('label','text','index'):
  2378. if k in attrs: del attrs[k]
  2379. if self.index:
  2380. # Set the numbering style from first list item.
  2381. attrs['style'] = self.calc_style(self.index)
  2382. BlockTitle.consume(attrs)
  2383. AttributeList.consume(attrs)
  2384. self.merge_attributes(attrs,['tags'])
  2385. if self.type in ('numbered','callout'):
  2386. self.number_style = self.attributes.get('style')
  2387. if self.number_style not in self.NUMBER_STYLES:
  2388. message.error('illegal numbered list style: %s' % self.number_style)
  2389. # Fall back to default style.
  2390. self.attributes['style'] = self.number_style = self.style
  2391. self.tag = lists.tags[self.parameters.tags]
  2392. self.check_tags()
  2393. if 'width' in self.attributes:
  2394. # Set horizontal list 'labelwidth' and 'itemwidth' attributes.
  2395. v = str(self.attributes['width'])
  2396. mo = re.match(r'^(\d{1,2})%?$',v)
  2397. if mo:
  2398. labelwidth = int(mo.group(1))
  2399. self.attributes['labelwidth'] = str(labelwidth)
  2400. self.attributes['itemwidth'] = str(100-labelwidth)
  2401. else:
  2402. self.error('illegal attribute value: width="%s"' % v)
  2403. stag,etag = subs_tag(self.tag.list, self.attributes)
  2404. if stag:
  2405. writer.write(stag,trace='list open')
  2406. self.ordinal = 0
  2407. # Process list till list syntax changes or there is a new title.
  2408. while Lex.next() is self and not BlockTitle.title:
  2409. self.ordinal += 1
  2410. document.attributes['listindex'] = str(self.ordinal)
  2411. if self.type in ('numbered','callout'):
  2412. self.check_index()
  2413. if self.type in ('bulleted','numbered','callout'):
  2414. reader.read() # Discard (already parsed item first line).
  2415. self.translate_item()
  2416. elif self.type == 'labeled':
  2417. self.translate_entry()
  2418. else:
  2419. raise AssertionError,'illegal [%s] list type' % self.name
  2420. if etag:
  2421. writer.write(etag,trace='list close')
  2422. if self.type == 'callout':
  2423. calloutmap.validate(self.ordinal)
  2424. calloutmap.listclose()
  2425. lists.open.pop()
  2426. if len(lists.open):
  2427. document.attributes['listindex'] = str(lists.open[-1].ordinal)
  2428. class Lists(AbstractBlocks):
  2429. """List of List objects."""
  2430. BLOCK_TYPE = List
  2431. PREFIX = 'listdef-'
  2432. TYPES = ('bulleted','numbered','labeled','callout')
  2433. TAGS = ('list', 'entry','item','text', 'label','term')
  2434. def __init__(self):
  2435. AbstractBlocks.__init__(self)
  2436. self.open = [] # A stack of the current and parent lists.
  2437. self.tags={} # List tags dictionary. Each entry is a tags AttrDict.
  2438. self.terminators=None # List of compiled re's.
  2439. def initialize(self):
  2440. self.terminators = [
  2441. re.compile(r'^\+$|^$'),
  2442. re.compile(AttributeList.pattern),
  2443. re.compile(lists.delimiters),
  2444. re.compile(blocks.delimiters),
  2445. re.compile(tables.delimiters),
  2446. re.compile(tables_OLD.delimiters),
  2447. ]
  2448. def load(self,sections):
  2449. AbstractBlocks.load(self,sections)
  2450. self.load_tags(sections)
  2451. def load_tags(self,sections):
  2452. """
  2453. Load listtags-* conf file sections to self.tags.
  2454. """
  2455. for section in sections.keys():
  2456. mo = re.match(r'^listtags-(?P<name>\w+)$',section)
  2457. if mo:
  2458. name = mo.group('name')
  2459. if name in self.tags:
  2460. d = self.tags[name]
  2461. else:
  2462. d = AttrDict()
  2463. parse_entries(sections.get(section,()),d)
  2464. for k in d.keys():
  2465. if k not in self.TAGS:
  2466. message.warning('[%s] contains illegal list tag: %s' %
  2467. (section,k))
  2468. self.tags[name] = d
  2469. def validate(self):
  2470. AbstractBlocks.validate(self)
  2471. for b in self.blocks:
  2472. # Check list has valid type.
  2473. if not b.type in Lists.TYPES:
  2474. raise EAsciiDoc,'[%s] illegal type' % b.name
  2475. b.validate()
  2476. def dump(self):
  2477. AbstractBlocks.dump(self)
  2478. for k,v in self.tags.items():
  2479. dump_section('listtags-'+k, v)
  2480. class DelimitedBlock(AbstractBlock):
  2481. def __init__(self):
  2482. AbstractBlock.__init__(self)
  2483. def load(self,name,entries):
  2484. AbstractBlock.load(self,name,entries)
  2485. def dump(self):
  2486. AbstractBlock.dump(self)
  2487. write = lambda s: sys.stdout.write('%s%s' % (s,writer.newline))
  2488. write('')
  2489. def isnext(self):
  2490. return AbstractBlock.isnext(self)
  2491. def translate(self):
  2492. AbstractBlock.translate(self)
  2493. reader.read() # Discard delimiter.
  2494. attrs = {}
  2495. BlockTitle.consume(attrs)
  2496. AttributeList.consume(attrs)
  2497. self.merge_attributes(attrs)
  2498. options = self.parameters.options
  2499. if 'skip' in options:
  2500. reader.read_until(self.delimiter,same_file=True)
  2501. elif safe() and self.name == 'blockdef-backend':
  2502. message.unsafe('Backend Block')
  2503. reader.read_until(self.delimiter,same_file=True)
  2504. else:
  2505. template = self.parameters.template
  2506. stag,etag = config.section2tags(template,self.attributes)
  2507. name = self.short_name()+' block'
  2508. if 'sectionbody' in options:
  2509. # The body is treated like a section body.
  2510. writer.write(stag,trace=name+' open')
  2511. Section.translate_body(self)
  2512. writer.write(etag,trace=name+' close')
  2513. else:
  2514. body = reader.read_until(self.delimiter,same_file=True)
  2515. presubs = self.parameters.presubs
  2516. postsubs = self.parameters.postsubs
  2517. body = Lex.subs(body,presubs)
  2518. if self.parameters.filter:
  2519. body = filter_lines(self.parameters.filter,body,self.attributes)
  2520. body = Lex.subs(body,postsubs)
  2521. # Write start tag, content, end tag.
  2522. writer.write(dovetail_tags(stag,body,etag),trace=name)
  2523. trace(self.short_name()+' block close',etag)
  2524. if reader.eof():
  2525. self.error('missing closing delimiter',self.start)
  2526. else:
  2527. delimiter = reader.read() # Discard delimiter line.
  2528. assert re.match(self.delimiter,delimiter)
  2529. class DelimitedBlocks(AbstractBlocks):
  2530. """List of delimited blocks."""
  2531. BLOCK_TYPE = DelimitedBlock
  2532. PREFIX = 'blockdef-'
  2533. def __init__(self):
  2534. AbstractBlocks.__init__(self)
  2535. def load(self,sections):
  2536. """Update blocks defined in 'sections' dictionary."""
  2537. AbstractBlocks.load(self,sections)
  2538. def validate(self):
  2539. AbstractBlocks.validate(self)
  2540. class Column:
  2541. """Table column."""
  2542. def __init__(self, width=None, align_spec=None, style=None):
  2543. self.width = width or '1'
  2544. self.align, self.valign = Table.parse_align_spec(align_spec)
  2545. self.style = style # Style name or None.
  2546. # Calculated attribute values.
  2547. self.abswidth = None # 1.. (page units).
  2548. self.pcwidth = None # 1..99 (percentage).
  2549. class Cell:
  2550. def __init__(self, data, span_spec=None, align_spec=None, style=None):
  2551. self.data = data
  2552. self.span, self.vspan = Table.parse_span_spec(span_spec)
  2553. self.align, self.valign = Table.parse_align_spec(align_spec)
  2554. self.style = style
  2555. def __repr__(self):
  2556. return '<Cell: %d.%d %s.%s %s "%s">' % (
  2557. self.span, self.vspan,
  2558. self.align, self.valign,
  2559. self.style or '',
  2560. self.data)
  2561. class Table(AbstractBlock):
  2562. ALIGN = {'<':'left', '>':'right', '^':'center'}
  2563. VALIGN = {'<':'top', '>':'bottom', '^':'middle'}
  2564. FORMATS = ('psv','csv','dsv')
  2565. SEPARATORS = dict(
  2566. csv=',',
  2567. dsv=r':|\n',
  2568. # The count and align group matches are not exact.
  2569. psv=r'((?<!\S)((?P<span>[\d.]+)(?P<op>[*+]))?(?P<align>[<\^>.]{,3})?(?P<style>[a-z])?)?\|'
  2570. )
  2571. def __init__(self):
  2572. AbstractBlock.__init__(self)
  2573. self.CONF_ENTRIES += ('format','tags','separator')
  2574. # tabledef conf file parameters.
  2575. self.format='psv'
  2576. self.separator=None
  2577. self.tags=None # Name of tabletags-<tags> conf section.
  2578. # Calculated parameters.
  2579. self.abswidth=None # 1.. (page units).
  2580. self.pcwidth = None # 1..99 (percentage).
  2581. self.rows=[] # Parsed rows, each row is a list of Cells.
  2582. self.columns=[] # List of Columns.
  2583. @staticmethod
  2584. def parse_align_spec(align_spec):
  2585. """
  2586. Parse AsciiDoc cell alignment specifier and return 2-tuple with
  2587. horizonatal and vertical alignment names. Unspecified alignments
  2588. set to None.
  2589. """
  2590. result = (None, None)
  2591. if align_spec:
  2592. mo = re.match(r'^([<\^>])?(\.([<\^>]))?$', align_spec)
  2593. if mo:
  2594. result = (Table.ALIGN.get(mo.group(1)),
  2595. Table.VALIGN.get(mo.group(3)))
  2596. return result
  2597. @staticmethod
  2598. def parse_span_spec(span_spec):
  2599. """
  2600. Parse AsciiDoc cell span specifier and return 2-tuple with horizonatal
  2601. and vertical span counts. Set default values (1,1) if not
  2602. specified.
  2603. """
  2604. result = (None, None)
  2605. if span_spec:
  2606. mo = re.match(r'^(\d+)?(\.(\d+))?$', span_spec)
  2607. if mo:
  2608. result = (mo.group(1) and int(mo.group(1)),
  2609. mo.group(3) and int(mo.group(3)))
  2610. return (result[0] or 1, result[1] or 1)
  2611. def load(self,name,entries):
  2612. AbstractBlock.load(self,name,entries)
  2613. def dump(self):
  2614. AbstractBlock.dump(self)
  2615. write = lambda s: sys.stdout.write('%s%s' % (s,writer.newline))
  2616. write('format='+self.format)
  2617. write('')
  2618. def validate(self):
  2619. AbstractBlock.validate(self)
  2620. if self.format not in Table.FORMATS:
  2621. self.error('illegal format=%s' % self.format,halt=True)
  2622. self.tags = self.tags or 'default'
  2623. tags = [self.tags]
  2624. tags += [s['tags'] for s in self.styles.values() if 'tags' in s]
  2625. for t in tags:
  2626. if t not in tables.tags:
  2627. self.error('missing section: [tabletags-%s]' % t,halt=True)
  2628. if self.separator:
  2629. # Evaluate escape characters.
  2630. self.separator = eval('"'+self.separator+'"')
  2631. #TODO: Move to class Tables
  2632. # Check global table parameters.
  2633. elif config.pagewidth is None:
  2634. self.error('missing [miscellaneous] entry: pagewidth')
  2635. elif config.pageunits is None:
  2636. self.error('missing [miscellaneous] entry: pageunits')
  2637. def validate_attributes(self):
  2638. """Validate and parse table attributes."""
  2639. # Set defaults.
  2640. format = self.format
  2641. tags = self.tags
  2642. separator = self.separator
  2643. abswidth = float(config.pagewidth)
  2644. pcwidth = 100.0
  2645. for k,v in self.attributes.items():
  2646. if k == 'format':
  2647. if v not in self.FORMATS:
  2648. self.error('illegal %s=%s' % (k,v))
  2649. else:
  2650. format = v
  2651. elif k == 'tags':
  2652. if v not in tables.tags:
  2653. self.error('illegal %s=%s' % (k,v))
  2654. else:
  2655. tags = v
  2656. elif k == 'separator':
  2657. separator = v
  2658. elif k == 'width':
  2659. if not re.match(r'^\d{1,3}%$',v) or int(v[:-1]) > 100:
  2660. self.error('illegal %s=%s' % (k,v))
  2661. else:
  2662. abswidth = float(v[:-1])/100 * config.pagewidth
  2663. pcwidth = float(v[:-1])
  2664. # Calculate separator if it has not been specified.
  2665. if not separator:
  2666. separator = Table.SEPARATORS[format]
  2667. if format == 'csv':
  2668. if len(separator) > 1:
  2669. self.error('illegal csv separator=%s' % separator)
  2670. separator = ','
  2671. else:
  2672. if not is_re(separator):
  2673. self.error('illegal regular expression: separator=%s' %
  2674. separator)
  2675. self.parameters.format = format
  2676. self.parameters.tags = tags
  2677. self.parameters.separator = separator
  2678. self.abswidth = abswidth
  2679. self.pcwidth = pcwidth
  2680. def get_tags(self,params):
  2681. tags = self.get_param('tags',params)
  2682. assert(tags and tags in tables.tags)
  2683. return tables.tags[tags]
  2684. def get_style(self,prefix):
  2685. """
  2686. Return the style dictionary whose name starts with 'prefix'.
  2687. """
  2688. if prefix is None:
  2689. return None
  2690. names = self.styles.keys()
  2691. names.sort()
  2692. for name in names:
  2693. if name.startswith(prefix):
  2694. return self.styles[name]
  2695. else:
  2696. self.error('missing style: %s*' % prefix)
  2697. return None
  2698. def parse_cols(self, cols, align, valign):
  2699. """
  2700. Build list of column objects from table 'cols', 'align' and 'valign'
  2701. attributes.
  2702. """
  2703. # [<multiplier>*][<align>][<width>][<style>]
  2704. COLS_RE1 = r'^((?P<count>\d+)\*)?(?P<align>[<\^>.]{,3})?(?P<width>\d+%?)?(?P<style>[a-z]\w*)?$'
  2705. # [<multiplier>*][<width>][<align>][<style>]
  2706. COLS_RE2 = r'^((?P<count>\d+)\*)?(?P<width>\d+%?)?(?P<align>[<\^>.]{,3})?(?P<style>[a-z]\w*)?$'
  2707. reo1 = re.compile(COLS_RE1)
  2708. reo2 = re.compile(COLS_RE2)
  2709. cols = str(cols)
  2710. if re.match(r'^\d+$',cols):
  2711. for i in range(int(cols)):
  2712. self.columns.append(Column())
  2713. else:
  2714. for col in re.split(r'\s*,\s*',cols):
  2715. mo = reo1.match(col)
  2716. if not mo:
  2717. mo = reo2.match(col)
  2718. if mo:
  2719. count = int(mo.groupdict().get('count') or 1)
  2720. for i in range(count):
  2721. self.columns.append(
  2722. Column(mo.group('width'), mo.group('align'),
  2723. self.get_style(mo.group('style')))
  2724. )
  2725. else:
  2726. self.error('illegal column spec: %s' % col,self.start)
  2727. # Set column (and indirectly cell) default alignments.
  2728. for col in self.columns:
  2729. col.align = col.align or align or 'left'
  2730. col.valign = col.valign or valign or 'top'
  2731. # Validate widths and calculate missing widths.
  2732. n = 0; percents = 0; props = 0
  2733. for col in self.columns:
  2734. if col.width:
  2735. if col.width[-1] == '%': percents += int(col.width[:-1])
  2736. else: props += int(col.width)
  2737. n += 1
  2738. if percents > 0 and props > 0:
  2739. self.error('mixed percent and proportional widths: %s'
  2740. % cols,self.start)
  2741. pcunits = percents > 0
  2742. # Fill in missing widths.
  2743. if n < len(self.columns) and percents < 100:
  2744. if pcunits:
  2745. width = float(100 - percents)/float(len(self.columns) - n)
  2746. else:
  2747. width = 1
  2748. for col in self.columns:
  2749. if not col.width:
  2750. if pcunits:
  2751. col.width = str(int(width))+'%'
  2752. percents += width
  2753. else:
  2754. col.width = str(width)
  2755. props += width
  2756. # Calculate column alignment and absolute and percent width values.
  2757. percents = 0
  2758. for col in self.columns:
  2759. if pcunits:
  2760. col.pcwidth = float(col.width[:-1])
  2761. else:
  2762. col.pcwidth = (float(col.width)/props)*100
  2763. col.abswidth = int(self.abswidth * (col.pcwidth/100))
  2764. percents += col.pcwidth
  2765. col.pcwidth = int(col.pcwidth)
  2766. if round(percents) > 100:
  2767. self.error('total width exceeds 100%%: %s' % cols,self.start)
  2768. elif round(percents) < 100:
  2769. self.error('total width less than 100%%: %s' % cols,self.start)
  2770. def build_colspecs(self):
  2771. """
  2772. Generate column related substitution attributes.
  2773. """
  2774. cols = []
  2775. i = 1
  2776. for col in self.columns:
  2777. colspec = self.get_tags(col.style).colspec
  2778. if colspec:
  2779. self.attributes['align'] = col.align
  2780. self.attributes['valign'] = col.valign
  2781. self.attributes['colabswidth'] = col.abswidth
  2782. self.attributes['colpcwidth'] = col.pcwidth
  2783. self.attributes['colnumber'] = str(i)
  2784. s = subs_attrs(colspec, self.attributes)
  2785. if not s:
  2786. message.warning('colspec dropped: contains undefined attribute')
  2787. else:
  2788. cols.append(s)
  2789. i += 1
  2790. if cols:
  2791. self.attributes['colspecs'] = writer.newline.join(cols)
  2792. def parse_rows(self, text):
  2793. """
  2794. Parse the table source text into self.rows (a list of rows, each row
  2795. is a list of Cells.
  2796. """
  2797. reserved = {} # Cols reserved by rowspans (indexed by row number).
  2798. if self.parameters.format in ('psv','dsv'):
  2799. ri = 0 # Current row index 0..
  2800. cells = self.parse_psv_dsv(text)
  2801. row = []
  2802. ci = 0 # Column counter 0..colcount
  2803. for cell in cells:
  2804. colcount = len(self.columns) - reserved.get(ri,0)
  2805. if cell.vspan > 1:
  2806. # Reserve spanned columns from ensuing rows.
  2807. for i in range(1, cell.vspan):
  2808. reserved[ri+i] = reserved.get(ri+i, 0) + cell.span
  2809. ci += cell.span
  2810. if ci <= colcount:
  2811. row.append(cell)
  2812. if ci >= colcount:
  2813. self.rows.append(row)
  2814. ri += 1
  2815. row = []
  2816. ci = 0
  2817. if ci > colcount:
  2818. message.warning('table row %d: span exceeds number of columns'
  2819. % ri)
  2820. elif self.parameters.format == 'csv':
  2821. self.rows = self.parse_csv(text)
  2822. else:
  2823. assert True,'illegal table format'
  2824. # Check that all row spans match.
  2825. for ri,row in enumerate(self.rows):
  2826. row_span = 0
  2827. for cell in row:
  2828. row_span += cell.span
  2829. row_span += reserved.get(ri,0)
  2830. if ri == 0:
  2831. header_span = row_span
  2832. if row_span < header_span:
  2833. message.warning('table row %d: does not span all columns' % (ri+1))
  2834. if row_span > header_span:
  2835. message.warning('table row %d: exceeds columns span' % (ri+1))
  2836. # Check that now row spans exceed the number of rows.
  2837. if len([x for x in reserved.keys() if x >= len(self.rows)]) > 0:
  2838. message.warning('one or more cell spans exceed the available rows')
  2839. def subs_rows(self, rows, rowtype='body'):
  2840. """
  2841. Return a string of output markup from a list of rows, each row
  2842. is a list of raw data text.
  2843. """
  2844. tags = tables.tags[self.parameters.tags]
  2845. if rowtype == 'header':
  2846. rtag = tags.headrow
  2847. elif rowtype == 'footer':
  2848. rtag = tags.footrow
  2849. else:
  2850. rtag = tags.bodyrow
  2851. result = []
  2852. stag,etag = subs_tag(rtag,self.attributes)
  2853. for row in rows:
  2854. result.append(stag)
  2855. result += self.subs_row(row,rowtype)
  2856. result.append(etag)
  2857. return writer.newline.join(result)
  2858. def subs_row(self, row, rowtype):
  2859. """
  2860. Substitute the list of Cells using the data tag.
  2861. Returns a list of marked up table cell elements.
  2862. """
  2863. result = []
  2864. i = 0
  2865. for cell in row:
  2866. col = self.columns[i]
  2867. self.attributes['align'] = cell.align or col.align
  2868. self.attributes['valign'] = cell.valign or col.valign
  2869. self.attributes['colabswidth'] = col.abswidth
  2870. self.attributes['colpcwidth'] = col.pcwidth
  2871. self.attributes['colnumber'] = str(i+1)
  2872. self.attributes['colspan'] = str(cell.span)
  2873. self.attributes['colstart'] = self.attributes['colnumber']
  2874. self.attributes['colend'] = str(i+cell.span)
  2875. self.attributes['rowspan'] = str(cell.vspan)
  2876. self.attributes['morerows'] = str(cell.vspan-1)
  2877. # Fill missing column data with blanks.
  2878. if i > len(self.columns) - 1:
  2879. data = ''
  2880. else:
  2881. data = cell.data
  2882. if rowtype == 'header':
  2883. # Use table style unless overriden by cell style.
  2884. colstyle = cell.style
  2885. else:
  2886. # If the cell style is not defined use the column style.
  2887. colstyle = cell.style or col.style
  2888. tags = self.get_tags(colstyle)
  2889. presubs,postsubs = self.get_subs(colstyle)
  2890. data = [data]
  2891. data = Lex.subs(data, presubs)
  2892. data = filter_lines(self.get_param('filter',colstyle),
  2893. data, self.attributes)
  2894. data = Lex.subs(data, postsubs)
  2895. if rowtype != 'header':
  2896. ptag = tags.paragraph
  2897. if ptag:
  2898. stag,etag = subs_tag(ptag,self.attributes)
  2899. text = '\n'.join(data).strip()
  2900. data = []
  2901. for para in re.split(r'\n{2,}',text):
  2902. data += dovetail_tags([stag],para.split('\n'),[etag])
  2903. if rowtype == 'header':
  2904. dtag = tags.headdata
  2905. elif rowtype == 'footer':
  2906. dtag = tags.footdata
  2907. else:
  2908. dtag = tags.bodydata
  2909. stag,etag = subs_tag(dtag,self.attributes)
  2910. result = result + dovetail_tags([stag],data,[etag])
  2911. i += cell.span
  2912. return result
  2913. def parse_csv(self,text):
  2914. """
  2915. Parse the table source text and return a list of rows, each row
  2916. is a list of Cells.
  2917. """
  2918. import StringIO
  2919. import csv
  2920. rows = []
  2921. rdr = csv.reader(StringIO.StringIO('\r\n'.join(text)),
  2922. delimiter=self.parameters.separator, skipinitialspace=True)
  2923. try:
  2924. for row in rdr:
  2925. rows.append([Cell(data) for data in row])
  2926. except Exception:
  2927. self.error('csv parse error: %s' % row)
  2928. return rows
  2929. def parse_psv_dsv(self,text):
  2930. """
  2931. Parse list of PSV or DSV table source text lines and return a list of
  2932. Cells.
  2933. """
  2934. def append_cell(data, span_spec, op, align_spec, style):
  2935. op = op or '+'
  2936. if op == '*': # Cell multiplier.
  2937. span = Table.parse_span_spec(span_spec)[0]
  2938. for i in range(span):
  2939. cells.append(Cell(data, '1', align_spec, style))
  2940. elif op == '+': # Column spanner.
  2941. cells.append(Cell(data, span_spec, align_spec, style))
  2942. else:
  2943. self.error('illegal table cell operator')
  2944. text = '\n'.join(text)
  2945. separator = '(?msu)'+self.parameters.separator
  2946. format = self.parameters.format
  2947. start = 0
  2948. span = None
  2949. op = None
  2950. align = None
  2951. style = None
  2952. cells = []
  2953. data = ''
  2954. for mo in re.finditer(separator,text):
  2955. data += text[start:mo.start()]
  2956. if data.endswith('\\'):
  2957. data = data[:-1]+mo.group() # Reinstate escaped separators.
  2958. else:
  2959. append_cell(data, span, op, align, style)
  2960. span = mo.groupdict().get('span')
  2961. op = mo.groupdict().get('op')
  2962. align = mo.groupdict().get('align')
  2963. style = mo.groupdict().get('style')
  2964. if style:
  2965. style = self.get_style(style)
  2966. data = ''
  2967. start = mo.end()
  2968. # Last cell follows final separator.
  2969. data += text[start:]
  2970. append_cell(data, span, op, align, style)
  2971. # We expect a dummy blank item preceeding first PSV cell.
  2972. if format == 'psv':
  2973. if cells[0].data.strip() != '':
  2974. self.error('missing leading separator: %s' % separator,
  2975. self.start)
  2976. else:
  2977. cells.pop(0)
  2978. return cells
  2979. def translate(self):
  2980. AbstractBlock.translate(self)
  2981. reader.read() # Discard delimiter.
  2982. # Reset instance specific properties.
  2983. self.columns = []
  2984. self.rows = []
  2985. attrs = {}
  2986. BlockTitle.consume(attrs)
  2987. # Mix in document attribute list.
  2988. AttributeList.consume(attrs)
  2989. self.merge_attributes(attrs)
  2990. self.validate_attributes()
  2991. # Add global and calculated configuration parameters.
  2992. self.attributes['pagewidth'] = config.pagewidth
  2993. self.attributes['pageunits'] = config.pageunits
  2994. self.attributes['tableabswidth'] = int(self.abswidth)
  2995. self.attributes['tablepcwidth'] = int(self.pcwidth)
  2996. # Read the entire table.
  2997. text = reader.read_until(self.delimiter)
  2998. if reader.eof():
  2999. self.error('missing closing delimiter',self.start)
  3000. else:
  3001. delimiter = reader.read() # Discard closing delimiter.
  3002. assert re.match(self.delimiter,delimiter)
  3003. if len(text) == 0:
  3004. message.warning('[%s] table is empty' % self.name)
  3005. return
  3006. cols = attrs.get('cols')
  3007. if not cols:
  3008. # Calculate column count from number of items in first line.
  3009. if self.parameters.format == 'csv':
  3010. cols = text[0].count(self.parameters.separator)
  3011. else:
  3012. cols = 0
  3013. for cell in self.parse_psv_dsv(text[:1]):
  3014. cols += cell.span
  3015. self.parse_cols(cols, attrs.get('align'), attrs.get('valign'))
  3016. # Set calculated attributes.
  3017. self.attributes['colcount'] = len(self.columns)
  3018. self.build_colspecs()
  3019. self.parse_rows(text)
  3020. # The 'rowcount' attribute is used by the experimental LaTeX backend.
  3021. self.attributes['rowcount'] = str(len(self.rows))
  3022. # Generate headrows, footrows, bodyrows.
  3023. # Headrow, footrow and bodyrow data replaces same named attributes in
  3024. # the table markup template. In order to ensure this data does not get
  3025. # a second attribute substitution (which would interfere with any
  3026. # already substituted inline passthroughs) unique placeholders are used
  3027. # (the tab character does not appear elsewhere since it is expanded on
  3028. # input) which are replaced after template attribute substitution.
  3029. headrows = footrows = bodyrows = None
  3030. if self.rows and 'header' in self.parameters.options:
  3031. headrows = self.subs_rows(self.rows[0:1],'header')
  3032. self.attributes['headrows'] = '\x07headrows\x07'
  3033. self.rows = self.rows[1:]
  3034. if self.rows and 'footer' in self.parameters.options:
  3035. footrows = self.subs_rows( self.rows[-1:], 'footer')
  3036. self.attributes['footrows'] = '\x07footrows\x07'
  3037. self.rows = self.rows[:-1]
  3038. if self.rows:
  3039. bodyrows = self.subs_rows(self.rows)
  3040. self.attributes['bodyrows'] = '\x07bodyrows\x07'
  3041. table = subs_attrs(config.sections[self.parameters.template],
  3042. self.attributes)
  3043. table = writer.newline.join(table)
  3044. # Before we finish replace the table head, foot and body place holders
  3045. # with the real data.
  3046. if headrows:
  3047. table = table.replace('\x07headrows\x07', headrows, 1)
  3048. if footrows:
  3049. table = table.replace('\x07footrows\x07', footrows, 1)
  3050. if bodyrows:
  3051. table = table.replace('\x07bodyrows\x07', bodyrows, 1)
  3052. writer.write(table,trace='table')
  3053. class Tables(AbstractBlocks):
  3054. """List of tables."""
  3055. BLOCK_TYPE = Table
  3056. PREFIX = 'tabledef-'
  3057. TAGS = ('colspec', 'headrow','footrow','bodyrow',
  3058. 'headdata','footdata', 'bodydata','paragraph')
  3059. def __init__(self):
  3060. AbstractBlocks.__init__(self)
  3061. # Table tags dictionary. Each entry is a tags dictionary.
  3062. self.tags={}
  3063. def load(self,sections):
  3064. AbstractBlocks.load(self,sections)
  3065. self.load_tags(sections)
  3066. def load_tags(self,sections):
  3067. """
  3068. Load tabletags-* conf file sections to self.tags.
  3069. """
  3070. for section in sections.keys():
  3071. mo = re.match(r'^tabletags-(?P<name>\w+)$',section)
  3072. if mo:
  3073. name = mo.group('name')
  3074. if name in self.tags:
  3075. d = self.tags[name]
  3076. else:
  3077. d = AttrDict()
  3078. parse_entries(sections.get(section,()),d)
  3079. for k in d.keys():
  3080. if k not in self.TAGS:
  3081. message.warning('[%s] contains illegal table tag: %s' %
  3082. (section,k))
  3083. self.tags[name] = d
  3084. def validate(self):
  3085. AbstractBlocks.validate(self)
  3086. # Check we have a default table definition,
  3087. for i in range(len(self.blocks)):
  3088. if self.blocks[i].name == 'tabledef-default':
  3089. default = self.blocks[i]
  3090. break
  3091. else:
  3092. raise EAsciiDoc,'missing [tabledef-default] section'
  3093. # Propagate defaults to unspecified table parameters.
  3094. for b in self.blocks:
  3095. if b is not default:
  3096. if b.format is None: b.format = default.format
  3097. if b.template is None: b.template = default.template
  3098. # Check tags and propagate default tags.
  3099. if not 'default' in self.tags:
  3100. raise EAsciiDoc,'missing [tabletags-default] section'
  3101. default = self.tags['default']
  3102. for tag in ('bodyrow','bodydata','paragraph'): # Mandatory default tags.
  3103. if tag not in default:
  3104. raise EAsciiDoc,'missing [tabletags-default] entry: %s' % tag
  3105. for t in self.tags.values():
  3106. if t is not default:
  3107. if t.colspec is None: t.colspec = default.colspec
  3108. if t.headrow is None: t.headrow = default.headrow
  3109. if t.footrow is None: t.footrow = default.footrow
  3110. if t.bodyrow is None: t.bodyrow = default.bodyrow
  3111. if t.headdata is None: t.headdata = default.headdata
  3112. if t.footdata is None: t.footdata = default.footdata
  3113. if t.bodydata is None: t.bodydata = default.bodydata
  3114. if t.paragraph is None: t.paragraph = default.paragraph
  3115. # Use body tags if header and footer tags are not specified.
  3116. for t in self.tags.values():
  3117. if not t.headrow: t.headrow = t.bodyrow
  3118. if not t.footrow: t.footrow = t.bodyrow
  3119. if not t.headdata: t.headdata = t.bodydata
  3120. if not t.footdata: t.footdata = t.bodydata
  3121. # Check table definitions are valid.
  3122. for b in self.blocks:
  3123. b.validate()
  3124. def dump(self):
  3125. AbstractBlocks.dump(self)
  3126. for k,v in self.tags.items():
  3127. dump_section('tabletags-'+k, v)
  3128. class Macros:
  3129. # Default system macro syntax.
  3130. SYS_RE = r'(?u)^(?P<name>[\\]?\w(\w|-)*?)::(?P<target>\S*?)' + \
  3131. r'(\[(?P<attrlist>.*?)\])$'
  3132. def __init__(self):
  3133. self.macros = [] # List of Macros.
  3134. self.current = None # The last matched block macro.
  3135. self.passthroughs = []
  3136. # Initialize default system macro.
  3137. m = Macro()
  3138. m.pattern = self.SYS_RE
  3139. m.prefix = '+'
  3140. m.reo = re.compile(m.pattern)
  3141. self.macros.append(m)
  3142. def load(self,entries):
  3143. for entry in entries:
  3144. m = Macro()
  3145. m.load(entry)
  3146. if m.name is None:
  3147. # Delete undefined macro.
  3148. for i,m2 in enumerate(self.macros):
  3149. if m2.pattern == m.pattern:
  3150. del self.macros[i]
  3151. break
  3152. else:
  3153. message.warning('unable to delete missing macro: %s' % m.pattern)
  3154. else:
  3155. # Check for duplicates.
  3156. for m2 in self.macros:
  3157. if m2.pattern == m.pattern:
  3158. message.verbose('macro redefinition: %s%s' % (m.prefix,m.name))
  3159. break
  3160. else:
  3161. self.macros.append(m)
  3162. def dump(self):
  3163. write = lambda s: sys.stdout.write('%s%s' % (s,writer.newline))
  3164. write('[macros]')
  3165. # Dump all macros except the first (built-in system) macro.
  3166. for m in self.macros[1:]:
  3167. # Escape = in pattern.
  3168. macro = '%s=%s%s' % (m.pattern.replace('=',r'\='), m.prefix, m.name)
  3169. if m.subslist is not None:
  3170. macro += '[' + ','.join(m.subslist) + ']'
  3171. write(macro)
  3172. write('')
  3173. def validate(self):
  3174. # Check all named sections exist.
  3175. if config.verbose:
  3176. for m in self.macros:
  3177. if m.name and m.prefix != '+':
  3178. m.section_name()
  3179. def subs(self,text,prefix='',callouts=False):
  3180. # If callouts is True then only callout macros are processed, if False
  3181. # then all non-callout macros are processed.
  3182. result = text
  3183. for m in self.macros:
  3184. if m.prefix == prefix:
  3185. if callouts ^ (m.name != 'callout'):
  3186. result = m.subs(result)
  3187. return result
  3188. def isnext(self):
  3189. """Return matching macro if block macro is next on reader."""
  3190. reader.skip_blank_lines()
  3191. line = reader.read_next()
  3192. if line:
  3193. for m in self.macros:
  3194. if m.prefix == '#':
  3195. if m.reo.match(line):
  3196. self.current = m
  3197. return m
  3198. return False
  3199. def match(self,prefix,name,text):
  3200. """Return re match object matching 'text' with macro type 'prefix',
  3201. macro name 'name'."""
  3202. for m in self.macros:
  3203. if m.prefix == prefix:
  3204. mo = m.reo.match(text)
  3205. if mo:
  3206. if m.name == name:
  3207. return mo
  3208. if re.match(name,mo.group('name')):
  3209. return mo
  3210. return None
  3211. def extract_passthroughs(self,text,prefix=''):
  3212. """ Extract the passthrough text and replace with temporary
  3213. placeholders."""
  3214. self.passthroughs = []
  3215. for m in self.macros:
  3216. if m.has_passthrough() and m.prefix == prefix:
  3217. text = m.subs_passthroughs(text, self.passthroughs)
  3218. return text
  3219. def restore_passthroughs(self,text):
  3220. """ Replace passthough placeholders with the original passthrough
  3221. text."""
  3222. for i,v in enumerate(self.passthroughs):
  3223. text = text.replace('\x07'+str(i)+'\x07', self.passthroughs[i], 1)
  3224. return text
  3225. class Macro:
  3226. def __init__(self):
  3227. self.pattern = None # Matching regular expression.
  3228. self.name = '' # Conf file macro name (None if implicit).
  3229. self.prefix = '' # '' if inline, '+' if system, '#' if block.
  3230. self.reo = None # Compiled pattern re object.
  3231. self.subslist = [] # Default subs for macros passtext group.
  3232. def has_passthrough(self):
  3233. return self.pattern.find(r'(?P<passtext>') >= 0
  3234. def section_name(self,name=None):
  3235. """Return macro markup template section name based on macro name and
  3236. prefix. Return None section not found."""
  3237. assert self.prefix != '+'
  3238. if not name:
  3239. assert self.name
  3240. name = self.name
  3241. if self.prefix == '#':
  3242. suffix = '-blockmacro'
  3243. else:
  3244. suffix = '-inlinemacro'
  3245. if name+suffix in config.sections:
  3246. return name+suffix
  3247. else:
  3248. message.warning('missing macro section: [%s]' % (name+suffix))
  3249. return None
  3250. def load(self,entry):
  3251. e = parse_entry(entry)
  3252. if e is None:
  3253. # Only the macro pattern was specified, mark for deletion.
  3254. self.name = None
  3255. self.pattern = entry
  3256. return
  3257. if not is_re(e[0]):
  3258. raise EAsciiDoc,'illegal macro regular expression: %s' % e[0]
  3259. pattern, name = e
  3260. if name and name[0] in ('+','#'):
  3261. prefix, name = name[0], name[1:]
  3262. else:
  3263. prefix = ''
  3264. # Parse passthrough subslist.
  3265. mo = re.match(r'^(?P<name>[^[]*)(\[(?P<subslist>.*)\])?$', name)
  3266. name = mo.group('name')
  3267. if name and not is_name(name):
  3268. raise EAsciiDoc,'illegal section name in macro entry: %s' % entry
  3269. subslist = mo.group('subslist')
  3270. if subslist is not None:
  3271. # Parse and validate passthrough subs.
  3272. subslist = parse_options(subslist, SUBS_OPTIONS,
  3273. 'illegal subs in macro entry: %s' % entry)
  3274. self.pattern = pattern
  3275. self.reo = re.compile(pattern)
  3276. self.prefix = prefix
  3277. self.name = name
  3278. self.subslist = subslist or []
  3279. def subs(self,text):
  3280. def subs_func(mo):
  3281. """Function called to perform inline macro substitution.
  3282. Uses matched macro regular expression object and returns string
  3283. containing the substituted macro body."""
  3284. # Check if macro reference is escaped.
  3285. if mo.group()[0] == '\\':
  3286. return mo.group()[1:] # Strip leading backslash.
  3287. d = mo.groupdict()
  3288. # Delete groups that didn't participate in match.
  3289. for k,v in d.items():
  3290. if v is None: del d[k]
  3291. if self.name:
  3292. name = self.name
  3293. else:
  3294. if not 'name' in d:
  3295. message.warning('missing macro name group: %s' % mo.re.pattern)
  3296. return ''
  3297. name = d['name']
  3298. section_name = self.section_name(name)
  3299. if not section_name:
  3300. return ''
  3301. # If we're dealing with a block macro get optional block ID and
  3302. # block title.
  3303. if self.prefix == '#':
  3304. AttributeList.consume(d)
  3305. BlockTitle.consume(d)
  3306. # Parse macro attributes.
  3307. if 'attrlist' in d:
  3308. if d['attrlist'] in (None,''):
  3309. del d['attrlist']
  3310. else:
  3311. if self.prefix == '':
  3312. # Unescape ] characters in inline macros.
  3313. d['attrlist'] = d['attrlist'].replace('\\]',']')
  3314. parse_attributes(d['attrlist'],d)
  3315. # Generate option attributes.
  3316. if 'options' in d:
  3317. options = parse_options(d['options'], (),
  3318. '%s: illegal option name' % name)
  3319. for option in options:
  3320. d[option+'-option'] = ''
  3321. if name == 'callout':
  3322. listindex =int(d['index'])
  3323. d['coid'] = calloutmap.add(listindex)
  3324. # Unescape special characters in LaTeX target file names.
  3325. if document.backend == 'latex' and 'target' in d and d['target']:
  3326. if not '0' in d:
  3327. d['0'] = d['target']
  3328. d['target']= config.subs_specialchars_reverse(d['target'])
  3329. # BUG: We've already done attribute substitution on the macro which
  3330. # means that any escaped attribute references are now unescaped and
  3331. # will be substituted by config.subs_section() below. As a partial
  3332. # fix have withheld {0} from substitution but this kludge doesn't
  3333. # fix it for other attributes containing unescaped references.
  3334. # Passthrough macros don't have this problem.
  3335. a0 = d.get('0')
  3336. if a0:
  3337. d['0'] = chr(0) # Replace temporarily with unused character.
  3338. body = config.subs_section(section_name,d)
  3339. if len(body) == 0:
  3340. result = ''
  3341. elif len(body) == 1:
  3342. result = body[0]
  3343. else:
  3344. if self.prefix == '#':
  3345. result = writer.newline.join(body)
  3346. else:
  3347. # Internally processed inline macros use UNIX line
  3348. # separator.
  3349. result = '\n'.join(body)
  3350. if a0:
  3351. result = result.replace(chr(0), a0)
  3352. return result
  3353. return self.reo.sub(subs_func, text)
  3354. def translate(self):
  3355. """ Block macro translation."""
  3356. assert self.prefix == '#'
  3357. s = reader.read()
  3358. before = s
  3359. if self.has_passthrough():
  3360. s = macros.extract_passthroughs(s,'#')
  3361. s = subs_attrs(s)
  3362. if s:
  3363. s = self.subs(s)
  3364. if self.has_passthrough():
  3365. s = macros.restore_passthroughs(s)
  3366. if s:
  3367. trace('macro',before,s)
  3368. writer.write(s)
  3369. def subs_passthroughs(self, text, passthroughs):
  3370. """ Replace macro attribute lists in text with placeholders.
  3371. Substitute and append the passthrough attribute lists to the
  3372. passthroughs list."""
  3373. def subs_func(mo):
  3374. """Function called to perform inline macro substitution.
  3375. Uses matched macro regular expression object and returns string
  3376. containing the substituted macro body."""
  3377. # Don't process escaped macro references.
  3378. if mo.group()[0] == '\\':
  3379. return mo.group()
  3380. d = mo.groupdict()
  3381. if not 'passtext' in d:
  3382. message.warning('passthrough macro %s: missing passtext group' %
  3383. d.get('name',''))
  3384. return mo.group()
  3385. passtext = d['passtext']
  3386. if re.search('\x07\\d+\x07', passtext):
  3387. message.warning('nested inline passthrough')
  3388. return mo.group()
  3389. if d.get('subslist'):
  3390. if d['subslist'].startswith(':'):
  3391. message.error('block macro cannot occur here: %s' % mo.group(),
  3392. halt=True)
  3393. subslist = parse_options(d['subslist'], SUBS_OPTIONS,
  3394. 'illegal passthrough macro subs option')
  3395. else:
  3396. subslist = self.subslist
  3397. passtext = Lex.subs_1(passtext,subslist)
  3398. if passtext is None: passtext = ''
  3399. if self.prefix == '':
  3400. # Unescape ] characters in inline macros.
  3401. passtext = passtext.replace('\\]',']')
  3402. passthroughs.append(passtext)
  3403. # Tabs guarantee the placeholders are unambiguous.
  3404. result = (
  3405. text[mo.start():mo.start('passtext')] +
  3406. '\x07' + str(len(passthroughs)-1) + '\x07' +
  3407. text[mo.end('passtext'):mo.end()]
  3408. )
  3409. return result
  3410. return self.reo.sub(subs_func, text)
  3411. class CalloutMap:
  3412. def __init__(self):
  3413. self.comap = {} # key = list index, value = callouts list.
  3414. self.calloutindex = 0 # Current callout index number.
  3415. self.listnumber = 1 # Current callout list number.
  3416. def listclose(self):
  3417. # Called when callout list is closed.
  3418. self.listnumber += 1
  3419. self.calloutindex = 0
  3420. self.comap = {}
  3421. def add(self,listindex):
  3422. # Add next callout index to listindex map entry. Return the callout id.
  3423. self.calloutindex += 1
  3424. # Append the coindex to a list in the comap dictionary.
  3425. if not listindex in self.comap:
  3426. self.comap[listindex] = [self.calloutindex]
  3427. else:
  3428. self.comap[listindex].append(self.calloutindex)
  3429. return self.calloutid(self.listnumber, self.calloutindex)
  3430. @staticmethod
  3431. def calloutid(listnumber,calloutindex):
  3432. return 'CO%d-%d' % (listnumber,calloutindex)
  3433. def calloutids(self,listindex):
  3434. # Retieve list of callout indexes that refer to listindex.
  3435. if listindex in self.comap:
  3436. result = ''
  3437. for coindex in self.comap[listindex]:
  3438. result += ' ' + self.calloutid(self.listnumber,coindex)
  3439. return result.strip()
  3440. else:
  3441. message.warning('no callouts refer to list item '+str(listindex))
  3442. return ''
  3443. def validate(self,maxlistindex):
  3444. # Check that all list indexes referenced by callouts exist.
  3445. for listindex in self.comap.keys():
  3446. if listindex > maxlistindex:
  3447. message.warning('callout refers to non-existent list item '
  3448. + str(listindex))
  3449. #---------------------------------------------------------------------------
  3450. # Input stream Reader and output stream writer classes.
  3451. #---------------------------------------------------------------------------
  3452. class Reader1:
  3453. """Line oriented AsciiDoc input file reader. Processes include and
  3454. conditional inclusion system macros. Tabs are expanded and lines are right
  3455. trimmed."""
  3456. # This class is not used directly, use Reader class instead.
  3457. READ_BUFFER_MIN = 10 # Read buffer low level.
  3458. def __init__(self):
  3459. self.f = None # Input file object.
  3460. self.fname = None # Input file name.
  3461. self.next = [] # Read ahead buffer containing
  3462. # [filename,linenumber,linetext] lists.
  3463. self.cursor = None # Last read() [filename,linenumber,linetext].
  3464. self.tabsize = 8 # Tab expansion number of spaces.
  3465. self.parent = None # Included reader's parent reader.
  3466. self._lineno = 0 # The last line read from file object f.
  3467. self.current_depth = 0 # Current include depth.
  3468. self.max_depth = 5 # Initial maxiumum allowed include depth.
  3469. def open(self,fname):
  3470. self.fname = fname
  3471. message.verbose('reading: '+fname)
  3472. if fname == '<stdin>':
  3473. self.f = sys.stdin
  3474. document.attributes['infile'] = None
  3475. document.attributes['indir'] = None
  3476. else:
  3477. self.f = open(fname,'rb')
  3478. document.attributes['infile'] = fname
  3479. document.attributes['indir'] = os.path.dirname(fname)
  3480. self._lineno = 0 # The last line read from file object f.
  3481. self.next = []
  3482. # Prefill buffer by reading the first line and then pushing it back.
  3483. if Reader1.read(self):
  3484. self.unread(self.cursor)
  3485. self.cursor = None
  3486. def closefile(self):
  3487. """Used by class methods to close nested include files."""
  3488. self.f.close()
  3489. self.next = []
  3490. def close(self):
  3491. self.closefile()
  3492. self.__init__()
  3493. def read(self, skip=False):
  3494. """Read next line. Return None if EOF. Expand tabs. Strip trailing
  3495. white space. Maintain self.next read ahead buffer. If skip=True then
  3496. conditional exclusion is active (ifdef and ifndef macros)."""
  3497. # Top up buffer.
  3498. if len(self.next) <= self.READ_BUFFER_MIN:
  3499. s = self.f.readline()
  3500. if s:
  3501. self._lineno = self._lineno + 1
  3502. while s:
  3503. if self.tabsize != 0:
  3504. s = s.expandtabs(self.tabsize)
  3505. s = s.rstrip()
  3506. self.next.append([self.fname,self._lineno,s])
  3507. if len(self.next) > self.READ_BUFFER_MIN:
  3508. break
  3509. s = self.f.readline()
  3510. if s:
  3511. self._lineno = self._lineno + 1
  3512. # Return first (oldest) buffer entry.
  3513. if len(self.next) > 0:
  3514. self.cursor = self.next[0]
  3515. del self.next[0]
  3516. result = self.cursor[2]
  3517. # Check for include macro.
  3518. mo = macros.match('+',r'include[1]?',result)
  3519. if mo and not skip:
  3520. # Don't process include macro once the maximum depth is reached.
  3521. if self.current_depth >= self.max_depth:
  3522. return result
  3523. # Perform attribute substitution on include macro file name.
  3524. fname = subs_attrs(mo.group('target'))
  3525. if not fname:
  3526. return Reader1.read(self) # Return next input line.
  3527. if self.fname != '<stdin>':
  3528. fname = os.path.expandvars(os.path.expanduser(fname))
  3529. fname = safe_filename(fname, os.path.dirname(self.fname))
  3530. if not fname:
  3531. return Reader1.read(self) # Return next input line.
  3532. if mo.group('name') == 'include1':
  3533. if not config.dumping:
  3534. # Store the include file in memory for later
  3535. # retrieval by the {include1:} system attribute.
  3536. config.include1[fname] = [
  3537. s.rstrip() for s in open(fname)]
  3538. return '{include1:%s}' % fname
  3539. else:
  3540. # This is a configuration dump, just pass the macro
  3541. # call through.
  3542. return result
  3543. # Parse include macro attributes.
  3544. attrs = {}
  3545. parse_attributes(mo.group('attrlist'),attrs)
  3546. # Clone self and set as parent (self assumes the role of child).
  3547. parent = Reader1()
  3548. assign(parent,self)
  3549. self.parent = parent
  3550. # Set attributes in child.
  3551. if 'tabsize' in attrs:
  3552. self.tabsize = int(validate(attrs['tabsize'],
  3553. 'int($)>=0',
  3554. 'illegal include macro tabsize argument'))
  3555. else:
  3556. self.tabsize = config.tabsize
  3557. if 'depth' in attrs:
  3558. attrs['depth'] = int(validate(attrs['depth'],
  3559. 'int($)>=1',
  3560. 'illegal include macro depth argument'))
  3561. self.max_depth = self.current_depth + attrs['depth']
  3562. # Process included file.
  3563. self.open(fname)
  3564. self.current_depth = self.current_depth + 1
  3565. result = Reader1.read(self)
  3566. else:
  3567. if not Reader1.eof(self):
  3568. result = Reader1.read(self)
  3569. else:
  3570. result = None
  3571. return result
  3572. def eof(self):
  3573. """Returns True if all lines have been read."""
  3574. if len(self.next) == 0:
  3575. # End of current file.
  3576. if self.parent:
  3577. self.closefile()
  3578. assign(self,self.parent) # Restore parent reader.
  3579. return Reader1.eof(self)
  3580. else:
  3581. return True
  3582. else:
  3583. return False
  3584. def read_next(self):
  3585. """Like read() but does not advance file pointer."""
  3586. if Reader1.eof(self):
  3587. return None
  3588. else:
  3589. return self.next[0][2]
  3590. def unread(self,cursor):
  3591. """Push the line (filename,linenumber,linetext) tuple back into the read
  3592. buffer. Note that it's up to the caller to restore the previous
  3593. cursor."""
  3594. assert cursor
  3595. self.next.insert(0,cursor)
  3596. class Reader(Reader1):
  3597. """ Wraps (well, sought of) Reader1 class and implements conditional text
  3598. inclusion."""
  3599. def __init__(self):
  3600. Reader1.__init__(self)
  3601. self.depth = 0 # if nesting depth.
  3602. self.skip = False # true if we're skipping ifdef...endif.
  3603. self.skipname = '' # Name of current endif macro target.
  3604. self.skipto = -1 # The depth at which skipping is reenabled.
  3605. def read_super(self):
  3606. result = Reader1.read(self,self.skip)
  3607. if result is None and self.skip:
  3608. raise EAsciiDoc,'missing endif::%s[]' % self.skipname
  3609. return result
  3610. def read(self):
  3611. result = self.read_super()
  3612. if result is None:
  3613. return None
  3614. while self.skip:
  3615. mo = macros.match('+',r'ifdef|ifndef|endif',result)
  3616. if mo:
  3617. name = mo.group('name')
  3618. target = mo.group('target')
  3619. if name == 'endif':
  3620. self.depth = self.depth-1
  3621. if self.depth < 0:
  3622. raise EAsciiDoc,'mismatched macro: %s' % result
  3623. if self.depth == self.skipto:
  3624. self.skip = False
  3625. if target and self.skipname != target:
  3626. raise EAsciiDoc,'mismatched macro: %s' % result
  3627. else: # ifdef or ifndef.
  3628. if not target:
  3629. raise EAsciiDoc,'missing macro target: %s' % result
  3630. attrlist = mo.group('attrlist')
  3631. if not attrlist:
  3632. self.depth = self.depth+1
  3633. result = self.read_super()
  3634. if result is None:
  3635. return None
  3636. mo = macros.match('+',r'ifdef|ifndef|endif',result)
  3637. if mo:
  3638. name = mo.group('name')
  3639. target = mo.group('target')
  3640. if name == 'endif':
  3641. self.depth = self.depth-1
  3642. else: # ifdef or ifndef.
  3643. if not target:
  3644. raise EAsciiDoc,'missing macro target: %s' % result
  3645. defined = document.attributes.get(target) is not None
  3646. attrlist = mo.group('attrlist')
  3647. if name == 'ifdef':
  3648. if attrlist:
  3649. if defined: return attrlist
  3650. else:
  3651. self.skip = not defined
  3652. else: # ifndef.
  3653. if attrlist:
  3654. if not defined: return attrlist
  3655. else:
  3656. self.skip = defined
  3657. if not attrlist:
  3658. if self.skip:
  3659. self.skipto = self.depth
  3660. self.skipname = target
  3661. self.depth = self.depth+1
  3662. result = self.read()
  3663. if result:
  3664. # Expand executable block macros.
  3665. mo = macros.match('+',r'eval|sys|sys2',result)
  3666. if mo:
  3667. action = mo.group('name')
  3668. cmd = mo.group('attrlist')
  3669. s = system(action, cmd, is_macro=True)
  3670. if s is not None:
  3671. self.cursor[2] = s # So we don't re-evaluate.
  3672. result = s
  3673. if result:
  3674. # Unescape escaped system macros.
  3675. if macros.match('+',r'\\eval|\\sys|\\sys2|\\ifdef|\\ifndef|\\endif|\\include|\\include1',result):
  3676. result = result[1:]
  3677. return result
  3678. def eof(self):
  3679. return self.read_next() is None
  3680. def read_next(self):
  3681. save_cursor = self.cursor
  3682. result = self.read()
  3683. if result is not None:
  3684. self.unread(self.cursor)
  3685. self.cursor = save_cursor
  3686. return result
  3687. def read_lines(self,count=1):
  3688. """Return tuple containing count lines."""
  3689. result = []
  3690. i = 0
  3691. while i < count and not self.eof():
  3692. result.append(self.read())
  3693. return tuple(result)
  3694. def read_ahead(self,count=1):
  3695. """Same as read_lines() but does not advance the file pointer."""
  3696. result = []
  3697. putback = []
  3698. save_cursor = self.cursor
  3699. try:
  3700. i = 0
  3701. while i < count and not self.eof():
  3702. result.append(self.read())
  3703. putback.append(self.cursor)
  3704. i = i+1
  3705. while putback:
  3706. self.unread(putback.pop())
  3707. finally:
  3708. self.cursor = save_cursor
  3709. return tuple(result)
  3710. def skip_blank_lines(self):
  3711. reader.read_until(r'\s*\S+')
  3712. def read_until(self,terminators,same_file=False):
  3713. """Like read() but reads lines up to (but not including) the first line
  3714. that matches the terminator regular expression, regular expression
  3715. object or list of regular expression objects. If same_file is True then
  3716. the terminating pattern must occur in the file the was being read when
  3717. the routine was called."""
  3718. if same_file:
  3719. fname = self.cursor[0]
  3720. result = []
  3721. if not isinstance(terminators,list):
  3722. if isinstance(terminators,basestring):
  3723. terminators = [re.compile(terminators)]
  3724. else:
  3725. terminators = [terminators]
  3726. while not self.eof():
  3727. save_cursor = self.cursor
  3728. s = self.read()
  3729. if not same_file or fname == self.cursor[0]:
  3730. for reo in terminators:
  3731. if reo.match(s):
  3732. self.unread(self.cursor)
  3733. self.cursor = save_cursor
  3734. return tuple(result)
  3735. result.append(s)
  3736. return tuple(result)
  3737. # NOT USED -- part of unimplemented attempt a generalised line continuation.
  3738. def read_continuation(self):
  3739. """Like read() but treats trailing backslash as line continuation
  3740. character."""
  3741. s = self.read()
  3742. if s is None:
  3743. return None
  3744. result = ''
  3745. while s is not None and len(s) > 0 and s[-1] == '\\':
  3746. result = result + s[:-1]
  3747. s = self.read()
  3748. if s is not None:
  3749. result = result + s
  3750. return result
  3751. # NOT USED -- part of unimplemented attempt a generalised line continuation.
  3752. def read_next_continuation(self):
  3753. """Like read_next() but treats trailing backslash as line continuation
  3754. character."""
  3755. save_cursor = self.cursor
  3756. result = self.read_continuation()
  3757. if result is not None:
  3758. self.unread(self.cursor)
  3759. self.cursor = save_cursor
  3760. return result
  3761. class Writer:
  3762. """Writes lines to output file."""
  3763. def __init__(self):
  3764. self.newline = '\r\n' # End of line terminator.
  3765. self.f = None # Output file object.
  3766. self.fname = None # Output file name.
  3767. self.lines_out = 0 # Number of lines written.
  3768. self.skip_blank_lines = False # If True don't output blank lines.
  3769. def open(self,fname):
  3770. self.fname = fname
  3771. if fname == '<stdout>':
  3772. self.f = sys.stdout
  3773. else:
  3774. self.f = open(fname,'wb+')
  3775. self.lines_out = 0
  3776. def close(self):
  3777. if self.fname != '<stdout>':
  3778. self.f.close()
  3779. def write_line(self, line=None):
  3780. if not (self.skip_blank_lines and (not line or not line.strip())):
  3781. self.f.write((line or '') + self.newline)
  3782. self.lines_out = self.lines_out + 1
  3783. def write(self,*args,**kwargs):
  3784. """Iterates arguments, writes tuple and list arguments one line per
  3785. element, else writes argument as single line. If no arguments writes
  3786. blank line. If argument is None nothing is written. self.newline is
  3787. appended to each line."""
  3788. if 'trace' in kwargs and len(args) > 0:
  3789. trace(kwargs['trace'],args[0])
  3790. if len(args) == 0:
  3791. self.write_line()
  3792. self.lines_out = self.lines_out + 1
  3793. else:
  3794. for arg in args:
  3795. if is_array(arg):
  3796. for s in arg:
  3797. self.write_line(s)
  3798. elif arg is not None:
  3799. self.write_line(arg)
  3800. def write_tag(self,tag,content,subs=None,d=None,**kwargs):
  3801. """Write content enveloped by tag.
  3802. Substitutions specified in the 'subs' list are perform on the
  3803. 'content'."""
  3804. if subs is None:
  3805. subs = config.subsnormal
  3806. stag,etag = subs_tag(tag,d)
  3807. content = Lex.subs(content,subs)
  3808. if 'trace' in kwargs:
  3809. trace(kwargs['trace'],[stag]+content+[etag])
  3810. if stag:
  3811. self.write(stag)
  3812. if content:
  3813. self.write(content)
  3814. if etag:
  3815. self.write(etag)
  3816. #---------------------------------------------------------------------------
  3817. # Configuration file processing.
  3818. #---------------------------------------------------------------------------
  3819. def _subs_specialwords(mo):
  3820. """Special word substitution function called by
  3821. Config.subs_specialwords()."""
  3822. word = mo.re.pattern # The special word.
  3823. template = config.specialwords[word] # The corresponding markup template.
  3824. if not template in config.sections:
  3825. raise EAsciiDoc,'missing special word template [%s]' % template
  3826. if mo.group()[0] == '\\':
  3827. return mo.group()[1:] # Return escaped word.
  3828. args = {}
  3829. args['words'] = mo.group() # The full match string is argument 'words'.
  3830. args.update(mo.groupdict()) # Add other named match groups to the arguments.
  3831. # Delete groups that didn't participate in match.
  3832. for k,v in args.items():
  3833. if v is None: del args[k]
  3834. lines = subs_attrs(config.sections[template],args)
  3835. if len(lines) == 0:
  3836. result = ''
  3837. elif len(lines) == 1:
  3838. result = lines[0]
  3839. else:
  3840. result = writer.newline.join(lines)
  3841. return result
  3842. class Config:
  3843. """Methods to process configuration files."""
  3844. # Non-template section name regexp's.
  3845. ENTRIES_SECTIONS= ('tags','miscellaneous','attributes','specialcharacters',
  3846. 'specialwords','macros','replacements','quotes','titles',
  3847. r'paradef-.+',r'listdef-.+',r'blockdef-.+',r'tabledef-.+',
  3848. r'tabletags-.+',r'listtags-.+','replacements2',
  3849. r'old_tabledef-.+')
  3850. def __init__(self):
  3851. self.sections = OrderedDict() # Keyed by section name containing
  3852. # lists of section lines.
  3853. # Command-line options.
  3854. self.verbose = False
  3855. self.header_footer = True # -s, --no-header-footer option.
  3856. # [miscellaneous] section.
  3857. self.tabsize = 8
  3858. self.textwidth = 70 # DEPRECATED: Old tables only.
  3859. self.newline = '\r\n'
  3860. self.pagewidth = None
  3861. self.pageunits = None
  3862. self.outfilesuffix = ''
  3863. self.subsnormal = SUBS_NORMAL
  3864. self.subsverbatim = SUBS_VERBATIM
  3865. self.tags = {} # Values contain (stag,etag) tuples.
  3866. self.specialchars = {} # Values of special character substitutions.
  3867. self.specialwords = {} # Name is special word pattern, value is macro.
  3868. self.replacements = OrderedDict() # Key is find pattern, value is
  3869. #replace pattern.
  3870. self.replacements2 = OrderedDict()
  3871. self.specialsections = {} # Name is special section name pattern, value
  3872. # is corresponding section name.
  3873. self.quotes = OrderedDict() # Values contain corresponding tag name.
  3874. self.fname = '' # Most recently loaded configuration file name.
  3875. self.conf_attrs = {} # Attributes entries from conf files.
  3876. self.cmd_attrs = {} # Attributes from command-line -a options.
  3877. self.loaded = [] # Loaded conf files.
  3878. self.include1 = {} # Holds include1::[] files for {include1:}.
  3879. self.dumping = False # True if asciidoc -c option specified.
  3880. def load_file(self,fname,dir=None):
  3881. """
  3882. Loads sections dictionary with sections from file fname.
  3883. Existing sections are overlaid.
  3884. Return False if no file was found in any of the locations.
  3885. """
  3886. if dir:
  3887. fname = os.path.join(dir, fname)
  3888. # Sliently skip missing configuration file.
  3889. if not os.path.isfile(fname):
  3890. return False
  3891. # Don't load conf files twice (local and application conf files are the
  3892. # same if the source file is in the application directory).
  3893. if os.path.realpath(fname) in self.loaded:
  3894. return True
  3895. rdr = Reader() # Reader processes system macros.
  3896. rdr.open(fname)
  3897. self.fname = fname
  3898. reo = re.compile(r'(?u)^\[(?P<section>[^\W\d][\w-]*)\]\s*$')
  3899. sections = OrderedDict()
  3900. section,contents = '',[]
  3901. while not rdr.eof():
  3902. s = rdr.read()
  3903. if s and s[0] == '#': # Skip comment lines.
  3904. continue
  3905. if s[:2] == '\\#': # Unescape lines starting with '#'.
  3906. s = s[1:]
  3907. s = s.rstrip()
  3908. found = reo.findall(s)
  3909. if found:
  3910. if section: # Store previous section.
  3911. if section in sections \
  3912. and self.entries_section(section):
  3913. if ''.join(contents):
  3914. # Merge entries.
  3915. sections[section] = sections[section] + contents
  3916. else:
  3917. del sections[section]
  3918. else:
  3919. sections[section] = contents
  3920. section = found[0].lower()
  3921. contents = []
  3922. else:
  3923. contents.append(s)
  3924. if section and contents: # Store last section.
  3925. if section in sections \
  3926. and self.entries_section(section):
  3927. if ''.join(contents):
  3928. # Merge entries.
  3929. sections[section] = sections[section] + contents
  3930. else:
  3931. del sections[section]
  3932. else:
  3933. sections[section] = contents
  3934. rdr.close()
  3935. self.load_sections(sections)
  3936. self.loaded.append(os.path.realpath(fname))
  3937. document.update_attributes() # So they are available immediately.
  3938. return True
  3939. def load_sections(self,sections):
  3940. '''Loads sections dictionary. Each dictionary entry contains a
  3941. list of lines.
  3942. '''
  3943. # Delete trailing blank lines from sections.
  3944. for k in sections.keys():
  3945. for i in range(len(sections[k])-1,-1,-1):
  3946. if not sections[k][i]:
  3947. del sections[k][i]
  3948. elif not self.entries_section(k):
  3949. break
  3950. # Add/overwrite new sections.
  3951. self.sections.update(sections)
  3952. self.parse_tags()
  3953. # Internally [miscellaneous] section entries are just attributes.
  3954. d = {}
  3955. parse_entries(sections.get('miscellaneous',()), d, unquote=True,
  3956. allow_name_only=True)
  3957. update_attrs(self.conf_attrs,d)
  3958. d = {}
  3959. parse_entries(sections.get('attributes',()), d, unquote=True,
  3960. allow_name_only=True)
  3961. update_attrs(self.conf_attrs,d)
  3962. d = {}
  3963. parse_entries(sections.get('titles',()),d)
  3964. Title.load(d)
  3965. parse_entries(sections.get('specialcharacters',()),self.specialchars,escape_delimiter=False)
  3966. parse_entries(sections.get('quotes',()),self.quotes)
  3967. self.parse_specialwords()
  3968. self.parse_replacements()
  3969. self.parse_replacements('replacements2')
  3970. self.parse_specialsections()
  3971. paragraphs.load(sections)
  3972. lists.load(sections)
  3973. blocks.load(sections)
  3974. tables_OLD.load(sections)
  3975. tables.load(sections)
  3976. macros.load(sections.get('macros',()))
  3977. def get_load_dirs(self):
  3978. """Return list of well known paths to search for conf files."""
  3979. result = []
  3980. # Load global configuration from system configuration directory.
  3981. result.append(CONF_DIR)
  3982. # Load global configuration files from asciidoc directory.
  3983. result.append(APP_DIR)
  3984. # Load configuration files from ~/.asciidoc if it exists.
  3985. if USER_DIR is not None:
  3986. result.append(USER_DIR)
  3987. # Load configuration files from document directory.
  3988. if document.infile != '<stdin>':
  3989. result.append(os.path.dirname(document.infile))
  3990. return result
  3991. def load_lang(self, lang, dirs=None):
  3992. """
  3993. Load language conf file from dirs list.
  3994. If dirs not specified try all the well known locations.
  3995. Return False if no file was found in any of the locations.
  3996. """
  3997. result = False
  3998. if dirs is None:
  3999. dirs = self.get_load_dirs()
  4000. filename = 'lang-' + lang + '.conf'
  4001. for d in dirs:
  4002. if self.load_file(filename,d):
  4003. result = True
  4004. return result
  4005. def load_all(self, dirs=None):
  4006. """
  4007. Load the standard configuration (except the language file)
  4008. files from dirs list.
  4009. If dirs not specified try all the well known locations.
  4010. """
  4011. if dirs is None:
  4012. dirs = self.get_load_dirs()
  4013. for d in dirs:
  4014. self.load_file('asciidoc.conf',d)
  4015. conf = document.backend + '.conf'
  4016. self.load_file(conf,d)
  4017. conf = document.backend + '-' + document.doctype + '.conf'
  4018. self.load_file(conf,d)
  4019. # Load filter .conf files.
  4020. filtersdir = os.path.join(d,'filters')
  4021. for dirpath,dirnames,filenames in os.walk(filtersdir):
  4022. for f in filenames:
  4023. if re.match(r'^.+\.conf$',f):
  4024. self.load_file(f,dirpath)
  4025. # English defaults.
  4026. self.load_file('lang-en.conf',d)
  4027. def load_miscellaneous(self,d):
  4028. """Set miscellaneous configuration entries from dictionary 'd'."""
  4029. def set_misc(name,rule='True',intval=False):
  4030. if name in d:
  4031. errmsg = 'illegal [miscellaneous] %s entry' % name
  4032. if intval:
  4033. setattr(self, name, int(validate(d[name],rule,errmsg)))
  4034. else:
  4035. setattr(self, name, validate(d[name],rule,errmsg))
  4036. set_misc('tabsize','int($)>0',intval=True)
  4037. set_misc('textwidth','int($)>0',intval=True) # DEPRECATED: Old tables only.
  4038. set_misc('pagewidth','int($)>0',intval=True)
  4039. set_misc('pageunits')
  4040. set_misc('outfilesuffix')
  4041. if 'newline' in d:
  4042. # Convert escape sequences to their character values.
  4043. self.newline = eval('"'+d['newline']+'"')
  4044. if 'subsnormal' in d:
  4045. self.subsnormal = parse_options(d['subsnormal'],SUBS_OPTIONS,
  4046. 'illegal [%s] %s: %s' %
  4047. ('miscellaneous','subsnormal',d['subsnormal']))
  4048. if 'subsverbatim' in d:
  4049. self.subsverbatim = parse_options(d['subsverbatim'],SUBS_OPTIONS,
  4050. 'illegal [%s] %s: %s' %
  4051. ('miscellaneous','subsverbatim',d['subsverbatim']))
  4052. def validate(self):
  4053. """Check the configuration for internal consistancy. Called after all
  4054. configuration files have been loaded."""
  4055. # Heuristic validate that at least one configuration file was loaded.
  4056. if not self.specialchars or not self.tags or not lists:
  4057. raise EAsciiDoc,'incomplete configuration files'
  4058. # Check special characters are only one character long.
  4059. for k in self.specialchars.keys():
  4060. if len(k) != 1:
  4061. raise EAsciiDoc,'[specialcharacters] ' \
  4062. 'must be a single character: %s' % k
  4063. # Check all special words have a corresponding inline macro body.
  4064. for macro in self.specialwords.values():
  4065. if not is_name(macro):
  4066. raise EAsciiDoc,'illegal special word name: %s' % macro
  4067. if not macro in self.sections:
  4068. message.warning('missing special word macro: [%s]' % macro)
  4069. # Check all text quotes have a corresponding tag.
  4070. for q in self.quotes.keys():
  4071. tag = self.quotes[q]
  4072. if not tag:
  4073. del self.quotes[q] # Undefine quote.
  4074. else:
  4075. if tag[0] == '#':
  4076. tag = tag[1:]
  4077. if not tag in self.tags:
  4078. message.warning('[quotes] %s missing tag definition: %s' % (q,tag))
  4079. # Check all specialsections section names exist.
  4080. for k,v in self.specialsections.items():
  4081. if not v:
  4082. del self.specialsections[k]
  4083. elif not v in self.sections:
  4084. message.warning('[%s] missing specialsections section' % v)
  4085. paragraphs.validate()
  4086. lists.validate()
  4087. blocks.validate()
  4088. tables_OLD.validate()
  4089. tables.validate()
  4090. macros.validate()
  4091. def entries_section(self,section_name):
  4092. """
  4093. Return True if conf file section contains entries, not a markup
  4094. template.
  4095. """
  4096. for name in self.ENTRIES_SECTIONS:
  4097. if re.match(name,section_name):
  4098. return True
  4099. return False
  4100. def dump(self):
  4101. """Dump configuration to stdout."""
  4102. # Header.
  4103. hdr = ''
  4104. hdr = hdr + '#' + writer.newline
  4105. hdr = hdr + '# Generated by AsciiDoc %s for %s %s.%s' % \
  4106. (VERSION,document.backend,document.doctype,writer.newline)
  4107. t = time.asctime(time.localtime(time.time()))
  4108. hdr = hdr + '# %s%s' % (t,writer.newline)
  4109. hdr = hdr + '#' + writer.newline
  4110. sys.stdout.write(hdr)
  4111. # Dump special sections.
  4112. # Dump only the configuration file and command-line attributes.
  4113. # [miscellanous] entries are dumped as part of the [attributes].
  4114. d = {}
  4115. d.update(self.conf_attrs)
  4116. d.update(self.cmd_attrs)
  4117. dump_section('attributes',d)
  4118. Title.dump()
  4119. dump_section('quotes',self.quotes)
  4120. dump_section('specialcharacters',self.specialchars)
  4121. d = {}
  4122. for k,v in self.specialwords.items():
  4123. if v in d:
  4124. d[v] = '%s "%s"' % (d[v],k) # Append word list.
  4125. else:
  4126. d[v] = '"%s"' % k
  4127. dump_section('specialwords',d)
  4128. dump_section('replacements',self.replacements)
  4129. dump_section('replacements2',self.replacements2)
  4130. dump_section('specialsections',self.specialsections)
  4131. d = {}
  4132. for k,v in self.tags.items():
  4133. d[k] = '%s|%s' % v
  4134. dump_section('tags',d)
  4135. paragraphs.dump()
  4136. lists.dump()
  4137. blocks.dump()
  4138. tables_OLD.dump()
  4139. tables.dump()
  4140. macros.dump()
  4141. # Dump remaining sections.
  4142. for k in self.sections.keys():
  4143. if not self.entries_section(k):
  4144. sys.stdout.write('[%s]%s' % (k,writer.newline))
  4145. for line in self.sections[k]:
  4146. sys.stdout.write('%s%s' % (line,writer.newline))
  4147. sys.stdout.write(writer.newline)
  4148. def subs_section(self,section,d):
  4149. """Section attribute substitution using attributes from
  4150. document.attributes and 'd'. Lines containing undefinded
  4151. attributes are deleted."""
  4152. if section in self.sections:
  4153. return subs_attrs(self.sections[section],d)
  4154. else:
  4155. message.warning('missing [%s] section' % section)
  4156. return ()
  4157. def parse_tags(self):
  4158. """Parse [tags] section entries into self.tags dictionary."""
  4159. d = {}
  4160. parse_entries(self.sections.get('tags',()),d)
  4161. for k,v in d.items():
  4162. if v is None:
  4163. if k in self.tags:
  4164. del self.tags[k]
  4165. elif v == '':
  4166. self.tags[k] = (None,None)
  4167. else:
  4168. mo = re.match(r'(?P<stag>.*)\|(?P<etag>.*)',v)
  4169. if mo:
  4170. self.tags[k] = (mo.group('stag'), mo.group('etag'))
  4171. else:
  4172. raise EAsciiDoc,'[tag] %s value malformed' % k
  4173. def tag(self, name, d=None):
  4174. """Returns (starttag,endtag) tuple named name from configuration file
  4175. [tags] section. Raise error if not found. If a dictionary 'd' is
  4176. passed then merge with document attributes and perform attribute
  4177. substitution on tags."""
  4178. # TODO: Tags should be stored a single string, not split into start
  4179. # and end tags since most are going to be substituted anyway (see
  4180. # subs_tag() for how we should process them. parse_tags() (above)
  4181. # should only validate i.e. parse_check(). This routine should be renamed
  4182. # split_tag() and would call subs_tag(). self.tags dictionary values
  4183. # would be strings not tuples.
  4184. if not name in self.tags:
  4185. raise EAsciiDoc, 'missing tag: %s' % name
  4186. stag,etag = self.tags[name]
  4187. if d is not None:
  4188. # TODO: Should we warn if substitution drops a tag?
  4189. if stag:
  4190. stag = subs_attrs(stag,d)
  4191. if etag:
  4192. etag = subs_attrs(etag,d)
  4193. if stag is None: stag = ''
  4194. if etag is None: etag = ''
  4195. return (stag,etag)
  4196. def parse_specialsections(self):
  4197. """Parse specialsections section to self.specialsections dictionary."""
  4198. # TODO: This is virtually the same as parse_replacements() and should
  4199. # be factored to single routine.
  4200. d = {}
  4201. parse_entries(self.sections.get('specialsections',()),d,unquote=True)
  4202. for pat,sectname in d.items():
  4203. pat = strip_quotes(pat)
  4204. if not is_re(pat):
  4205. raise EAsciiDoc,'[specialsections] entry ' \
  4206. 'is not a valid regular expression: %s' % pat
  4207. if sectname is None:
  4208. if pat in self.specialsections:
  4209. del self.specialsections[pat]
  4210. else:
  4211. self.specialsections[pat] = sectname
  4212. def parse_replacements(self,sect='replacements'):
  4213. """Parse replacements section into self.replacements dictionary."""
  4214. d = OrderedDict()
  4215. parse_entries(self.sections.get(sect,()), d, unquote=True)
  4216. for pat,rep in d.items():
  4217. if not self.set_replacement(pat, rep, getattr(self,sect)):
  4218. raise EAsciiDoc,'[%s] entry in %s is not a valid' \
  4219. ' regular expression: %s' % (sect,self.fname,pat)
  4220. @staticmethod
  4221. def set_replacement(pat, rep, replacements):
  4222. """Add pattern and replacement to replacements dictionary."""
  4223. pat = strip_quotes(pat)
  4224. if not is_re(pat):
  4225. return False
  4226. if rep is None:
  4227. if pat in replacements:
  4228. del replacements[pat]
  4229. else:
  4230. replacements[pat] = strip_quotes(rep)
  4231. return True
  4232. def subs_replacements(self,s,sect='replacements'):
  4233. """Substitute patterns from self.replacements in 's'."""
  4234. result = s
  4235. for pat,rep in getattr(self,sect).items():
  4236. result = re.sub(pat, rep, result)
  4237. return result
  4238. def parse_specialwords(self):
  4239. """Parse special words section into self.specialwords dictionary."""
  4240. reo = re.compile(r'(?:\s|^)(".+?"|[^"\s]+)(?=\s|$)')
  4241. for line in self.sections.get('specialwords',()):
  4242. e = parse_entry(line)
  4243. if not e:
  4244. raise EAsciiDoc,'[specialwords] entry in %s is malformed: %s' \
  4245. % (self.fname,line)
  4246. name,wordlist = e
  4247. if not is_name(name):
  4248. raise EAsciiDoc,'[specialwords] name in %s is illegal: %s' \
  4249. % (self.fname,name)
  4250. if wordlist is None:
  4251. # Undefine all words associated with 'name'.
  4252. for k,v in self.specialwords.items():
  4253. if v == name:
  4254. del self.specialwords[k]
  4255. else:
  4256. words = reo.findall(wordlist)
  4257. for word in words:
  4258. word = strip_quotes(word)
  4259. if not is_re(word):
  4260. raise EAsciiDoc,'[specialwords] entry in %s ' \
  4261. 'is not a valid regular expression: %s' \
  4262. % (self.fname,word)
  4263. self.specialwords[word] = name
  4264. def subs_specialchars(self,s):
  4265. """Perform special character substitution on string 's'."""
  4266. """It may seem like a good idea to escape special characters with a '\'
  4267. character, the reason we don't is because the escape character itself
  4268. then has to be escaped and this makes including code listings
  4269. problematic. Use the predefined {amp},{lt},{gt} attributes instead."""
  4270. result = ''
  4271. for ch in s:
  4272. result = result + self.specialchars.get(ch,ch)
  4273. return result
  4274. def subs_specialchars_reverse(self,s):
  4275. """Perform reverse special character substitution on string 's'."""
  4276. result = s
  4277. for k,v in self.specialchars.items():
  4278. result = result.replace(v, k)
  4279. return result
  4280. def subs_specialwords(self,s):
  4281. """Search for word patterns from self.specialwords in 's' and
  4282. substitute using corresponding macro."""
  4283. result = s
  4284. for word in self.specialwords.keys():
  4285. result = re.sub(word, _subs_specialwords, result)
  4286. return result
  4287. def expand_templates(self,entries):
  4288. """Expand any template::[] macros in a list of section entries."""
  4289. result = []
  4290. for line in entries:
  4291. mo = macros.match('+',r'template',line)
  4292. if mo:
  4293. s = mo.group('attrlist')
  4294. if s in self.sections:
  4295. result += self.expand_templates(self.sections[s])
  4296. else:
  4297. message.warning('missing [%s] section' % s)
  4298. else:
  4299. result.append(line)
  4300. return result
  4301. def expand_all_templates(self):
  4302. for k,v in self.sections.items():
  4303. self.sections[k] = self.expand_templates(v)
  4304. def section2tags(self, section, d={}):
  4305. """Perform attribute substitution on 'section' using document
  4306. attributes plus 'd' attributes. Return tuple (stag,etag) containing
  4307. pre and post | placeholder tags."""
  4308. assert section is not None
  4309. if section in self.sections:
  4310. body = self.sections[section]
  4311. else:
  4312. message.warning('missing [%s] section' % section)
  4313. body = ()
  4314. # Split macro body into start and end tag lists.
  4315. stag = []
  4316. etag = []
  4317. in_stag = True
  4318. for s in body:
  4319. if in_stag:
  4320. mo = re.match(r'(?P<stag>.*)\|(?P<etag>.*)',s)
  4321. if mo:
  4322. if mo.group('stag'):
  4323. stag.append(mo.group('stag'))
  4324. if mo.group('etag'):
  4325. etag.append(mo.group('etag'))
  4326. in_stag = False
  4327. else:
  4328. stag.append(s)
  4329. else:
  4330. etag.append(s)
  4331. # Do attribute substitution last so {brkbar} can be used to escape |.
  4332. # But don't do attribute substitution on title -- we've already done it.
  4333. title = d.get('title')
  4334. if title:
  4335. d['title'] = chr(0) # Replace with unused character.
  4336. stag = subs_attrs(stag, d)
  4337. etag = subs_attrs(etag, d)
  4338. # Put the {title} back.
  4339. if title:
  4340. stag = map(lambda x: x.replace(chr(0), title), stag)
  4341. etag = map(lambda x: x.replace(chr(0), title), etag)
  4342. d['title'] = title
  4343. return (stag,etag)
  4344. #---------------------------------------------------------------------------
  4345. # Deprecated old table classes follow.
  4346. # Naming convention is an _OLD name suffix.
  4347. # These will be removed from future versions of AsciiDoc
  4348. #
  4349. def join_lines_OLD(lines):
  4350. """Return a list in which lines terminated with the backslash line
  4351. continuation character are joined."""
  4352. result = []
  4353. s = ''
  4354. continuation = False
  4355. for line in lines:
  4356. if line and line[-1] == '\\':
  4357. s = s + line[:-1]
  4358. continuation = True
  4359. continue
  4360. if continuation:
  4361. result.append(s+line)
  4362. s = ''
  4363. continuation = False
  4364. else:
  4365. result.append(line)
  4366. if continuation:
  4367. result.append(s)
  4368. return result
  4369. class Column_OLD:
  4370. """Table column."""
  4371. def __init__(self):
  4372. self.colalign = None # 'left','right','center'
  4373. self.rulerwidth = None
  4374. self.colwidth = None # Output width in page units.
  4375. class Table_OLD(AbstractBlock):
  4376. COL_STOP = r"(`|'|\.)" # RE.
  4377. ALIGNMENTS = {'`':'left', "'":'right', '.':'center'}
  4378. FORMATS = ('fixed','csv','dsv')
  4379. def __init__(self):
  4380. AbstractBlock.__init__(self)
  4381. self.CONF_ENTRIES += ('template','fillchar','format','colspec',
  4382. 'headrow','footrow','bodyrow','headdata',
  4383. 'footdata', 'bodydata')
  4384. # Configuration parameters.
  4385. self.fillchar=None
  4386. self.format=None # 'fixed','csv','dsv'
  4387. self.colspec=None
  4388. self.headrow=None
  4389. self.footrow=None
  4390. self.bodyrow=None
  4391. self.headdata=None
  4392. self.footdata=None
  4393. self.bodydata=None
  4394. # Calculated parameters.
  4395. self.underline=None # RE matching current table underline.
  4396. self.isnumeric=False # True if numeric ruler.
  4397. self.tablewidth=None # Optional table width scale factor.
  4398. self.columns=[] # List of Columns.
  4399. # Other.
  4400. self.check_msg='' # Message set by previous self.validate() call.
  4401. def load(self,name,entries):
  4402. AbstractBlock.load(self,name,entries)
  4403. """Update table definition from section entries in 'entries'."""
  4404. for k,v in entries.items():
  4405. if k == 'fillchar':
  4406. if v and len(v) == 1:
  4407. self.fillchar = v
  4408. else:
  4409. raise EAsciiDoc,'malformed table fillchar: %s' % v
  4410. elif k == 'format':
  4411. if v in Table_OLD.FORMATS:
  4412. self.format = v
  4413. else:
  4414. raise EAsciiDoc,'illegal table format: %s' % v
  4415. elif k == 'colspec':
  4416. self.colspec = v
  4417. elif k == 'headrow':
  4418. self.headrow = v
  4419. elif k == 'footrow':
  4420. self.footrow = v
  4421. elif k == 'bodyrow':
  4422. self.bodyrow = v
  4423. elif k == 'headdata':
  4424. self.headdata = v
  4425. elif k == 'footdata':
  4426. self.footdata = v
  4427. elif k == 'bodydata':
  4428. self.bodydata = v
  4429. def dump(self):
  4430. AbstractBlock.dump(self)
  4431. write = lambda s: sys.stdout.write('%s%s' % (s,writer.newline))
  4432. write('fillchar='+self.fillchar)
  4433. write('format='+self.format)
  4434. if self.colspec:
  4435. write('colspec='+self.colspec)
  4436. if self.headrow:
  4437. write('headrow='+self.headrow)
  4438. if self.footrow:
  4439. write('footrow='+self.footrow)
  4440. write('bodyrow='+self.bodyrow)
  4441. if self.headdata:
  4442. write('headdata='+self.headdata)
  4443. if self.footdata:
  4444. write('footdata='+self.footdata)
  4445. write('bodydata='+self.bodydata)
  4446. write('')
  4447. def validate(self):
  4448. AbstractBlock.validate(self)
  4449. """Check table definition and set self.check_msg if invalid else set
  4450. self.check_msg to blank string."""
  4451. # Check global table parameters.
  4452. if config.textwidth is None:
  4453. self.check_msg = 'missing [miscellaneous] textwidth entry'
  4454. elif config.pagewidth is None:
  4455. self.check_msg = 'missing [miscellaneous] pagewidth entry'
  4456. elif config.pageunits is None:
  4457. self.check_msg = 'missing [miscellaneous] pageunits entry'
  4458. elif self.headrow is None:
  4459. self.check_msg = 'missing headrow entry'
  4460. elif self.footrow is None:
  4461. self.check_msg = 'missing footrow entry'
  4462. elif self.bodyrow is None:
  4463. self.check_msg = 'missing bodyrow entry'
  4464. elif self.headdata is None:
  4465. self.check_msg = 'missing headdata entry'
  4466. elif self.footdata is None:
  4467. self.check_msg = 'missing footdata entry'
  4468. elif self.bodydata is None:
  4469. self.check_msg = 'missing bodydata entry'
  4470. else:
  4471. # No errors.
  4472. self.check_msg = ''
  4473. def isnext(self):
  4474. return AbstractBlock.isnext(self)
  4475. def parse_ruler(self,ruler):
  4476. """Parse ruler calculating underline and ruler column widths."""
  4477. fc = re.escape(self.fillchar)
  4478. # Strip and save optional tablewidth from end of ruler.
  4479. mo = re.match(r'^(.*'+fc+r'+)([\d\.]+)$',ruler)
  4480. if mo:
  4481. ruler = mo.group(1)
  4482. self.tablewidth = float(mo.group(2))
  4483. self.attributes['tablewidth'] = str(float(self.tablewidth))
  4484. else:
  4485. self.tablewidth = None
  4486. self.attributes['tablewidth'] = '100.0'
  4487. # Guess whether column widths are specified numerically or not.
  4488. if ruler[1] != self.fillchar:
  4489. # If the first column does not start with a fillchar then numeric.
  4490. self.isnumeric = True
  4491. elif ruler[1:] == self.fillchar*len(ruler[1:]):
  4492. # The case of one column followed by fillchars is numeric.
  4493. self.isnumeric = True
  4494. else:
  4495. self.isnumeric = False
  4496. # Underlines must be 3 or more fillchars.
  4497. self.underline = r'^' + fc + r'{3,}$'
  4498. splits = re.split(self.COL_STOP,ruler)[1:]
  4499. # Build self.columns.
  4500. for i in range(0,len(splits),2):
  4501. c = Column_OLD()
  4502. c.colalign = self.ALIGNMENTS[splits[i]]
  4503. s = splits[i+1]
  4504. if self.isnumeric:
  4505. # Strip trailing fillchars.
  4506. s = re.sub(fc+r'+$','',s)
  4507. if s == '':
  4508. c.rulerwidth = None
  4509. else:
  4510. c.rulerwidth = int(validate(s,'int($)>0',
  4511. 'malformed ruler: bad width'))
  4512. else: # Calculate column width from inter-fillchar intervals.
  4513. if not re.match(r'^'+fc+r'+$',s):
  4514. raise EAsciiDoc,'malformed ruler: illegal fillchars'
  4515. c.rulerwidth = len(s)+1
  4516. self.columns.append(c)
  4517. # Fill in unspecified ruler widths.
  4518. if self.isnumeric:
  4519. if self.columns[0].rulerwidth is None:
  4520. prevwidth = 1
  4521. for c in self.columns:
  4522. if c.rulerwidth is None:
  4523. c.rulerwidth = prevwidth
  4524. prevwidth = c.rulerwidth
  4525. def build_colspecs(self):
  4526. """Generate colwidths and colspecs. This can only be done after the
  4527. table arguments have been parsed since we use the table format."""
  4528. self.attributes['cols'] = len(self.columns)
  4529. # Calculate total ruler width.
  4530. totalwidth = 0
  4531. for c in self.columns:
  4532. totalwidth = totalwidth + c.rulerwidth
  4533. if totalwidth <= 0:
  4534. raise EAsciiDoc,'zero width table'
  4535. # Calculate marked up colwidths from rulerwidths.
  4536. for c in self.columns:
  4537. # Convert ruler width to output page width.
  4538. width = float(c.rulerwidth)
  4539. if self.format == 'fixed':
  4540. if self.tablewidth is None:
  4541. # Size proportional to ruler width.
  4542. colfraction = width/config.textwidth
  4543. else:
  4544. # Size proportional to page width.
  4545. colfraction = width/totalwidth
  4546. else:
  4547. # Size proportional to page width.
  4548. colfraction = width/totalwidth
  4549. c.colwidth = colfraction * config.pagewidth # To page units.
  4550. if self.tablewidth is not None:
  4551. c.colwidth = c.colwidth * self.tablewidth # Scale factor.
  4552. if self.tablewidth > 1:
  4553. c.colwidth = c.colwidth/100 # tablewidth is in percent.
  4554. # Build colspecs.
  4555. if self.colspec:
  4556. cols = []
  4557. i = 0
  4558. for c in self.columns:
  4559. i += 1
  4560. self.attributes['colalign'] = c.colalign
  4561. self.attributes['colwidth'] = str(int(c.colwidth))
  4562. self.attributes['colnumber'] = str(i + 1)
  4563. s = subs_attrs(self.colspec,self.attributes)
  4564. if not s:
  4565. message.warning('colspec dropped: contains undefined attribute')
  4566. else:
  4567. cols.append(s)
  4568. self.attributes['colspecs'] = writer.newline.join(cols)
  4569. def split_rows(self,rows):
  4570. """Return a two item tuple containing a list of lines up to but not
  4571. including the next underline (continued lines are joined ) and the
  4572. tuple of all lines after the underline."""
  4573. reo = re.compile(self.underline)
  4574. i = 0
  4575. while not reo.match(rows[i]):
  4576. i = i+1
  4577. if i == 0:
  4578. raise EAsciiDoc,'missing table rows'
  4579. if i >= len(rows):
  4580. raise EAsciiDoc,'closing [%s] underline expected' % self.name
  4581. return (join_lines_OLD(rows[:i]), rows[i+1:])
  4582. def parse_rows(self, rows, rtag, dtag):
  4583. """Parse rows list using the row and data tags. Returns a substituted
  4584. list of output lines."""
  4585. result = []
  4586. # Source rows are parsed as single block, rather than line by line, to
  4587. # allow the CSV reader to handle multi-line rows.
  4588. if self.format == 'fixed':
  4589. rows = self.parse_fixed(rows)
  4590. elif self.format == 'csv':
  4591. rows = self.parse_csv(rows)
  4592. elif self.format == 'dsv':
  4593. rows = self.parse_dsv(rows)
  4594. else:
  4595. assert True,'illegal table format'
  4596. # Substitute and indent all data in all rows.
  4597. stag,etag = subs_tag(rtag,self.attributes)
  4598. for row in rows:
  4599. result.append(' '+stag)
  4600. for data in self.subs_row(row,dtag):
  4601. result.append(' '+data)
  4602. result.append(' '+etag)
  4603. return result
  4604. def subs_row(self, data, dtag):
  4605. """Substitute the list of source row data elements using the data tag.
  4606. Returns a substituted list of output table data items."""
  4607. result = []
  4608. if len(data) < len(self.columns):
  4609. message.warning('fewer row data items then table columns')
  4610. if len(data) > len(self.columns):
  4611. message.warning('more row data items than table columns')
  4612. for i in range(len(self.columns)):
  4613. if i > len(data) - 1:
  4614. d = '' # Fill missing column data with blanks.
  4615. else:
  4616. d = data[i]
  4617. c = self.columns[i]
  4618. self.attributes['colalign'] = c.colalign
  4619. self.attributes['colwidth'] = str(int(c.colwidth))
  4620. self.attributes['colnumber'] = str(i + 1)
  4621. stag,etag = subs_tag(dtag,self.attributes)
  4622. # Insert AsciiDoc line break (' +') where row data has newlines
  4623. # ('\n'). This is really only useful when the table format is csv
  4624. # and the output markup is HTML. It's also a bit dubious in that it
  4625. # assumes the user has not modified the shipped line break pattern.
  4626. subs = self.get_subs()[0]
  4627. if 'replacements' in subs:
  4628. # Insert line breaks in cell data.
  4629. d = re.sub(r'(?m)\n',r' +\n',d)
  4630. d = d.split('\n') # So writer.newline is written.
  4631. else:
  4632. d = [d]
  4633. result = result + [stag] + Lex.subs(d,subs) + [etag]
  4634. return result
  4635. def parse_fixed(self,rows):
  4636. """Parse the list of source table rows. Each row item in the returned
  4637. list contains a list of cell data elements."""
  4638. result = []
  4639. for row in rows:
  4640. data = []
  4641. start = 0
  4642. # build an encoded representation
  4643. row = char_decode(row)
  4644. for c in self.columns:
  4645. end = start + c.rulerwidth
  4646. if c is self.columns[-1]:
  4647. # Text in last column can continue forever.
  4648. # Use the encoded string to slice, but convert back
  4649. # to plain string before further processing
  4650. data.append(char_encode(row[start:]).strip())
  4651. else:
  4652. data.append(char_encode(row[start:end]).strip())
  4653. start = end
  4654. result.append(data)
  4655. return result
  4656. def parse_csv(self,rows):
  4657. """Parse the list of source table rows. Each row item in the returned
  4658. list contains a list of cell data elements."""
  4659. import StringIO
  4660. import csv
  4661. result = []
  4662. rdr = csv.reader(StringIO.StringIO('\r\n'.join(rows)),
  4663. skipinitialspace=True)
  4664. try:
  4665. for row in rdr:
  4666. result.append(row)
  4667. except Exception:
  4668. raise EAsciiDoc,'csv parse error: %s' % row
  4669. return result
  4670. def parse_dsv(self,rows):
  4671. """Parse the list of source table rows. Each row item in the returned
  4672. list contains a list of cell data elements."""
  4673. separator = self.attributes.get('separator',':')
  4674. separator = eval('"'+separator+'"')
  4675. if len(separator) != 1:
  4676. raise EAsciiDoc,'malformed dsv separator: %s' % separator
  4677. # TODO If separator is preceeded by an odd number of backslashes then
  4678. # it is escaped and should not delimit.
  4679. result = []
  4680. for row in rows:
  4681. # Skip blank lines
  4682. if row == '': continue
  4683. # Unescape escaped characters.
  4684. row = eval('"'+row.replace('"','\\"')+'"')
  4685. data = row.split(separator)
  4686. data = [s.strip() for s in data]
  4687. result.append(data)
  4688. return result
  4689. def translate(self):
  4690. message.deprecated('old tables syntax')
  4691. AbstractBlock.translate(self)
  4692. # Reset instance specific properties.
  4693. self.underline = None
  4694. self.columns = []
  4695. attrs = {}
  4696. BlockTitle.consume(attrs)
  4697. # Add relevant globals to table substitutions.
  4698. attrs['pagewidth'] = str(config.pagewidth)
  4699. attrs['pageunits'] = config.pageunits
  4700. # Mix in document attribute list.
  4701. AttributeList.consume(attrs)
  4702. # Validate overridable attributes.
  4703. for k,v in attrs.items():
  4704. if k == 'format':
  4705. if v not in self.FORMATS:
  4706. raise EAsciiDoc, 'illegal [%s] %s: %s' % (self.name,k,v)
  4707. self.format = v
  4708. elif k == 'tablewidth':
  4709. try:
  4710. self.tablewidth = float(attrs['tablewidth'])
  4711. except Exception:
  4712. raise EAsciiDoc, 'illegal [%s] %s: %s' % (self.name,k,v)
  4713. self.merge_attributes(attrs)
  4714. # Parse table ruler.
  4715. ruler = reader.read()
  4716. assert re.match(self.delimiter,ruler)
  4717. self.parse_ruler(ruler)
  4718. # Read the entire table.
  4719. table = []
  4720. while True:
  4721. line = reader.read_next()
  4722. # Table terminated by underline followed by a blank line or EOF.
  4723. if len(table) > 0 and re.match(self.underline,table[-1]):
  4724. if line in ('',None):
  4725. break;
  4726. if line is None:
  4727. raise EAsciiDoc,'closing [%s] underline expected' % self.name
  4728. table.append(reader.read())
  4729. # EXPERIMENTAL: The number of lines in the table, requested by Benjamin Klum.
  4730. self.attributes['rows'] = str(len(table))
  4731. #TODO: Inherited validate() doesn't set check_msg, needs checking.
  4732. if self.check_msg: # Skip if table definition was marked invalid.
  4733. message.warning('skipping %s table: %s' % (self.name,self.check_msg))
  4734. return
  4735. # Generate colwidths and colspecs.
  4736. self.build_colspecs()
  4737. # Generate headrows, footrows, bodyrows.
  4738. # Headrow, footrow and bodyrow data replaces same named attributes in
  4739. # the table markup template. In order to ensure this data does not get
  4740. # a second attribute substitution (which would interfere with any
  4741. # already substituted inline passthroughs) unique placeholders are used
  4742. # (the tab character does not appear elsewhere since it is expanded on
  4743. # input) which are replaced after template attribute substitution.
  4744. headrows = footrows = []
  4745. bodyrows,table = self.split_rows(table)
  4746. if table:
  4747. headrows = bodyrows
  4748. bodyrows,table = self.split_rows(table)
  4749. if table:
  4750. footrows,table = self.split_rows(table)
  4751. if headrows:
  4752. headrows = self.parse_rows(headrows, self.headrow, self.headdata)
  4753. headrows = writer.newline.join(headrows)
  4754. self.attributes['headrows'] = '\x07headrows\x07'
  4755. if footrows:
  4756. footrows = self.parse_rows(footrows, self.footrow, self.footdata)
  4757. footrows = writer.newline.join(footrows)
  4758. self.attributes['footrows'] = '\x07footrows\x07'
  4759. bodyrows = self.parse_rows(bodyrows, self.bodyrow, self.bodydata)
  4760. bodyrows = writer.newline.join(bodyrows)
  4761. self.attributes['bodyrows'] = '\x07bodyrows\x07'
  4762. table = subs_attrs(config.sections[self.template],self.attributes)
  4763. table = writer.newline.join(table)
  4764. # Before we finish replace the table head, foot and body place holders
  4765. # with the real data.
  4766. if headrows:
  4767. table = table.replace('\x07headrows\x07', headrows, 1)
  4768. if footrows:
  4769. table = table.replace('\x07footrows\x07', footrows, 1)
  4770. table = table.replace('\x07bodyrows\x07', bodyrows, 1)
  4771. writer.write(table,trace='table')
  4772. class Tables_OLD(AbstractBlocks):
  4773. """List of tables."""
  4774. BLOCK_TYPE = Table_OLD
  4775. PREFIX = 'old_tabledef-'
  4776. def __init__(self):
  4777. AbstractBlocks.__init__(self)
  4778. def load(self,sections):
  4779. AbstractBlocks.load(self,sections)
  4780. def validate(self):
  4781. # Does not call AbstractBlocks.validate().
  4782. # Check we have a default table definition,
  4783. for i in range(len(self.blocks)):
  4784. if self.blocks[i].name == 'old_tabledef-default':
  4785. default = self.blocks[i]
  4786. break
  4787. else:
  4788. raise EAsciiDoc,'missing [OLD_tabledef-default] section'
  4789. # Set default table defaults.
  4790. if default.format is None: default.subs = 'fixed'
  4791. # Propagate defaults to unspecified table parameters.
  4792. for b in self.blocks:
  4793. if b is not default:
  4794. if b.fillchar is None: b.fillchar = default.fillchar
  4795. if b.format is None: b.format = default.format
  4796. if b.template is None: b.template = default.template
  4797. if b.colspec is None: b.colspec = default.colspec
  4798. if b.headrow is None: b.headrow = default.headrow
  4799. if b.footrow is None: b.footrow = default.footrow
  4800. if b.bodyrow is None: b.bodyrow = default.bodyrow
  4801. if b.headdata is None: b.headdata = default.headdata
  4802. if b.footdata is None: b.footdata = default.footdata
  4803. if b.bodydata is None: b.bodydata = default.bodydata
  4804. # Check all tables have valid fill character.
  4805. for b in self.blocks:
  4806. if not b.fillchar or len(b.fillchar) != 1:
  4807. raise EAsciiDoc,'[%s] missing or illegal fillchar' % b.name
  4808. # Build combined tables delimiter patterns and assign defaults.
  4809. delimiters = []
  4810. for b in self.blocks:
  4811. # Ruler is:
  4812. # (ColStop,(ColWidth,FillChar+)?)+, FillChar+, TableWidth?
  4813. b.delimiter = r'^(' + Table_OLD.COL_STOP \
  4814. + r'(\d*|' + re.escape(b.fillchar) + r'*)' \
  4815. + r')+' \
  4816. + re.escape(b.fillchar) + r'+' \
  4817. + '([\d\.]*)$'
  4818. delimiters.append(b.delimiter)
  4819. if not b.headrow:
  4820. b.headrow = b.bodyrow
  4821. if not b.footrow:
  4822. b.footrow = b.bodyrow
  4823. if not b.headdata:
  4824. b.headdata = b.bodydata
  4825. if not b.footdata:
  4826. b.footdata = b.bodydata
  4827. self.delimiters = re_join(delimiters)
  4828. # Check table definitions are valid.
  4829. for b in self.blocks:
  4830. b.validate()
  4831. if config.verbose:
  4832. if b.check_msg:
  4833. message.warning('[%s] table definition: %s' % (b.name,b.check_msg))
  4834. # End of deprecated old table classes.
  4835. #---------------------------------------------------------------------------
  4836. #---------------------------------------------------------------------------
  4837. # Application code.
  4838. #---------------------------------------------------------------------------
  4839. # Constants
  4840. # ---------
  4841. APP_FILE = None # This file's full path.
  4842. APP_DIR = None # This file's directory.
  4843. USER_DIR = None # ~/.asciidoc
  4844. # Global configuration files directory (set by Makefile build target).
  4845. CONF_DIR = '/etc/asciidoc'
  4846. DATA_DIR = '/usr/share/asciidoc'
  4847. HELP_FILE = 'help.conf' # Default (English) help file.
  4848. # Globals
  4849. # -------
  4850. document = Document() # The document being processed.
  4851. config = Config() # Configuration file reader.
  4852. reader = Reader() # Input stream line reader.
  4853. writer = Writer() # Output stream line writer.
  4854. message = Message() # Message functions.
  4855. paragraphs = Paragraphs() # Paragraph definitions.
  4856. lists = Lists() # List definitions.
  4857. blocks = DelimitedBlocks() # DelimitedBlock definitions.
  4858. tables_OLD = Tables_OLD() # Table_OLD definitions.
  4859. tables = Tables() # Table definitions.
  4860. macros = Macros() # Macro definitions.
  4861. calloutmap = CalloutMap() # Coordinates callouts and callout list.
  4862. trace = Trace() # Implements trace attribute processing.
  4863. ### Used by asciidocapi.py ###
  4864. # List of message strings written to stderr.
  4865. messages = message.messages
  4866. def asciidoc(backend, doctype, confiles, infile, outfile, options):
  4867. """Convert AsciiDoc document to DocBook document of type doctype
  4868. The AsciiDoc document is read from file object src the translated
  4869. DocBook file written to file object dst."""
  4870. try:
  4871. if doctype not in ('article','manpage','book'):
  4872. raise EAsciiDoc,'illegal document type'
  4873. document.backend = backend
  4874. if not os.path.exists(os.path.join(APP_DIR, backend+'.conf')) and not \
  4875. os.path.exists(os.path.join(CONF_DIR, backend+'.conf')):
  4876. message.warning('non-standard %s backend' % backend, linenos=False)
  4877. document.doctype = doctype
  4878. document.infile = infile
  4879. document.update_attributes()
  4880. # Set processing options.
  4881. for o in options:
  4882. if o == '-c': config.dumping = True
  4883. if o == '-s': config.header_footer = False
  4884. if o == '-v': config.verbose = True
  4885. # Check the infile exists.
  4886. if infile != '<stdin>' and not os.path.isfile(infile):
  4887. raise EAsciiDoc,'input file %s missing' % infile
  4888. if '-e' not in options:
  4889. config.load_all()
  4890. if infile != '<stdin>':
  4891. # Load implicit document specific configuration files if they exist.
  4892. config.load_file(os.path.splitext(infile)[0] + '.conf')
  4893. config.load_file(os.path.splitext(infile)[0] + '-' + backend + '.conf')
  4894. # If user specified configuration file(s) overlay the defaults.
  4895. if confiles:
  4896. for conf in confiles:
  4897. if os.path.isfile(conf):
  4898. config.load_file(conf)
  4899. else:
  4900. raise EAsciiDoc,'configuration file %s missing' % conf
  4901. document.update_attributes()
  4902. # Check configuration for consistency.
  4903. config.validate()
  4904. # Build outfile name now all conf files have been read.
  4905. if outfile is None:
  4906. outfile = os.path.splitext(infile)[0] + '.' + backend
  4907. if config.outfilesuffix:
  4908. # Change file extension.
  4909. outfile = os.path.splitext(outfile)[0] + config.outfilesuffix
  4910. document.outfile = outfile
  4911. if config.dumping:
  4912. config.dump()
  4913. else:
  4914. reader.tabsize = config.tabsize
  4915. reader.open(infile)
  4916. try:
  4917. writer.newline = config.newline
  4918. writer.open(outfile)
  4919. try:
  4920. AttributeList.initialize()
  4921. paragraphs.initialize()
  4922. lists.initialize()
  4923. document.update_attributes() # Add file name related.
  4924. document.translate()
  4925. finally:
  4926. writer.close()
  4927. finally:
  4928. reader.closefile() # Keep reader state for postmortem.
  4929. except KeyboardInterrupt:
  4930. raise
  4931. except Exception,e:
  4932. # Cleanup.
  4933. if outfile and outfile != '<stdout>' and os.path.isfile(outfile):
  4934. os.unlink(outfile)
  4935. # Build and print error description.
  4936. msg = 'FAILED: '
  4937. if reader.cursor:
  4938. msg = msg + message.format('')
  4939. if isinstance(e,EAsciiDoc):
  4940. message.stderr(msg+str(e))
  4941. else:
  4942. if __name__ == '__main__':
  4943. message.stderr(msg+'unexpected error:')
  4944. message.stderr('-'*60)
  4945. traceback.print_exc(file=sys.stderr)
  4946. message.stderr('-'*60)
  4947. else:
  4948. message.stderr('%sunexpected error: %s' % (msg,str(e)))
  4949. sys.exit(1)
  4950. def usage(msg=''):
  4951. if msg:
  4952. message.stderr(msg)
  4953. show_help('default', sys.stderr)
  4954. def show_help(topic, f=None):
  4955. """Print help topic to file object f."""
  4956. if f is None:
  4957. f = sys.stdout
  4958. # Select help file.
  4959. lang = config.cmd_attrs.get('lang')
  4960. if lang and lang != 'en':
  4961. help_file = 'help-' + lang + '.conf'
  4962. else:
  4963. help_file = HELP_FILE
  4964. # Print [topic] section from help file.
  4965. topics = OrderedDict()
  4966. load_conf_file(topics, help_file, CONF_DIR)
  4967. load_conf_file(topics, help_file, APP_DIR)
  4968. if USER_DIR is not None:
  4969. load_conf_file(topics, help_file, USER_DIR)
  4970. if len(topics) == 0:
  4971. # Default to English if specified language help files not found.
  4972. help_file = HELP_FILE
  4973. load_conf_file(topics, help_file, CONF_DIR)
  4974. load_conf_file(topics, help_file, APP_DIR)
  4975. if len(topics) == 0:
  4976. message.stderr('no help topics found')
  4977. sys.exit(1)
  4978. n = 0
  4979. for k in topics.keys():
  4980. if re.match(re.escape(topic), k):
  4981. n += 1
  4982. lines = topics[k]
  4983. if n == 0:
  4984. message.stderr('help topic not found: [%s] in %s' % (topic, help_file))
  4985. message.stderr('available help topics: %s' % ', '.join(topics.keys()))
  4986. sys.exit(1)
  4987. elif n > 1:
  4988. message.stderr('ambiguous help topic: %s' % topic)
  4989. else:
  4990. for line in lines:
  4991. print >>f, line
  4992. ### Used by asciidocapi.py ###
  4993. def execute(cmd,opts,args):
  4994. """
  4995. Execute asciidoc with command-line options and arguments.
  4996. cmd is asciidoc command or asciidoc.py path.
  4997. opts and args conform to values returned by getopt.getopt().
  4998. Raises SystemExit if an error occurs.
  4999. Doctests:
  5000. 1. Check execution:
  5001. >>> import StringIO
  5002. >>> infile = StringIO.StringIO('Hello *{author}*')
  5003. >>> outfile = StringIO.StringIO()
  5004. >>> opts = []
  5005. >>> opts.append(('--backend','html4'))
  5006. >>> opts.append(('--no-header-footer',None))
  5007. >>> opts.append(('--attribute','author=Joe Bloggs'))
  5008. >>> opts.append(('--out-file',outfile))
  5009. >>> execute(__file__, opts, [infile])
  5010. >>> print outfile.getvalue()
  5011. <p>Hello <strong>Joe Bloggs</strong></p>
  5012. >>>
  5013. """
  5014. if float(sys.version[:3]) < MIN_PYTHON_VERSION:
  5015. message.stderr('FAILED: Python 2.3 or better required')
  5016. sys.exit(1)
  5017. if not os.path.exists(cmd):
  5018. message.stderr('FAILED: Missing asciidoc command: %s' % cmd)
  5019. sys.exit(1)
  5020. # Locate the executable and configuration files directory.
  5021. global APP_FILE
  5022. APP_FILE = os.path.realpath(cmd)
  5023. global APP_DIR
  5024. APP_DIR = os.path.dirname(APP_FILE)
  5025. global USER_DIR
  5026. USER_DIR = os.environ.get('HOME')
  5027. if USER_DIR is not None:
  5028. USER_DIR = os.path.join(USER_DIR,'.asciidoc')
  5029. if not os.path.isdir(USER_DIR):
  5030. USER_DIR = None
  5031. if len(args) > 1:
  5032. usage('To many arguments')
  5033. sys.exit(1)
  5034. backend = DEFAULT_BACKEND
  5035. doctype = DEFAULT_DOCTYPE
  5036. confiles = []
  5037. outfile = None
  5038. options = []
  5039. help_option = False
  5040. for o,v in opts:
  5041. if o in ('--help','-h'):
  5042. help_option = True
  5043. if o == '--safe':
  5044. document.safe = True
  5045. if o == '--version':
  5046. print('asciidoc %s' % VERSION)
  5047. sys.exit(0)
  5048. if o in ('-b','--backend'):
  5049. backend = v
  5050. if o in ('-c','--dump-conf'):
  5051. options.append('-c')
  5052. if o in ('-d','--doctype'):
  5053. doctype = v
  5054. if o in ('-e','--no-conf'):
  5055. options.append('-e')
  5056. if o in ('-f','--conf-file'):
  5057. confiles.append(v)
  5058. if o in ('-n','--section-numbers'):
  5059. o = '-a'
  5060. v = 'numbered'
  5061. if o in ('-a','--attribute'):
  5062. e = parse_entry(v, allow_name_only=True)
  5063. if not e:
  5064. usage('Illegal -a option: %s' % v)
  5065. sys.exit(1)
  5066. k,v = e
  5067. # A @ suffix denotes don't override existing document attributes.
  5068. if v and v[-1] == '@':
  5069. document.attributes[k] = v[:-1]
  5070. else:
  5071. config.cmd_attrs[k] = v
  5072. if o in ('-o','--out-file'):
  5073. outfile = v
  5074. if o in ('-s','--no-header-footer'):
  5075. options.append('-s')
  5076. if o in ('-v','--verbose'):
  5077. options.append('-v')
  5078. if help_option:
  5079. if len(args) == 0:
  5080. show_help('default')
  5081. else:
  5082. show_help(args[-1])
  5083. sys.exit(0)
  5084. if len(args) == 0 and len(opts) == 0:
  5085. usage()
  5086. sys.exit(0)
  5087. if len(args) == 0:
  5088. usage('No source file specified')
  5089. sys.exit(1)
  5090. if not backend:
  5091. usage('No --backend option specified')
  5092. sys.exit(1)
  5093. stdin,stdout = sys.stdin,sys.stdout
  5094. try:
  5095. infile = args[0]
  5096. if infile == '-':
  5097. infile = '<stdin>'
  5098. elif isinstance(infile, str):
  5099. infile = os.path.abspath(infile)
  5100. else: # Input file is file object from API call.
  5101. sys.stdin = infile
  5102. infile = '<stdin>'
  5103. if outfile == '-':
  5104. outfile = '<stdout>'
  5105. elif isinstance(outfile, str):
  5106. outfile = os.path.abspath(outfile)
  5107. elif outfile is None:
  5108. if infile == '<stdin>':
  5109. outfile = '<stdout>'
  5110. else: # Output file is file object from API call.
  5111. sys.stdout = outfile
  5112. outfile = '<stdout>'
  5113. # Do the work.
  5114. asciidoc(backend, doctype, confiles, infile, outfile, options)
  5115. if document.has_errors:
  5116. sys.exit(1)
  5117. finally:
  5118. sys.stdin,sys.stdout = stdin,stdout
  5119. if __name__ == '__main__':
  5120. # Process command line options.
  5121. import getopt
  5122. try:
  5123. #DEPRECATED: --safe option.
  5124. opts,args = getopt.getopt(sys.argv[1:],
  5125. 'a:b:cd:ef:hno:svw:',
  5126. ['attribute=','backend=','conf-file=','doctype=','dump-conf',
  5127. 'help','no-conf','no-header-footer','out-file=',
  5128. 'section-numbers','verbose','version','safe','unsafe',
  5129. 'doctest'])
  5130. except getopt.GetoptError:
  5131. usage('illegal command options')
  5132. sys.exit(1)
  5133. if '--doctest' in [opt[0] for opt in opts]:
  5134. # Run module doctests.
  5135. import doctest
  5136. options = doctest.NORMALIZE_WHITESPACE + doctest.ELLIPSIS
  5137. failures,tries = doctest.testmod(optionflags=options)
  5138. if failures == 0:
  5139. message.stderr('All doctests passed')
  5140. exit(0)
  5141. else:
  5142. exit(1)
  5143. try:
  5144. execute(sys.argv[0],opts,args)
  5145. except KeyboardInterrupt:
  5146. message.stderr()