Get started with adocfmt

In this tutorial, you format a small AsciiDoc file and fix a document that adocfmt refuses to format.

Before you begin

Install adocfmt and open a shell, such as bash or zsh.

Format a document

  1. Create a directory for the tutorial and the file guide.adoc in it:

    mkdir adocfmt-tutorial
    cd adocfmt-tutorial
    cat > guide.adoc <<'EOF'
    = Tea guide
    
    ##  Brewing
    Green tea turns bitter in boiling water. Heat the water
    to 80 degrees.
    
    - Use one teaspoon per cup.
    
    - Steep for two minutes.
    
    
    
    ## Serving ##
    1. Pour the tea.
    2. Serve it hot.
    EOF

    The file is valid AsciiDoc, but it mixes styles, such as Markdown-style ## headings and - bullets.

  2. See what adocfmt makes of it:

    adocfmt guide.adoc

    Without a flag, adocfmt prints the result and leaves the file as it is:

    = Tea guide
    
    == Brewing
    
    Green tea turns bitter in boiling water.
    Heat the water to 80 degrees.
    
    * Use one teaspoon per cup.
    * Steep for two minutes.
    
    == Serving
    
    . Pour the tea.
    . Serve it hot.

    The output follows the rules for heading markers, blank lines, and list markers, with one sentence per line. Asciidoctor still renders the same page, one of the guarantees that adocfmt gives.

  3. Check the file:

    adocfmt --check guide.adoc

    The --check flag changes no file. It names each file that is not formatted:

    guide.adoc

    It exits with 1, so a CI job can fail the build.

  4. Write the result back to the file, then check it again:

    adocfmt --write guide.adoc
    adocfmt --check guide.adoc

    Both print nothing, and the check now exits with 0.

Fix a refused document

adocfmt refuses a document when it finds structure that it cannot read reliably. It then leaves the file unchanged and reports each problem it found, called a finding.

  1. Create the file recipe.adoc:

    cat > recipe.adoc <<'EOF'
    = Matcha
    
    Whisk the powder with the water.
    ----
    2 g matcha
    70 ml water
    EOF

    The ---- line opens a listing block, which shows code or other text exactly as written. No line closes this block.

  2. Format the file:

    adocfmt --write recipe.adoc

    adocfmt reports the finding:

    recipe.adoc:4: block has no closing delimiter

    The message names the file, the line to look at, and the problem. adocfmt exits with 2, which means an error. For what each message means and how to fix it, see Refusal findings.

  3. Close the block by adding a ---- line at the end of the file:

    echo '----' >> recipe.adoc
  4. Format the file again, then print it:

    adocfmt --write recipe.adoc
    cat recipe.adoc

    adocfmt now exits with 0 and adds a blank line above the listing block:

    = Matcha
    
    Whisk the powder with the water.
    
    ----
    2 g matcha
    70 ml water
    ----

To learn why adocfmt refuses the whole document instead of formatting the parts it can read, see Why a document with findings is refused whole.

Next steps

View the source on GitHub