64 lines
2.1 KiB
Markdown
64 lines
2.1 KiB
Markdown
+++
|
|
title = "AsciiDoc parser"
|
|
date = 2022-04-06
|
|
+++
|
|
|
|
It started with some lines of throwaway code, held together by staples and duct tape in my little [Shitty SSG](/why-i-built-my-own-shitty-static-site-generator/) project and it kept evolving. A long time ago I decided that I liked [AsciiDoc](AsciiDoc parser) better than [Markdown](AsciiDoc parser) when it comes to simple markup languages.
|
|
|
|
On the surface they are very similar, both are very easy to write and read and can be used in their "raw" form. You don't need to render a page before you can read it comfortably, unlike, for instance, [HTML](https://developer.mozilla.org/en-US/docs/Web/HTML).
|
|
|
|
The difference becomes clear when you write texts that longer than the typical short comment or snippet. AsciiDoc is much more complete. You can write a whole book in it. I never did that, but I did notice that I started using it everywhere I could. Unfortunately there wasn't a really good library for Go available.
|
|
|
|
So I started one myself. The AsciiDoc specification is big and I started implementation with the features I use myself. It's far from complete, but [here](https://forgejo.ewintr.nl/ewintr/adoc) is the code.
|
|
|
|
## Example
|
|
|
|
[Run the snippet below on the Go Playground](https://go.dev/play/p/hF2wn_GdkBK)
|
|
|
|
```go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"ewintr.nl/adoc"
|
|
)
|
|
|
|
func main() {
|
|
sourceDoc := `= This is the title
|
|
|
|
And this is the first paragraph. With some text. Lists are supported too:
|
|
|
|
* Item 1
|
|
* Item 2
|
|
* Item 3
|
|
|
|
And we also have things like *bold* and _italic_.`
|
|
|
|
par := adoc.NewParser(strings.NewReader(sourceDoc))
|
|
doc := par.Parse()
|
|
|
|
htmlDoc := adoc.NewHTMLFormatter().Format(doc)
|
|
fmt.Println(htmlDoc)
|
|
|
|
// output:
|
|
//
|
|
// <!DOCTYPE html>
|
|
// <html>
|
|
// <head>
|
|
// <title>This is the title</title>
|
|
// </head>
|
|
// <body>
|
|
// <p>And this is the first paragraph. With some text. Lists are supported too:</p>
|
|
// <ul>
|
|
// <li>Item 1</li>
|
|
// <li>Item 2</li>
|
|
// <li>Item 3</li>
|
|
// </ul>
|
|
// <p>And we also have things like <strong>bold</strong> and <em>italic</em>.</p>
|
|
// </html>
|
|
}
|
|
```
|
|
|