Search code examples
python-3.xpython-sphinxrestructuredtext

Sphinx field list - url line break formatting


I'm using sphinx to generate the documentation for a project.

I'm using field list, referencing a long url.

My reST script is below:

**Operação**
^^^^^^^^^^^^

:Fonte: https://google.com.br
:Github: https://github.com/123456789876543221/hello_world/987654321/random/path/lalalalalalallaa/12345678987654321.py
:Tópicos adicionais: lorem ipsum.
:Pós-processamento: \-

The generated html is:

enter image description here

How can I split the URL in two lines, in order not to strangle the left column with field categories?


Solution

  • Costumizing the CSS seems the right way to go. But if you want an "quick fix" using only reST directives the most pratical would seem:

    1. Truncating the URL. (Drawback of selectivly shortening the URL.)
    2. Using a costum List Table. (Drawback of changing the field list format.)

    enter image description here

    **Operacao**
    ^^^^^^^^^^^^
    
    :Fonte: `a short url`_
    :Github: alongurl_
    :Tópicos: `https://www.a_very_long_url/..truncated../file.html`_ lorem ipsum.
    
    
    .. _a short url: http://google.com
    .. _alongurl: http://google.com/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    .. _https://www.a_very_long_url/..truncated../file.html: https://github.com/123456789876543221/hello_world/987654321/random/path/lalalalalalallaa/12345678987654321.py
    
    
    .. list-table:: 
       :widths: 30
    
       * - :Fonte: alongurl_ 
       * - :Github: If we took the bones out, it wouldn't be crunchy, now would it?jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj 
       * - :Tópicos: https://github.com/123456789876543221/hello_world/987654321/random/path/lalalalalalallaa/12345678987654321.py 
    

    I thought about editing or substituting the Hyperlinks References using Substitution Definitions but apparently reST doesn't allow that.

    You could define the HTML in an external file and include it, or use Raw Data Pass-Through but, in this specific case those still seem inferior "workarounds" for what should essentially be achieved using CSS.

    Also, I thought of using Line Blocks (or similar) inside the List Table as a way to manually separate the URL into lines. But that seems like pushing the reST markup syntax beyond its supposed use...

    There might be a really smart way to do it, if you understand the Element Hierarchy well enough, but I'd keep things simple by making changes on the endproduct HTML through CSS instead of detail customizing the reST.