Now Reading
Utilizing AsciiDoc & Asciidoctor for running a blog · duncan­lock­.web

Utilizing AsciiDoc & Asciidoctor for running a blog · duncan­lock­.web

2023-10-20 04:03:29

Asciidoc
Determine 1. Asciidoc.

I’ve been utilizing reStructuredText for writing on this weblog, as a result of it has a lot of built-in options that markdown doesn’t.

Nevertheless, reStructuredText’s precise syntax is a bit…​ fiddly – notably its non-atx headings, too many issues counting on lining up white house, and so on…​ If I don’t use it for a bit, I’ve to lookup or copy & paste all of the superior syntax.

I’d want to make use of AsciiDoc, because it has all the additional options, and in case you use Asciidoctor, all the simple stuff is the same as markdown – which isn’t (presently) customary AsciiDoc, however is a pleasant simplification.

The subset of options from reStructuredText (or Asciidoc) that Markdown doesn’t have – and that I’m truly utilizing on this weblog, are:

  • Determine/Photographs with captions
  • Admonitions
  • Entrance-matter/Metadata
  • Footnotes

Worse is higher?

Markdown has tons of issues. Most of those issues stem from two issues: Until CommonMark it was lazily, pointlessly, stubbornly non-standard – and it has only a few structural or semantic options. Due to folks wanting so as to add a few of these options – and since there was no customary and due to this fact no option to prolong one, Markdown has exploded into many fragmented semi-compatible dialects.

Nevertheless, worse is better. Markdown’s simplicity makes it fairly easy to implement, to the extent that there are native implementations of Markdown in in all probability each language: AWK, Bash, C …​ to Zig. There are hundreds of more-or-less full markdown implementations to select from, to suit any mission; listed below are fifty high quality maintained ones, that support CommonMark, sorted by language, for instance.

AsciiDoc is a lot higher than Markdown, however these additional options for the author include additional complexity for the individual implementing the instruments. Most likely as a consequence, the AsciiDoc ecosystem and tooling could be very anemic and leaves quite a bit to be desired.

The solely full and nicely maintained AsciiDoc processor is Asciidoctor – which is written in Ruby. That’s it – there aren’t any different choices as of early 2021. I don’t have something in opposition to Ruby, notably, however I don’t understand it or use it for something, so it’s not acquainted. It additionally has all the identical nicely know issues as Python with packaging/working tasks & managing dependencies. As a substitute of pip/setuptools/virtualenv, and so on…​ its gem/rbvenv/rake/bundler/rvm, and so on…​ tons of of world gem information, the entire big mess. I get the impression that the Ruby model of this mess is…​ much less of an issue someway than the python one, nevertheless it’s nonetheless a mess.

So, the AsciiDoc story is clearly way more restricted than the markdown story. In order for you native markdown help, you bought it, it doesn’t matter what you’re doing. In order for you native AsciiDoc help, you possibly can solely have it in case your mission is in Ruby (or Java & JavaScript with some caveats ). Apart from that, it’s important to shell out and run a Ruby course of – and have all of the required Ruby dependencies put in.

This turns your doc processing right into a slower-than-it-could-be, annoying to setup and preserve, exterior black field. Don’t take this the fallacious means – the contents of that black field are unbelievable and good folks have labored exhausting on them, however there are just a few of these folks and pitting them in opposition to the markdown group – which is way, a lot bigger – isn’t actually honest.

What are the implications of this for running a blog?

Enhancing expertise isn’t as good

Most fashionable editors help AsciiDoc syntax highlighting and generally preview, through plugins. However, as anticipated, these aren’t as well-developed, quite a few or as totally featured because the Markdown equivalents. If you happen to’re utilizing the Asciidoctor markdown-a-like syntax for the straightforward stuff, then you possibly can simply inform the editor it’s Markdown, and use the extra totally featured markdown help.

Not a lot native help

If you happen to’re utilizing a Ruby weblog engine, it would possibly have native AsciiDoc support, or your weblog engine might need a plugin that helps it. Don’t count on each weblog engine to help it, although. Some weblog engines have an escape hatch that allow you to use Pandoc or another customized command to course of your content material – and whereas Pandoc does have AsciiDoc help, it’s neither full nor flawless.

Does native help matter?

Does the truth that the doc processing is shelled out to a black field truly have an effect on utilizing AsciiDoc for running a blog? Properly, it makes publishing a bit slower to quite a bit slower, relying on the dimensions of your web site. However in most weblog/publishing methods you’ve a template that defines the construction of the web page with some form of placeholder that claims {{content-goes-here}}. It doesn’t actually matter how the content material is generated, so long as it’s – and so long as the HTML that’s produced strains up along with your CSS & is cheap. Talking of which…​

Asciidoctor’s built-in HTML output might be higher

When asciidoctor processes your .adoc file you possibly can inform it what converter (or “back-end”) you need to use – i.e. what sort of output you need. These are the built-in options. That is what they are saying in regards to the default HTML converter:

That is technically true, nonetheless, whereas the HTML it produces is technically HTML5, it’s additionally <div> soup. For instance, this doc processor for processing paperwork doesn’t output paragraph tags (<p>…​</p>) – it outputs this as a substitute: <div class="paragraph"><p>…​</p></div> – and it does one thing like that for mainly every thing. To be honest, they are aware of this & working on it.

So, I’m not going to make use of that, I’ll simply use some group written back-end/converter that’s higher…​ oh…​ yeah. Properly, fortunately, on this case there truly is one: html5s – which does a a lot higher job.

AsciiDoc Tough Edges

I’ve written a number of articles from scratch and converted the existing 80 reStructuredText articles to AsciiDoc, and it’s been pretty painless, however I have come throughout a number of tough edges and issues with AsciiDoc.

AsciiDoc has built-in support for footnotes, however there are some tough edges:

The mix of those points signifies that if you need externalized footnotes that work like the remainder of your content material, it’s important to give the footnote an ID and wrap the footnote definition in an inline pass-through. As a result of these grow to be doc attributes, it’s important to outline them earlier than you utilize them, so it’s best to in all probability put these on the high.

That is extra advanced & convoluted than it must be – footnotes ought to simply work. Anyway, it appears to be like like this:

:fn-disclaimer: move:q[footnote:disclaimer[Opinions are *my own*.]]

A daring assertion!{fn-disclaimer}

One other daring assertion!{fn-disclaimer}

They’ve a proposal for an improved footnote syntax – though it doesn’t speak about textual content formatting contained in the footnote.

Blockquotes

I took me ages poking round on GitHub earlier than I discovered the best way to set the hyperlink textual content within the quotation for a quoted block. That is the fundamental syntax:

[quote, attribution, citation title and information]
Quote or excerpt textual content

You’ll be able to put a URL in there, and it really works, however giving the URL a title doesn’t appear to work. So this works:

[quote, https://en.wikipedia.org/wiki/Main_Page]
Quote or excerpt textual content

however this doesn’t:

[quote, https://en.wikipedia.org/wiki/Main_Page[Wikipedia]]
Quote or excerpt textual content

Nevertheless, utilizing the Quoted paragraph syntax works:

"Quote or excerpt textual content"
-- https://en.wikipedia.org/wiki/Main_Page[Wikipedia]

Apparently, the correct way to do this with quoted blocks, is to “use single quotes across the attribute worth, that offers Asciidoctor the trace to use regular substitutions (identical to paragraph textual content)”. Unsure what which means at this level, however the docs on subtitutions are here. That is what it appears to be like like on this case:

[quote, 'https://en.wikipedia.org/wiki/Main_Page[Wikipedia]']
Quote or excerpt textual content

Utilizing AsciiDoc with Pelican

I’m presently utilizing Pelican for this weblog and scripting this put up in AsciiDoc. That is what you’ll want to do to get that working.

First install the Ruby dependencies & Asciidoctor itself. Not like me, it’s best to take heed to them and use RVM for this. After getting that put in, you’ll want to install html5s and its dependencies. Subsequent, you’ll want to add the asciidoc_reader Pelican Plugin and add it to your pelicanconf.py

PLUGINS = [
    'asciidoc_reader',
]

You must then set the Asciidoctor command line choices. These will configure it to make use of the html5s backend and rouge for source code syntax highlighting:

ASCIIDOC_OPTIONS = [
    '-a source-highlighter=rouge',
    '-a rouge-style=monokai',
    '-r asciidoctor-html5s',
    '-b html5s'
]

Rouge is appropriate with pygments – which I used to be utilizing beforehand and my theme is ready as much as count on, so this was a drop-in alternative – which could be very handy.

Including & eradicating plugins

The AsciiDoctor + htmls output has higher determine output than reStructuredText + my Higher Figures & Photographs Plugin, so I don’t want that anymore – supplied that I convert all articles utilizing figures to AsciiDoc. Then again, the extract_toc plugin doesn’t work for AsciiDoc + htmls output, so I copied it to an area plugin and modified it to work:

# -*- coding: utf-8 -*-
"""
Extract Desk of Contents from AsciiDoc output from the htmls backend
========================

A Pelican plugin to extract desk of contents (ToC) from `article.content material` and
place it in its personal `article.toc` variable to be used in templates.
"""

from os import path
from bs4 import BeautifulSoup
from pelican import indicators, readers, contents
import logging

logger = logging.getLogger(__name__)


def extract_asciidoc_toc(content material):
    if isinstance(content material, contents.Static):
        return

    soup = BeautifulSoup(content material._content, "html.parser")
    toc = None

    toc = soup.discover("nav", id="toc")

    if toc:
        toc.extract()
        content material._content = soup.decode()

        # Take away: <h2 id="toc-title">Desk of Contents</h2>
        toc.h2.decompose()

        # Change all ordered lists to unordered
        for l in toc("ol"):
            l.identify = "ul"

        content material.toc = toc.decode()

        logger.debug(f"ExtractAsciidocToc: content material.toc: {content material.toc}")


def register():
    indicators.content_object_init.join(extract_asciidoc_toc)

Changing your present content material to AsciiDoc

This is determined by the format of your present content material:

Changing reStructuredText to AsciiDoc

I attempted tons of various means of changing reStructuredText to AsciiDoc – and none of them are good.

Pandoc does a cheap job, except you utilize figures, which get fairly mangled. They’re aware/working on this. If you wish to use pandoc, That is the fundamental command:

See Also

$ pandoc --wrap=protect -f rst -t asciidoctor "supply.rst" > "dest.adoc"

In addition to figures, this additionally messes up metadata and pelican {static} hyperlinks, so that you in all probability need some pre- & post-processing to repair that. I wrote a bit of shell script to repair every thing besides the figures:

#
# Pre-process
#
cat "$src_path" | 
# Take away :alt: tags from figures & photographs, in any other case they get misplaced
sed -r 's/:alt: /n/g' | 
# Tabs to areas
sed -r 's/t/  /g' | 
#
# Convert rst to asciidoc utilizing pandoc
#
pandoc --wrap=protect --from rst --to asciidoctor | 
#
# Publish-process
#
# Repair metadata syntax, from date:: to :date:
sed -r 'N; s/^(.*)::n /:1:/g; P; D' | 
# Take away additional breaks created from determine caption conversion
sed -r 'N; s/____n//g; P; D' | 
# Repair Pelican {static} hyperlinks
sed -e 's/%7B/{/g' -e 's/%7D/}/g' 
> "$src_folder/$src_name".adoc

Nice, I’ll write my very own converter…​

I take advantage of figures fairly a bit, so this wasn’t a really passable answer. Eager about it, reStructuredText is mainly the Python documentation format, so I seemed for docutils based mostly instruments to transform reStructuredText to different issues. I ultimately discovered sphinx_asciidoc, which kind of labored – and was a reasonably easy python script that I may enhance. I forked it here and glued all the problems I discovered – fixing metadata, figures, tables, linked images and various other things.

I developed & examined this by changing all 80 odd rst articles on this web site to AsciiDoc and fixing all the problems that I fond within the converter.

Till pandoc fixes their figures, so far as I do know, that is in all probability the perfect, highest constancy option to convert reStructuredText to AsciiDoc. If you wish to use this, do one thing like this to get it setup:

$ git clone https://github.com/dflock/sphinx_asciidoc.git
$ cd sphinx_asciidoc
$ python3 -m venv ~/venv/sphinx_asciidoc
$ supply ~/venv/sphinx_asciidoc/bin/activate
$ python3 -m pip set up -r necessities.txt

then this to run it:

$ python3 ./sphinx_asciidoc/author.py supply.rst

It will create a supply.rst.adoc file in the identical folder. I attempted to maintain this as basic objective as doable, however there are in all probability some issues in right here that are particular to my paperwork. There’s a part on the high of author.py with some knobs to twiddle:

#
# Issues that ought to be choices, however aren't
#
# Output the rendered TOC from docutils, or simply `:toc:`
self.outputTOC = False
# Desk column alignment, if not specified. May be <>^ or
# '' for unspecified.
self.defaultTableColAlign = ""
# Specify percentages for columns widths, or depart browser to auto-layout?
self.defaultTableColWidths = True
# Do you need to output the [1] ref's after the {footnote}, or let asciidoctor do it?
self.outputFootnoteRef = False

Changing Markdown to AsciiDoc

In case your content material is in Markdown, you want Kramdown. Kramdown is an excellent markdown to AsciiDoc converter, that works nice and produces flawless AsciiDoc – unsurprising, provided that it’s written by Dan Allen, the identical man who largely runs the Asciidoctor mission. After getting Kramdown put in, you possibly can simply do: $ kramdoc supply.md and it’ll create a supply.adoc file in the identical folder.

Way forward for AsciiDoc & Asciidoctor

There are a number of promising tasks that can assist enhance the AsciiDoc ecosystem.

The AsciiDoc Specification

The primary and largest one is that AsciiDoc is finally getting a proper spec, beneath the umbrella of the Eclipse Basis. That is one thing that Markdown by no means had till CommonMark – and that AsciiDoc has lacked so far. What this implies is:

The specification for the AsciiDoc language will embody an open supply specification doc, which defines required and non-compulsory API definitions, semantic behaviours, knowledge codecs, and protocols, in addition to an open supply Know-how Compatibility Package (TCK) that builders can use to develop and take a look at appropriate implementations. …​ A appropriate implementation, as outlined by the EFSP, should totally implement all non-optional parts of a specification model, should fulfill all the necessities of the corresponding TCK, and should not alter the required API.

For customers and builders alike, the AsciiDoc specification will imply a transparent, working definition of what AsciiDoc is and the way it ought to be interpreted. Builders will be capable to construct implementations, instruments, and companies round AsciiDoc with out threat of diluting its which means or splintering it. In flip, customers can have extra choices, better doc portability, and the reassurance that appropriate implementations and instruments will deal with their AsciiDoc paperwork based on a versioned specification.

Right here is the AsciiDoc Language project proposal and the approved scope of the project.

So, this could assist forestall the fragmentation that plagues the Markdown ecosystem, as nicely a making it simpler for folks to develop AsciiDoc parsers & instruments. Nonetheless nowhere close to as straightforward as implementing a Markdown one, although – AsciiDoc is simply extra advanced.

Having mentioned that, this can be a massive mission and many of the exercise is happening on mailing lists – there additionally now a website for the Working Group which presently contains assembly minutes and so on…​ There’s now an AsciiDoc language repo for discussing the spec work, nevertheless it’s nonetheless early days: https://gitlab.eclipse.org/eclipse/asciidoc/asciidoc-lang/

libasciidoc

Libsciidoc is a Golang library for processing AsciiDoc files. This makes use of a PEG parser with a proper grammar for AsciiDoc. It already helps a helpful subset of AsciiDoc and is being slowly labored on by a number of folks, I believe with the intention to make use of it with Hugo, which is able to make a pleasant mixture, when it’s carried out.

Like most software program written in Go, it’s statically linked, which implies no dependencies in any respect – you simply must put the libacsiidoc binary someplace and run it. That is very nice in comparison with establishing and sustaining the Ruby dependencies required for Asciidoctor, or the JS & Java ones for Asciidoctor-J/Java, for instance.




Source Link

What's Your Reaction?
Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top