PageRenderTime 104ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/dev/asciidoc/asciidoc.py

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