source: rtems-source-builder/source-builder/sb/asciidoc/examples/website/asciidocapi.txt @ 0464153

4.104.114.95
Last change on this file since 0464153 was 0464153, checked in by Chris Johns <chrisj@…>, on 03/03/13 at 04:58:11

Change asciidoc to the 8.6.4 release package because Windows was broken.

  • Property mode set to 100644
File size: 6.2 KB
Line 
1AsciiDoc API
2============
3
4'asciidocapi' -- a Python API module for 'AsciiDoc'.
5
6
7Introduction
8------------
9The 'asciidocapi' module implements a Python API for AsciiDoc. It
10allows you to set `asciidoc(1)` program options, compile an AsciiDoc
11source file and then interrogate the results.  The `asciidocapi.py`
12module file contains the `AsciiDocAPI` wrapper class for
13`asciidoc.py`.
14
15.Benefits
16- Stable API Shields the user from the undocumented and possibly
17  volatile `asciidoc.py` internals.
18- Easier to use and more flexible than the alternative of running
19  `asciidoc(1)` as a separate process.
20- Executes inside your application (better performance than running
21  separate `asciidoc(1)` command processes).
22
23
24Using asciidocapi
25-----------------
26To use the API just drop the `asciidocapi.py` file into your
27application directory, import it and use the `AsciiDocAPI` class.  The
28only requirement is that a compatible version of 'AsciiDoc' is already
29installed -- simple, no setuptools to run, no Eggs to install, no
30non-standard library dependencies.
31
32You can find `asciidocapi.py` in the AsciiDoc
33http://www.methods.co.nz/asciidoc/INSTALL.html#X1[distribution
34archives] (version 8.4.1 or better).
35
36Once you have `asciidocapi.py` Verify everything is working by running
37the module doctests:
38
39  python asciidocapi.py
40
41If there are no messages then all is well.
42
43The following minimal example compiles `mydoc.txt` to `mydoc.html`:
44
45[source,python]
46-------------------------------------------------------------------------------
47from asciidocapi import AsciiDocAPI
48asciidoc = AsciiDocAPI()
49asciidoc.execute('mydoc.txt')
50-------------------------------------------------------------------------------
51
52The next interactive example uses file-like objects for input and output:
53
54-------------------------------------------------------------------------------
55$ python
56Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
57[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
58Type "help", "copyright", "credits" or "license" for more information.
59>>> from asciidocapi import AsciiDocAPI
60>>> import StringIO
61>>> infile = StringIO.StringIO('Hello *{author}*')
62>>> outfile = StringIO.StringIO()
63>>> asciidoc = AsciiDocAPI()
64>>> asciidoc.options('--no-header-footer')
65>>> asciidoc.attributes['author'] = 'Joe Bloggs'
66>>> asciidoc.execute(infile, outfile, backend='html4')
67>>> print outfile.getvalue()
68<p>Hello <strong>Joe Bloggs</strong></p>
69
70>>>
71-------------------------------------------------------------------------------
72
73
74Implementation Rationale
75------------------------
76.Leverage existing knowledge
77The API maps directly onto the `asciidoc(1)` command -- this is
78deliberate -- if you know the `asciidoc(1)` command learning the API
79will be trivial. A nice side effect of this goal is that API and
80command-line modes share the same code -- virtually no `asciidoc(1)`
81code is specific to API usage.
82
83.Simplicity
84Implemented with a single Python module file (`asciidocapi.py`)
85containing the 'AsciiDocAPI' API class. 'AsciiDocAPI'  contains just
86one method plus a few attributes for processing options and result
87messages.  No external setup tools and no non-standard library
88dependencies are used or required.
89
90.Loose coupling
91The dependency between `asciidocapi.py` and `asciidoc.py` is minimal
92-- the current `asciidocapi.py` module uses only two attributes and
93one function from the `asciidoc.py` module.
94
95.Why isn't the API baked right into the asciidoc.py command script?
961. You can't just drop `asciidoc.py` into your application because it
97   requires all the related config files and filters -- complex and
98   unnecessary since all this was already done when you installed
99   AsciiDoc.
1002. This scheme separates the API from the AsciiDoc application -- the
101   API implementation can be extended or replaced independently of
102   AsciiDoc.
103
104
105API reference
106-------------
107
108[[X2]]
109Class `AsciiDocAPI(object)`
110~~~~~~~~~~~~~~~~~~~~~~~~~~~
111This is the 'AsciiDoc' API class.
112
113Instance attributes
114^^^^^^^^^^^^^^^^^^^
115`asciidoc`::
116The imported `asciidoc.py` module.
117
118`attributes`::
119A dictionary of AsciiDoc attribute values passed to AsciiDoc.
120
121- Setting an attribute value to `None` (`name: None`) will undefine
122  (delete) the attribute (this in addition to the `name!` attribute
123  name format that the `asciidoc(1)` command uses).
124- To simply define an attribute set the attribute value to a blank
125  string (`name: ''`)
126
127`cmd`::
128The file path of the `asciidoc.py` script. Set by the `__init__`
129method.
130
131`messages`::
132A chronologically ordered list of message strings generated during
133AsciiDoc execution (last message at the end of the list).
134
135`options`::
136An instance of the <<X1,Options class>>. Contains a list of command
137options passed to AsciiDoc.
138
139Instance methods
140^^^^^^^^^^^^^^^^
141`__init__(self, asciidoc_py=None)`::
142Locate and import `asciidoc.py` module and verify API compatibility.
143Initialize instance attributes. A search for the `asciidoc` module is
144made in the following order:
145
146. Use the `ASCIIDOC_PY` environment variable if it is set.
147. Use the `asciidoc_py` argument if it is set.
148. Search the environment 'PATH' for `asciidoc.py`, `asciidoc.pyc` and
149  `asciidoc` (in that order).
150. Finally repeat the previous search in the current working directory.
151
152`execute(self, infile, outfile=None, backend=None)`::
153Compile `infile` to `outfile` using `backend` format.  `infile` and
154`outfile` can be file path strings or file-like objects. `backend` is
155name of 'AsciiDoc' backend (takes same values as `asciidoc(1)` command
156`--backend` option). If `outfile` or `backend` are `None` then their
157respective `asciidoc(1)` defaults are used.
158
159
160[[X1]]
161Class `Options(object)`
162~~~~~~~~~~~~~~~~~~~~~~~
163Stores `asciidoc(1)` command options. You can use any `asciidoc(1)`
164options with the exception of the `--doctest` and `--filter` options.
165
166Instance attributes
167^^^^^^^^^^^^^^^^^^^
168`values`::
169The list of `(name,value)` command option tuples.
170
171Instance methods
172^^^^^^^^^^^^^^^^
173`__call__(self, name, value=None)`::
174A shortcut for the `append` method. Example:
175
176  opts = Options()
177  opts('--verbose')
178
179`append(self, name, value=None)`::
180Append `(name,value)` to the options list. Example:
181
182  opts = Options()
183  opts.append('--conf-file', 'blog.conf')
184
185
186Class `AsciiDocError(Exception)`
187~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
188Thrown by the <<X2,AsciiDocAPI class>> when an 'AsciiDoc' execution
189error occurs.
Note: See TracBrowser for help on using the repository browser.