diff --git a/.gitignore b/.gitignore index 0d20b64..cec05fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ *.pyc +build/ +dist/ +zippydoc.egg-info/ diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..c011d97 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include src/zippydoc/data/* diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..920e824 --- /dev/null +++ b/setup.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python + +""" +distutils/setuptools install script. See inline comments for packaging documentation. +""" + +import os +import sys + +try: + from setuptools import setup + # hush pyflakes + setup +except ImportError: + from distutils.core import setup + +try: + from distutils.command.build_py import build_py_2to3 as build_py +except ImportError: + from distutils.command.build_py import build_py + +packages = ['zippydoc'] + +package_dir = {"zippydoc": "src/zippydoc"} + +package_data = {"zippydoc": ["data/template.html"]} + +scripts = [ + 'zpy2html' +] + +print repr(package_data) + +setup( + name='zippydoc', + version='1.0', + maintainer='Sven Slootweg', + maintainer_email='admin@cryto.net', + description='Documentation markup language and library, including HTML converter.', + url='http://www.cryto.net/zippydoc', + packages=packages, + package_dir=package_dir, + package_data=package_data, + include_package_data=True, + scripts=scripts, + install_requires=['argparse'], + cmdclass={'build_py': build_py} +) + diff --git a/site/documentation.html b/site/documentation.html new file mode 100644 index 0000000..1110b69 --- /dev/null +++ b/site/documentation.html @@ -0,0 +1,285 @@ + + + + + + +

ZippyDoc format documentation

Download the ZippyDoc source of this page.
ZippyDoc is a compact, light-weight and code-oriented documentation markup language. It allows you to easily write documentation for your code or APIs, and batch-convert it to HTML.

Table of contents

Format overview

ZippyDoc is a paragraph-oriented format, much like Markdown. Each paragraph represents a "block" of something, and no linebreaks are used anywhere - to start on a new line, you simply start out with a new paragraph. A block is indicated by a specific prefix. Tabs (not spaces!) are used to indent blocks and indicate children of previous blocks. A new "paragraph" is started by having two or more successive line-endings - this basically comes down to at least one empty line inbetween paragraphs.
There is also some inline markup available, including emphasis, strong text, and hyperlinks to both other ZippyDoc documents and external locations.

Blocks

Several block types exist. Some of them have "continuation characters" that indicate the continuation of the previous block in a new paragraph, as opposed to starting a new block.
Definition block
A definition block is prefixed with a caret, and contains something along the lines of a function definition or type. In this particular document, it is used to denote the types of blocks and markup elements. Inline markup (such as the emphasis for arguments in the below example) is applied. These blocks are used for a table of contents if you choose to use one.
Example: Using a definition block
Code:
^ my_function(**argument**, **another_argument**)
+
+	Some kind of text describing the function goes here.
You can also have an alternative notation for a definition. This can be useful if you have aliased functions, or multiple input forms. The alternative notation should be put on the same indentation as the original definition, but without a caret in front. The alternative notations (there is no limit) are shown separately in the table of contents if you choose to use one. You may indent the alternative notations with spaces to make the definition easier to read.
Example: Using a definition block
Code:
^ my_function(**argument**, **another_argument**)
+  alt_func(**argument**, **another_argument**)
+
+	Some kind of text describing the function goes here.
Argument block
An argument block shows a particular argument or parameter, and its explanation. The argument name is suffixed with a double colon (::), and the explanation follows on the next line. If so desired, the explanation can be indented with a tab - the tabs in front of the explanation will be eaten by the parser. The explanation can also consist of multiple lines - the newlines are preserved in the HTML version. Inline markup is applied only in the explanation.
Example: Using an argument block
Code:
^ my_function(**argument**, **another_argument**)
+
+	Some kind of text describing the function goes here.
+
+	argument::
+		This is the first argument to this example function.
+
+	another_argument::
+		This is the second argument to this example function.
+		As you can see, it's possible to split the explanation over multiple lines as well.
Example block
An example block shows an example of the function you are documenting, with code and output. The example block itself is prefixed with an @, and only specifies a title; you will use indented children of the Code and Output block variety to show respectively the code and the output for the example. These blocks will be explained later on. The title of your example block will be prefixed with "Example:" in the output automatically.
Example: Using an example block
Code:
^ my_function(**argument**, **another_argument**)
+
+	Some kind of text describing the function goes here.
+
+	argument::
+		This is the first argument to this example function.
+
+	another_argument::
+		This is the second argument to this example function.
+		As you can see, it's possible to split the explanation over multiple lines as well.
+
+	@ Using this function
+
+		$ Some code goes here.
+
+		> Some output goes here.
Code block
Important: This block cannot have child elements!
The code block is used in an example to show example code. It is prefixed with a dollar sign ($), and all text following it will be show on the HTML page verbatim, without any further markup processing being done. It even allows you to display ZippyDoc formatting characters without having them interpreted, as is done on this page!
Example: Using a code block
Code:
^ my_function(**argument**, **another_argument**)
+
+	Some kind of text describing the function goes here.
+
+	argument::
+		This is the first argument to this example function.
+
+	another_argument::
+		This is the second argument to this example function.
+		As you can see, it's possible to split the explanation over multiple lines as well.
+
+	@ Using this function
+
+		$ my_function(42, "The answer to everything")
+
+		> Some output goes here.
It is also possible to have a code block spanning multiple paragraphs, without each paragraph being broken up into a separate code block (as would normally happen if you just used the dollar sign). To do this, you can use two dollar signs at the start of the block. Note that after these two dollar signs, whitespace (except for spaces) is not eaten, meaning you can use tabs to indent further blocks of your code!
Example: Using a multi-paragraph code block
Code:
^ my_function(**argument**, **another_argument**)
+
+	Some kind of text describing the function goes here.
+
+	argument::
+		This is the first argument to this example function.
+
+	another_argument::
+		This is the second argument to this example function.
+		As you can see, it's possible to split the explanation over multiple lines as well.
+
+	@ Using this function
+
+		$ if some_variable == True:
+
+		$$	my_function(42, "The answer to everything")
+
+		> Some output goes here.
Output block
Important: This block cannot have child elements!
The output block is used to display sample output in an example. Just like the code block, it is shown exactly as it originally was, without any further formatting applied. It is prefixed by a >, and like the code block it has a continuation character - in this case, that is >>.
Example: Using an output block
Code:
^ my_function(**argument**, **another_argument**)
+
+	Some kind of text describing the function goes here.
+
+	argument::
+		This is the first argument to this example function.
+
+	another_argument::
+		This is the second argument to this example function.
+		As you can see, it's possible to split the explanation over multiple lines as well.
+
+	@ Using this function
+
+		$ my_function(42, "The answer to everything")
+
+		> The answer to everything is 42!
Example: Using a multi-paragraph output block
Code:
^ my_function(**argument**, **another_argument**)
+
+	Some kind of text describing the function goes here.
+
+	argument::
+		This is the first argument to this example function.
+
+	another_argument::
+		This is the second argument to this example function.
+		As you can see, it's possible to split the explanation over multiple lines as well.
+
+	@ Using this function
+
+		$ my_function(42, "The answer to everything")
+
+		> The answer to everything is 42!
+
+		>> Did you know The answer to everything is 42?
Exclamation block
The exclamation block allows you to mark a block of text as "important". In the standard HTML layout, it will have a yellow-ish background, and will be prefixed with "Important!". It is prefixed with an exclamation mark (!). Inline markup is applied.
Example: Using an exclamation block
Code:
^ my_function(**argument**, **another_argument**)
+
+	Some kind of text describing the function goes here.
+
+	! Only ever use this function with the number '42'!
+
+	argument::
+		This is the first argument to this example function.
+
+	another_argument::
+		This is the second argument to this example function.
+		As you can see, it's possible to split the explanation over multiple lines as well.
+
+	@ Using this function
+
+		$ my_function(42, "The answer to everything")
+
+		> The answer to everything is 42!
Header block
Important: This block cannot have child elements!
A header block is a generic header to indicate the start of a new section. It is treated as a separate element, not as a "container". The header blocks in ZippyDoc work similarly to those in Markdown: they are prefixed by a hash (#), and the amount of hash characters defines what level of header it is.
Example: Using header blocks
Code:
# This is a level 1 (largest) header.
+
+## This is a level 2 header.
+
+...
+
+####### This is a level 7 (smallest) header.
Text block
Important: This block cannot have child elements!
A text block is any block that is not prefixed by a special character. It is shown as defined, with inline markup applied.

Inline markup

There are also various forms of inline markup that you can use in your documentation.
Emphasized text
Emphasized text is typically displayed as italic. You can emphasize text by enclosing it in two asterisks on each side.
Example: Emphasizing text
Code:
This is just some text, and **this part is emphasized**.
Strong text
Strong text is typically displayed as bold. You can make text strong by enclosing it in two underscores on each side.
Example: Making text strong
Code:
This is just some text, __and this part is strong__.
Internal references (hyperlinks)
Internal references are hyperlinks that point to other documents in the same documentation set. Depending on the export format (currently only HTML is supported), the appropriate extension is automatically appended. The paths should resemble the directory structure you are storing the ZippyDoc source files in. The target of the reference is enclosed in curly braces and prefixed with a >. If you wish to give the reference a friendly description, you can do so by appending it, enclosed in parentheses.
Example: Referencing another documentation page
Code:
You can also view the API documentation at {>api/index}.
Example: Referencing another documentation page with a friendly description
Code:
You can also view the {>api/index}(API documentation).
External references (hyperlinks)
External references are hyperlinks just like the internal references, but they refer to an external resources. The syntax is identical to that of internal references, except for the > disappearing. Note that external references are only picked up when the text enclosed in the braces is an actual URI of some sort.
You can also force an external reference to be created by prefixing the URI with <. This is useful when you want to for example link to a download relative to the current page.
Example: Referencing Google
Code:
You could also search {http://www.google.com/}.
Example: Referencing Google with a friendly description
Code:
You could also search {http://www.google.com/}(Google).
Example: Referencing a relative file that is not a ZippyDoc document
Code:
You can download it by {<file.zip}(clicking here).
Fixed-width text
Fixed-width text can be useful to indicate code elements or other things that would benefit from being displayed in a terminal-like font. You can make text fixed-width by enclosing it in backticks.
Example: Fixed-width text
Code:
Now enter `./run.sh` into your terminal.

Special tags

Currently there is only one special tag. Special tags can be inserted anywhere in the document to insert a generated element.
Table of contents
To insert a table of contents that is automatically generated from all definition blocks on the page, simply insert {TOC} on the page where you want it to appear (it has to be in its own paragraph). Typically you will want to place it just below the main page header.
Every item in a table of contents will be followed by a snippet of text, that is grabbed from the first Text block for that definition. Alternative notations are shown after this description.
Example: Including a table of contents
Code:
# Sample documentation
+
+{TOC}
+
+^ my_function(**argument**, **another_argument**)
+
+	Some kind of text describing the function goes here.
+
+	...

Full example

You can view a full example here of a ZippyDoc source file and its result.
+ + diff --git a/site/documentation.zpy b/site/documentation.zpy new file mode 100644 index 0000000..7304ec2 --- /dev/null +++ b/site/documentation.zpy @@ -0,0 +1,333 @@ +# ZippyDoc format documentation + +{ Some output goes here. + +^ Code block + + ! This block cannot have child elements! + + The code block is used in an example to show example code. It is prefixed with a dollar sign ($), and all text following it will be show on the HTML page verbatim, without + any further markup processing being done. It even allows you to display ZippyDoc formatting characters without having them interpreted, as is done on this page! + + @ Using a code block + + $ ^ my_function(**argument**, **another_argument**) + + $$ Some kind of text describing the function goes here. + + $$ argument:: + This is the first argument to this example function. + + $$ another_argument:: + This is the second argument to this example function. + As you can see, it's possible to split the explanation over multiple lines as well. + + $$ @ Using this function + + $$ $ my_function(42, "The answer to everything") + + $$ > Some output goes here. + + It is also possible to have a code block spanning multiple paragraphs, without each paragraph being broken up into a separate code block (as would normally happen if you + just used the dollar sign). To do this, you can use two dollar signs at the start of the block. Note that after these two dollar signs, whitespace (except for spaces) is + **not** eaten, meaning you can use tabs to indent further blocks of your code! + + @ Using a multi-paragraph code block + + $ ^ my_function(**argument**, **another_argument**) + + $$ Some kind of text describing the function goes here. + + $$ argument:: + This is the first argument to this example function. + + $$ another_argument:: + This is the second argument to this example function. + As you can see, it's possible to split the explanation over multiple lines as well. + + $$ @ Using this function + + $$ $ if some_variable == True: + + $$ $$ my_function(42, "The answer to everything") + + $$ > Some output goes here. + +^ Output block + + ! This block cannot have child elements! + + The output block is used to display sample output in an example. Just like the code block, it is shown exactly as it originally was, without any further formatting applied. + It is prefixed by a >, and like the code block it has a continuation character - in this case, that is >>. + + @ Using an output block + + $ ^ my_function(**argument**, **another_argument**) + + $$ Some kind of text describing the function goes here. + + $$ argument:: + This is the first argument to this example function. + + $$ another_argument:: + This is the second argument to this example function. + As you can see, it's possible to split the explanation over multiple lines as well. + + $$ @ Using this function + + $$ $ my_function(42, "The answer to everything") + + $$ > The answer to everything is 42! + + @ Using a multi-paragraph output block + + $ ^ my_function(**argument**, **another_argument**) + + $$ Some kind of text describing the function goes here. + + $$ argument:: + This is the first argument to this example function. + + $$ another_argument:: + This is the second argument to this example function. + As you can see, it's possible to split the explanation over multiple lines as well. + + $$ @ Using this function + + $$ $ my_function(42, "The answer to everything") + + $$ > The answer to everything is 42! + + $$ >> Did you know The answer to everything is 42? + +^ Exclamation block + + The exclamation block allows you to mark a block of text as "important". In the standard HTML layout, it will have a yellow-ish background, and will be prefixed + with "__Important!__". It is prefixed with an exclamation mark (!). Inline markup is applied. + + @ Using an exclamation block + + $ ^ my_function(**argument**, **another_argument**) + + $$ Some kind of text describing the function goes here. + + $$ ! Only ever use this function with the number '42'! + + $$ argument:: + This is the first argument to this example function. + + $$ another_argument:: + This is the second argument to this example function. + As you can see, it's possible to split the explanation over multiple lines as well. + + $$ @ Using this function + + $$ $ my_function(42, "The answer to everything") + + $$ > The answer to everything is 42! + + +^ Header block + + ! This block cannot have child elements! + + A header block is a generic header to indicate the start of a new section. It is treated as a separate element, not as a "container". The header blocks in ZippyDoc + work similarly to those in Markdown: they are prefixed by a hash (#), and the amount of hash characters defines what level of header it is. + + @ Using header blocks + + $ # This is a level 1 (largest) header. + + $$ ## This is a level 2 header. + + $$ ... + + $$ ####### This is a level 7 (smallest) header. + + +^ Text block + + ! This block cannot have child elements! + + A text block is any block that is not prefixed by a special character. It is shown as defined, with inline markup applied. + + +## Inline markup + +There are also various forms of inline markup that you can use in your documentation. + +^ Emphasized text + + Emphasized text is typically displayed as italic. You can emphasize text by enclosing it in two asterisks on each side. + + @ Emphasizing text + + $ This is just some text, and **this part is emphasized**. + +^ Strong text + + Strong text is typically displayed as bold. You can make text strong by enclosing it in two underscores on each side. + + @ Making text strong + + $ This is just some text, __and this part is strong__. + +^ Internal references (hyperlinks) + + Internal references are hyperlinks that point to other documents in the same documentation set. Depending on the export format (currently only HTML is supported), + the appropriate extension is automatically appended. The paths should resemble the directory structure you are storing the ZippyDoc source files in. The target + of the reference is enclosed in curly braces and prefixed with a >. If you wish to give the reference a friendly description, you can do so by appending it, + enclosed in parentheses. + + @ Referencing another documentation page + + $ You can also view the API documentation at {>api/index}. + + @ Referencing another documentation page with a friendly description + + $ You can also view the {>api/index}(API documentation). + + +^ External references (hyperlinks) + + External references are hyperlinks just like the internal references, but they refer to an external resources. The syntax is identical to that of internal references, + except for the > disappearing. Note that external references are only picked up when the text enclosed in the braces is an actual URI of some sort. + + You can also force an external reference to be created by prefixing the URI with <. This is useful when you want to for example link to a download relative to the current + page. + + @ Referencing Google + + $ You could also search {http://www.google.com/}. + + @ Referencing Google with a friendly description + + $ You could also search {http://www.google.com/}(Google). + + @ Referencing a relative file that is not a ZippyDoc document + + $ You can download it by {example}(view a full example here) of a ZippyDoc source file and its result. diff --git a/site/example.html b/site/example.html new file mode 100644 index 0000000..66d696e --- /dev/null +++ b/site/example.html @@ -0,0 +1,187 @@ + + + + + + +

A complete example

Code:
^ my_function(**argument**, **another_argument**)
+
+	Some kind of text describing the function goes here. `Also some mono-spaced text.`
+
+	! Only ever use this function with the number '42'!
+
+	argument::
+		This is the first argument to this example function.
+
+	another_argument::
+		This is the second argument to this example function.
+		As you can see, it's possible to split the explanation over multiple lines as well.
+		We can also add an {>documentation}(internal link) and an {http://google.com/}(external link).
+
+	@ Using this function
+
+		$ if some_variable == True:
+
+		$$	my_function(42, "The answer to everything")
+
+		> The answer to everything is 42!
+
+		>>	Did you know The answer to everything is 42?

Result

+ + diff --git a/site/example.zpy b/site/example.zpy new file mode 100644 index 0000000..bd4110e --- /dev/null +++ b/site/example.zpy @@ -0,0 +1,51 @@ +# A complete example + +$ ^ my_function(**argument**, **another_argument**) + +$$ Some kind of text describing the function goes here. `Also some mono-spaced text.` + +$$ ! Only ever use this function with the number '42'! + +$$ argument:: + This is the first argument to this example function. + +$$ another_argument:: + This is the second argument to this example function. + As you can see, it's possible to split the explanation over multiple lines as well. + We can also add an {>documentation}(internal link) and an {http://google.com/}(external link). + +$$ @ Using this function + +$$ $ if some_variable == True: + +$$ $$ my_function(42, "The answer to everything") + +$$ > The answer to everything is 42! + +$$ >> Did you know The answer to everything is 42? + +## Result + +^ my_function(**argument**, **another_argument**) + + Some kind of text describing the function goes here. `Also some mono-spaced text.` + + ! Only ever use this function with the number '42'! + + argument:: + This is the first argument to this example function. + + another_argument:: + This is the second argument to this example function. + As you can see, it's possible to split the explanation over multiple lines as well. + We can also add an {>documentation}(internal link) and an {http://google.com/}(external link). + + @ Using this function + + $ if some_variable == True: + + $$ my_function(42, "The answer to everything") + + > The answer to everything is 42! + + >> Did you know The answer to everything is 42? diff --git a/site/index.html b/site/index.html new file mode 100644 index 0000000..c21e05a --- /dev/null +++ b/site/index.html @@ -0,0 +1,177 @@ + + + + + + +

ZippyDoc

Hi, this is the website of ZippyDoc, a compact, light-weight and human-readable format for documenting code, APIs, and other things, that can be easily converted to HTML.
It is designed primarily to be simple to use (unlike complex markup languages like reStructuredText), and very code-oriented (unlike other simple markup languages like Markdown). You will probably learn the entire syntax in about 10 minutes.
ZippyDoc (both the format and the parser) are licensed under the WTFPL, meaning you can basically do with it whatever you want, and reuse it in any fashion you see fit. I hope it will help you write nicer, easier, and more complete documentation!
While ZippyDoc is technically intended for documentation, I decided to whip up a simple index page in ZippyDoc as well - you're looking at it! :)

What does the ZippyDoc format look like?

Code:
^ my_function(argument1, argument2)
+
+	! This is just an example!
+
+	This is a function.
+
+	argument1::
+		This is the first argument.
+
+	argument2::
+		This is the second argument.
+
+	@ How to call my_function
+
+		$ my_function("ZippyDoc", "awesome")
+
+		> "ZippyDoc is awesome!"
Result:

Documentation

The documentation for ZippyDoc can be found here.

Downloading ZippyDoc

ZippyDoc is now a PyPi package! To install it, make sure you have pip installed, and run pip install zippydoc. You can then use the zpy2html command anywhere, to convert your ZippyDoc source files to HTML. The GitHub repository can be found here.
+ + diff --git a/site/index.zpy b/site/index.zpy new file mode 100644 index 0000000..dfe11c8 --- /dev/null +++ b/site/index.zpy @@ -0,0 +1,60 @@ +# ZippyDoc + +Hi, this is the website of ZippyDoc, a compact, light-weight and human-readable format for documenting code, APIs, and other things, that can be easily converted to HTML. + +It is designed primarily to be simple to use (unlike complex markup languages like reStructuredText), and very code-oriented (unlike other simple markup languages like Markdown). +You will probably learn the entire syntax in about 10 minutes. + +ZippyDoc (both the format and the parser) are licensed under the {http://www.wtfpl.net/}(WTFPL), meaning you can basically do with it whatever you want, and reuse it in any +fashion you see fit. I hope it will help you write nicer, easier, and more complete documentation! + +While ZippyDoc is technically intended for documentation, I decided to whip up a simple index page in ZippyDoc as well - you're looking at it! :) + +## What does the ZippyDoc format look like? + +$ ^ my_function(argument1, argument2) + +$$ ! This is just an example! + +$$ This is a function. + +$$ argument1:: + This is the first argument. + +$$ argument2:: + This is the second argument. + +$$ @ How to call my_function + +$$ $ my_function("ZippyDoc", "awesome") + +$$ > "ZippyDoc is awesome!" + +####### Result: + +^ my_function(argument1, argument2) + + ! This is just an example! + + This is a function. + + argument1:: + This is the first argument. + + argument2:: + This is the second argument. + + @ How to call my_function + + $ my_function("ZippyDoc", "awesome") + + > "ZippyDoc is awesome!" + +## Documentation + +The documentation for ZippyDoc can be found {>documentation}(here). + +## Downloading ZippyDoc + +ZippyDoc is now a PyPi package! To install it, make sure you have pip installed, and run `pip install zippydoc`. You can then use the `zpy2html` command +anywhere, to convert your ZippyDoc source files to HTML. The GitHub repository can be found {https://github.com/joepie91/ZippyDoc}(here). diff --git a/src/zippydoc/__init__.py b/src/zippydoc/__init__.py new file mode 100644 index 0000000..d328b10 --- /dev/null +++ b/src/zippydoc/__init__.py @@ -0,0 +1,4 @@ +from block_markup import * +from document import * +from transformation_ruleset import * +from value import * diff --git a/src/zippydoc/block_markup.py b/src/zippydoc/block_markup.py new file mode 100644 index 0000000..cec6fd0 --- /dev/null +++ b/src/zippydoc/block_markup.py @@ -0,0 +1,85 @@ +import re +from value import Value + +class TreeLevel(object): + def __init__(self, indentation, data): + self.elements = [] + self.indentation = indentation + self.data = data + + def add(self, element): + self.elements.append(element) + + def transform(self, ruleset): + return self.transform_children(ruleset) + + def transform_children(self, ruleset): + child_output = "" + + for child in self.elements: + child_output += child.transform(ruleset) + + return ruleset.transform_children(child_output) + +class Header(TreeLevel): + def __init__(self, indentation, data, depth): + self.elements = [] + self.indentation = indentation + self.data = data + self.depth = depth + + def transform(self, ruleset): + return ruleset.transform_header(self.depth, Value(self.data)) + +class Text(TreeLevel): + def transform(self, ruleset): + return ruleset.transform_text(Value(self.data)) + +class Exclamation(TreeLevel): + def transform(self, ruleset): + return ruleset.transform_exclamation(Value(self.data), self.transform_children(ruleset)) + +class Definition(TreeLevel): + def __init__(self, indentation, forms): + self.elements = [] + self.indentation = indentation + self.forms = [form.lstrip() for form in forms] + + def transform(self, ruleset): + return ruleset.transform_definition([Value(form) for form in self.forms], self.transform_children(ruleset)) + + def get_forms(self): + return [Value(form) for form in self.forms] + + def get_description(self): + for element in self.elements: + if element.__class__.__name__ == "Text": + return element.data + + return "" + +class Argument(TreeLevel): + def __init__(self, indentation, data, argname): + self.elements = [] + self.indentation = indentation + self.data = data + self.argname = argname + + def transform(self, ruleset): + return ruleset.transform_argument(Value(self.argname), Value(self.data), self.transform_children(ruleset)) + +class Example(TreeLevel): + def transform(self, ruleset): + return ruleset.transform_example(Value(self.data), self.transform_children(ruleset)) + +class Code(TreeLevel): + def transform(self, ruleset): + return ruleset.transform_code(self.data) + +class Output(TreeLevel): + def transform(self, ruleset): + return ruleset.transform_output(Value(self.data)) + +class Index(TreeLevel): + def transform(self, ruleset): + return ruleset.transform_toc([(definition, Value(definition.get_description())) for definition in self.data.get_definitions()]) diff --git a/src/zippydoc/data/template.html b/src/zippydoc/data/template.html new file mode 100644 index 0000000..d8ea410 --- /dev/null +++ b/src/zippydoc/data/template.html @@ -0,0 +1,161 @@ + + + + + + + {CONTENT} + + diff --git a/src/zippydoc/document.py b/src/zippydoc/document.py new file mode 100644 index 0000000..dc1d5be --- /dev/null +++ b/src/zippydoc/document.py @@ -0,0 +1,85 @@ +import re +import block_markup + +class Document(object): + def __init__(self, data): + self.data = data + self._parse() + + def _parse(self): + paragraphs = re.split("\s*\n\s*\n", self.data) + + self.paragraphs = paragraphs + self.definitions = [] + + current_level = 0 + current_paragraph = 0 + self.current_elements = {0: block_markup.TreeLevel(0, "root")} + + for paragraph in paragraphs: + if paragraph.strip() == "": + continue + + current_paragraph += 1 + indentation = len(paragraph) - len(paragraph.lstrip("\t")) + 1 + + if indentation > current_level + 1: + raise Exception("Invalid indentation found in paragraph %d" % current_paragraph) + + start = indentation - 1 + lines = [line[start:] for line in paragraph.splitlines()] + + if lines[0].startswith("#"): + # Header + depth = len(lines[0]) - len(lines[0].lstrip("#")) + lines[0] = lines[0].lstrip("# ") + element = block_markup.Header(indentation, " ".join(lines), depth) + elif lines[0].startswith("^"): + # Definition + lines[0] = lines[0].lstrip("^ ") + element = block_markup.Definition(indentation, lines) + self.definitions.append(element) + elif lines[0].startswith("@"): + # Example + lines[0] = lines[0].lstrip("@ ") + element = block_markup.Example(indentation, " ".join(lines)) + elif lines[0].startswith("$$") and self.current_elements[current_level].__class__.__name__ == "Code": + # Code continuation + self.current_elements[current_level].data += "\n\n" + "\n".join(lines).lstrip("$ ") + continue + elif lines[0].startswith("$"): + # Code block start + lines[0] = lines[0].lstrip("$ ") + element = block_markup.Code(indentation, "\n".join(lines)) + elif lines[0].startswith(">>") and self.current_elements[current_level].__class__.__name__ == "Output": + # Output continuation + self.current_elements[current_level].data += "\n\n" + "\n".join(lines).lstrip("> ") + continue + elif lines[0].startswith(">"): + # Output block start + lines[0] = lines[0].lstrip("> ") + element = block_markup.Output(indentation, "\n".join(lines)) + elif lines[0].startswith("!"): + # Exclamation + lines[0] = lines[0].lstrip("! ") + element = block_markup.Exclamation(indentation, " ".join(lines)) + elif re.match(".*::\s*$", lines[0]): + # Argument definition + argname = re.match("(.*)::\s*$", lines[0]).group(1) + element = block_markup.Argument(indentation, " ".join(line.lstrip() for line in lines[1:]), argname) + elif lines[0].strip() == "{TOC}": + # Table of contents + element = block_markup.Index(indentation, self) + else: + # Text + element = block_markup.Text(indentation, " ".join(lines)) + + self.current_elements[indentation - 1].add(element) + current_level = indentation + self.current_elements[current_level] = element + + def transform(self, ruleset): + return self.current_elements[0].transform(ruleset) + + def get_definitions(self): + return self.definitions diff --git a/src/zippydoc/transformation_ruleset.py b/src/zippydoc/transformation_ruleset.py new file mode 100644 index 0000000..13a5c2d --- /dev/null +++ b/src/zippydoc/transformation_ruleset.py @@ -0,0 +1,45 @@ +class TransformationRuleset(object): + def transform_children(self, text): + pass + + def transform_header(self, depth, text): + pass + + def transform_definition(self, forms, children): + pass + + def transform_argument(self, name, description, children): + pass + + def transform_example(self, title, children): + pass + + def transform_code(self, text): + pass + + def transform_output(self, text): + pass + + def transform_exclamation(self, text, children): + pass + + def transform_text(self, text): + pass + + def transform_reference(self, target, description): + pass + + def transform_external_reference(self, target, description): + pass + + def transform_fixed_width(self, text): + pass + + def transform_emphasis(self, text): + pass + + def transform_strong(self, text): + pass + + def transform_toc(self, items): + pass diff --git a/src/zippydoc/value.py b/src/zippydoc/value.py new file mode 100644 index 0000000..f63cd27 --- /dev/null +++ b/src/zippydoc/value.py @@ -0,0 +1,29 @@ +import re + +class Value(str): + def transform(self, ruleset): + text = self + text = re.sub("`([^`]+)`", lambda x: ruleset.transform_fixed_width(Value(x.group(1))), text) # Fixed-width + text = re.sub("\*\*([^*]+)\*\*", lambda x: ruleset.transform_emphasis(Value(x.group(1))), text) # Emphasized + text = re.sub("__([^_]+)__", lambda x: ruleset.transform_strong(Value(x.group(1))), text) # Strong + text = re.sub("{>([^}]+)}\(([^)]+)\)", lambda x: ruleset.transform_reference(Value(x.group(1)), Value(x.group(2))), text) # Hyperlink with text + text = re.sub("{>([^}]+)}", lambda x: ruleset.transform_reference(Value(x.group(1)), Value(x.group(1))), text) # Hyperlink + text = re.sub("{([^}]+:[^}]+)}\(([^)]+)\)", lambda x: ruleset.transform_external_reference(Value(x.group(1)), Value(x.group(2))), text) # External hyperlink with text + text = re.sub("{([^}]+:[^}]+)}", lambda x: ruleset.transform_external_reference(Value(x.group(1)), Value(x.group(1))), text) # External hyperlink + text = re.sub("{<([^}]+)}\(([^)]+)\)", lambda x: ruleset.transform_external_reference(Value(x.group(1)), Value(x.group(2))), text) # Forced external hyperlink with text + text = re.sub("{<([^}]+)}", lambda x: ruleset.transform_external_reference(Value(x.group(1)), Value(x.group(1))), text) # Forced external hyperlink + return text + + def clean(self): + text = self + text = re.sub("`([^`]+)`", '\\1', text) # Fixed-width + text = re.sub("\*\*([^*]+)\*\*", "\\1", text) # Emphasized + text = re.sub("__([^_]+)__", "\\1", text) # Strong + text = re.sub("{>([^}]+)}\(([^)]+)\)", '\\2', text) # Hyperlink with text + text = re.sub("{>([^}]+)}", '\\1', text) # Hyperlink + text = re.sub("{([^}]+:[^}]+)}\(([^)]+)\)", '\\2', text) # External hyperlink with text + text = re.sub("{([^}]+:[^}]+)}", '\\1', text) # External hyperlink + text = re.sub("{<([^}]+)}\(([^)]+)\)", '\\2', text) # Forced external hyperlink with text + text = re.sub("{<([^}]+)}", '\\1', text) # Forced external hyperlink + return text + diff --git a/zpy2html b/zpy2html new file mode 100755 index 0000000..13a4fc0 --- /dev/null +++ b/zpy2html @@ -0,0 +1,122 @@ +#!/usr/bin/env python + +import os, argparse, sys, re +import zippydoc + +import os + +_ROOT = os.path.abspath(os.path.dirname(zippydoc.__file__)) + +def get_data(path): + return os.path.join(_ROOT, 'data', path) + +parser = argparse.ArgumentParser(description='Converts ZippyDoc source files to HTML.') + +parser.add_argument('files', metavar='FILE', type=str, nargs='+', + help='files to convert to HTML') + +args = parser.parse_args() +options = vars(args) + +class HtmlRuleset(zippydoc.TransformationRuleset): + def create_anchor(self, title): + anchor = title.clean().replace("...", "").replace(".", "_") + anchor = re.sub("[^a-zA-Z0-9_]", "", anchor) + return anchor + + def escape_html(self, text): + return text.replace("<", "<").replace(">", ">") + + def transform_children(self, text): + return '
%s
' % text + + def transform_header(self, depth, text): + if depth <= 7: + title_type = "h%d" % depth + else: + title_type = "h7" + + return "<%s>%s" % (title_type, text.transform(self), title_type) + + def transform_definition(self, forms, children): + anchor = self.create_anchor(forms[0]) + formlist = "
".join([form.transform(self) for form in forms]) + return '' % (anchor, formlist, children) + + def transform_argument(self, name, description, children): + return "
%s
%s%s
" % (name, description.transform(self), children) + + def transform_example(self, title, children): + return '
Example: %s %s
' % (title.transform(self), children) + + def transform_code(self, text): + return 'Code:
%s
' % self.escape_html(text) + + def transform_output(self, text): + return 'Output:
%s
' % self.escape_html(text) + + def transform_exclamation(self, text, children): + return '
Important: %s %s
' % (text.transform(self), children) + + def transform_text(self, text): + return '
%s
' % text.transform(self) + + def transform_reference(self, target, description): + return '%s' % (target, description.transform(self)) + + def transform_external_reference(self, target, description): + return '%s' % (target, description.transform(self)) + + def transform_fixed_width(self, text): + return '%s' % text + + def transform_emphasis(self, text): + return "%s" % text.transform(self) + + def transform_strong(self, text): + return "%s" % text.transform(self) + + def transform_toc(self, items): + rendered = "" + + for item in items: + forms = item[0].get_forms() + anchor = self.create_anchor(forms[0]) + + if len(forms) > 1: + alternatives = '(also: %s)' % ", ".join(form.clean() for form in forms[1:]) + else: + alternatives = "" + + description = item[1] + + if len(description) > 80: + matches = re.match("^(.{0,80})\W", description) + description = matches.group(1) + "..." + + description = zippydoc.Value(description).transform(self) + + rendered += '
  • %s %s %s
  • ' % (anchor, forms[0].clean(), description, alternatives) + + return '

    Table of contents

      %s
    ' % rendered + +files = options["files"] + +template = open(get_data("template.html")).read() + +for zpy in files: + destination = os.path.splitext(zpy)[0] + ".html" + + f = open(zpy, "r") + data = f.read() + f.close() + + doc = zippydoc.Document(open(zpy, "r").read()) + + rendered = doc.transform(HtmlRuleset()) + + f = open(destination, "w") + f.write(template.replace("{CONTENT}", rendered)) + f.close() + + print "Rendered %s" % destination diff --git a/zpy2html.py b/zpy2html.py deleted file mode 100644 index d0a8097..0000000 --- a/zpy2html.py +++ /dev/null @@ -1,392 +0,0 @@ -import os, argparse, sys, re - -parser = argparse.ArgumentParser(description='Converts ZippyDoc source files to HTML.') - -parser.add_argument('files', metavar='FILE', type=str, nargs='+', - help='files to convert to HTML') - -args = parser.parse_args() -options = vars(args) - -files = options["files"] - -template = """ - - - - - - - %s - - -""" - -class TreeLevel: - def __init__(self, indentation, data): - self.elements = [] - self.indentation = indentation - self.data = data - - def add(self, element): - self.elements.append(element) - - def output(self): - return self.render() - - def render_children(self): - child_output = "" - - for child in self.elements: - child_output += child.output() - - return '
    %s
    ' % child_output - - def process_inline_markup(self, text): - text = re.sub("`([^`]+)`", '\\1', text) # Emphasized - text = re.sub("\*\*([^*]+)\*\*", "\\1", text) # Emphasized - text = re.sub("__([^_]+)__", "\\1", text) # Strong - text = re.sub("{>([^}]+)}\(([^)]+)\)", '\\2', text) # Hyperlink with text - text = re.sub("{>([^}]+)}", '\\1', text) # Hyperlink - text = re.sub("{([^}]+:[^}]+)}\(([^)]+)\)", '\\2', text) # External hyperlink with text - text = re.sub("{([^}]+:[^}]+)}", '\\1', text) # External hyperlink - text = re.sub("{<([^}]+)}\(([^)]+)\)", '\\2', text) # Forced external hyperlink with text - text = re.sub("{<([^}]+)}", '\\1', text) # Forced external hyperlink - - return text - - def fix_preformatted(self, text): - return text.replace("<", "<").replace(">", ">") - - def clear_markup(self, text): - return re.sub("\*\*([^*]+)\*\*", "\\1", text) - - def render(self): - return self.render_children() - -class Example(TreeLevel): - def render(self): - return '
    Example: %s %s
    ' % (self.data, self.render_children()) - -class Code(TreeLevel): - def render(self): - return 'Code:
    %s
    ' % self.fix_preformatted(self.data) - -class Output(TreeLevel): - def render(self): - return 'Output:
    %s
    ' % self.fix_preformatted(self.data) - -class Definition(TreeLevel): - def get_anchor(self): - first = self.clear_markup(self.data.splitlines()[0]) - anchor = first.replace("...", "") - anchor = anchor.replace(".", "_") - anchor = re.sub("[^a-zA-Z0-9_]", "", anchor) - return anchor - - def get_description(self): - for element in self.elements: - if element.__class__.__name__ == "Text": - data = self.process_inline_markup(element.data) - - if len(data) > 80: - matches = re.match("^(.{0,80})\W", data) - return matches.group(1) + "..." - else: - return data - - return "" - - def render(self): - return '' % (self.get_anchor(), self.process_inline_markup(self.data.replace("\n", "
    ")), self.render_children()) - -class Exclamation(TreeLevel): - def render(self): - return '
    Important: %s
    ' % self.process_inline_markup(self.data) - -class Argument(TreeLevel): - def __init__(self, indentation, data, argname): - self.elements = [] - self.indentation = indentation - self.argname = argname - self.data = data - - def render(self): - return '
    %s
    %s
    ' % (self.argname, self.process_inline_markup(self.data)) - -class Header(TreeLevel): - def __init__(self, indentation, data, depth): - self.elements = [] - self.indentation = indentation - self.depth = depth - self.data = data - - def render(self): - if self.depth <= 7: - title_type = "h%d" % self.depth - else: - title_type = "h7" - - return "<%s>%s" % (title_type, self.data, title_type) - -class Text(TreeLevel): - def render(self): - return '
    %s
    ' % self.process_inline_markup(self.data) - -class Index(TreeLevel): - def render(self): - global toc_items - - rendered = "" - - for item in toc_items: - forms = item.data.splitlines() - first = self.clear_markup(forms[0]) - - if len(forms) > 1: - rest = '(also: ' + ', '.join(self.clear_markup(form) for form in forms[1:]) + ")" - else: - rest = "" - - anchor = item.get_anchor() - description = item.get_description() - rendered += '
  • %s %s %s
  • ' % (anchor, first, description, rest) - - return '

    Table of contents

      %s
    ' % rendered - -for zpy in files: - destination = os.path.splitext(zpy)[0] + ".html" - - f = open(zpy, "r") - data = f.read() - f.close() - - paragraphs = re.split("\s*\n\s*\n", data) - toc_items = [] - current_level = 0 - current_paragraph = 0 - current_elements = {0: TreeLevel(0, "root")} - - for paragraph in paragraphs: - if paragraph.strip() == "": - continue - - current_paragraph += 1 - indentation = len(paragraph) - len(paragraph.lstrip("\t")) + 1 - - if indentation > current_level + 1: - raise Exception("Invalid indentation found in paragraph %d" % current_paragraph) - - element_type = TreeLevel - start = indentation - 1 - - lines = [line[start:] for line in paragraph.splitlines()] - - if lines[0].startswith("#"): - element_type = Header - depth = len(lines[0]) - len(lines[0].lstrip("#")) - lines[0] = lines[0].lstrip("# ") - data = " ".join(lines) - elif lines[0].startswith("^"): - element_type = Definition - lines[0] = lines[0].lstrip("^ ") - data = "\n".join(lines) - elif lines[0].startswith("@"): - element_type = Example - lines[0] = lines[0].lstrip("@ ") - data = " ".join(lines) - elif lines[0].startswith("$$") and current_elements[current_level].__class__.__name__ == "Code": - current_elements[current_level].data += "\n\n" + "\n".join(lines).lstrip("$ ") - continue - elif lines[0].startswith("$"): - element_type = Code - lines[0] = lines[0].lstrip("$ ") - data = "\n".join(lines) - elif lines[0].startswith(">>") and current_elements[current_level].__class__.__name__ == "Output": - current_elements[current_level].data += "\n\n" + "\n".join(lines).lstrip("> ") - continue - elif lines[0].startswith(">"): - element_type = Output - lines[0] = lines[0].lstrip("> ") - data = "\n".join(lines) - elif lines[0].startswith("!"): - element_type = Exclamation - lines[0] = lines[0].lstrip("! ") - data = " ".join(lines) - elif re.match(".*::\s*$", lines[0]): - element_type = Argument - argname = lines[0][:-2] - data = " ".join(line.lstrip() for line in lines[1:]) - elif lines[0].strip() == "{TOC}": - element_type = Index - data = "" - else: - element_type = Text - data = " ".join(lines) - - #print "Found element of type %s at indentation %d with data %s" % (element_type.__name__, indentation, data[:80]) - - if element_type.__name__ == "Header": - element = Header(indentation, data, depth) - elif element_type.__name__ == "Argument": - element = Argument(indentation, data, argname) - else: - element = element_type(indentation, data) - - if element_type.__name__ == "Definition": - toc_items.append(element) - - current_elements[indentation - 1].add(element) - - current_level = indentation - current_elements[current_level] = element - - rendered = template % (current_elements[0].output()) - - f = open(destination, "w") - f.write(rendered) - f.close() - - print "Rendered %s" % destination