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
Create a directory for the tutorial and the file
guide.adocin 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. EOFThe file is valid AsciiDoc, but it mixes styles, such as Markdown-style
##headings and-bullets.See what adocfmt makes of it:
adocfmt guide.adocWithout 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.
Check the file:
adocfmt --check guide.adocThe
--checkflag 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.Write the result back to the file, then check it again:
adocfmt --write guide.adoc adocfmt --check guide.adocBoth 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.
Create the file
recipe.adoc:cat > recipe.adoc <<'EOF' = Matcha Whisk the powder with the water. ---- 2 g matcha 70 ml water EOFThe
----line opens a listing block, which shows code or other text exactly as written. No line closes this block.Format the file:
adocfmt --write recipe.adocadocfmt 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.Close the block by adding a
----line at the end of the file:echo '----' >> recipe.adocFormat the file again, then print it:
adocfmt --write recipe.adoc cat recipe.adocadocfmt now exits with
0and 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
Check formatting in CI, so that a build fails when a file is not formatted.
Adopt adocfmt in a repository with one formatting commit and a pre-commit hook.
Read the formatting rules to see what adocfmt changes and what it leaves alone.