Fun with python string formatting

Axy axy at declassed.art
Sun Dec 18 14:08:36 EST 2022


Hi all,

what do you see looking at format string syntax 
https://docs.python.org/3/library/string.html#formatstrings ?

In particular, at something like this:

{h[1].red.jumbo-header:Hello, World!}

Yes, this is syntactically correct statement and if we tweak Formatter 
methods, we can generate such an output:

<h1 class="red jumbo-header">Hello, World!</h1>

Someone might need a booze to catch sight of arguments and css classes 
in the source statement, okay: let it be Aligote markup.

Although I haven't implemented HTML rendering yet (my actual needs were 
reST and Markdown), this does generate plain text and Markdown now.

However, I'm discouraged at the moment. Without nested markup the 
implementation is very small and nice. With more or less complex 
formatting, such as nested lists, there's a need to analyse upper level 
statements and to enforce some rules. Also, there's a pain with 
indentation, but that's mainly because I was too lazy and based my 
implementation on previous work instead of writing a better one from 
scratch.

There are some undefined points such as how to render paragraphs. Use 
strict {p:text} directive or just split literal text by doubly newlines.

Can't decide whether to cut down all the complexity and revert recursion 
level to 2 or proceed with rich markup.

Anyway, below is the rendered specification in plain text. Here's the 
source code 
https://github.com/declassed-art/clabate/blob/main/clabate/extras/aligote.py 
and the specification 
https://github.com/declassed-art/clabate/blob/main/clabate/examples/aligote_spec.py

If this looks funny and you have any crazy ideas what can be added to or 
changed in the specification, let me know.

Axy.


Aligote markup specification
============================

Headings
--------

{h1:heading}
============

{h2:heading}
------------

{h3:heading}
------------

{h4:heading}
------------

{h5:heading}
------------

{h6:heading}
------------

Styles
------

{b:bold text}

{i:italic text}

{b:bold and {i:italic} text}

{i:italic and {b:bold} text}

{u:underline text}

{s:strike-through text}

{sub:subscript text}

{sup:superscript text}

Links
-----

{link[optional text]:URL}

Examples:

{link:http://declassed.art}

{link[Declassed Art]:http://declassed.art}

Rendered as:

http://declassed.art

Declassed Art (http://declassed.art)


Lists
-----

Unordered lists
---------------

{ul:
     {li:item one, can be markup}
     {li:item two
         can be multi-line}
     {li:etc}
}

Rendered as:

* item one, can be {b:markup}
* item two
   can be multi-line
* etc

Ordered lists
-------------

{ol:
     {li:item one}
     {li:item two}
     {li:etc}
}

Rendered as:

1. item one
2. item two
3. etc

Nested lists
------------

{ul:
     {li:item one}
     {li:item two}
     {ol:
         {li:item one,
             multi-line}
         {li:item two}
     }
     {li:etc}
}

Rendered as:

* item one
* item two
     1. item one,
        multi-line
     2. item two
* etc

Optional arguments
------------------

XXX Markdown does not support arbitrary numbering, does it?

{ol:
     {li[3]:item 3}
     {li[5]:item 5
         {ol:
             {li:ordinal is rendered as 5.1}
             {li[3]:ordinal is rendered as 5.3}
             {li[5][5]:ordinal is rendered as 5.5}
             {li: ordinal is rendered as 5.6}
         }
     }
}

Rendered as:

3. item 3
5. item 5
    5.1. ordinal is rendered as 5.1
    5.3. ordinal is rendered as 5.3
    5.5. ordinal is rendered as 5.5
    5.6. ordinal is rendered as 5.6

Optional argument for unordered list is the bullet character.
Default is `*`:
{ul:
     {li:item 1}
     {li[+]:item 2}
     {li[-]:item 3}
}

Rendered as:

* item 1
+ item 2
- item 3

Quirks
------

{ol:
     {li:item one}

     Basically, lists may contain any literal text.
     In terms of python formatting this is not an error,
     but that's not good for rendering.

     {li:item two}
}

Rendered as:

1. item one

Basically, lists may contain any literal text.
In terms of python formatting this is not an error,
but that's not good for rendering.

2. item two

Syntax highlighting
-------------------

{python:
     print('Hello, world!')
}

Rendered as:


     print('Hello, world!')


More information about the Python-list mailing list