Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/paulschuberth/mdgen

A Kotlin DSL to generate basic Markdown files
https://github.com/paulschuberth/mdgen

dsl kotlin markdown

Last synced: 3 days ago
JSON representation

A Kotlin DSL to generate basic Markdown files

Awesome Lists containing this project

README

        

# About

This is a tiny Kotlin DSL for creating Markdown files.

## Features

The DSL currently supports

- Headings
- Lists
- Code Blocks
### Headings

Headings can be created like by using the `section()` method

```kotlin
val md = Md(FileOutputStream(File("README.md")))

md.start {
section {
+"This is a heading with one #"
}
}
```
Nesting `section()` methods automatically increases the number of # used for the heading
The following results in a # Heading and a ## Sub-Heading

```kotlin
val md = Md(FileOutputStream(File("README.md")))

md.start {
section {
+"Heading"
section {
+"Sub-Heading"
}
}
}
```
Headings will be put on the same level if their `section()` methods are called after one-another.

```kotlin
val md = Md(FileOutputStream(File("README.md")))

md.start {
section {
+"Heading"
}
section {
+"Another Heading"
}
}
```
### Lists

Lists can be created using the `list()` method. By default, ordered lists start at
index 1 and their elements are added with the unary plus operator.
Unordered list elements are added using the unaryMinus operator and use the asterisk (*).

```kotlin
val md = Md(FileOutputStream(File("README.md")))

md.start {
list {
+"First element"
+"Second element"
+"Third element"
}
list {
-"Foo"
-"Bar"
-"Baz"
}
}
```
Running the code above results in the following Markdown.

1. First element
2. Second element
3. Third element
- Foo
- Bar
- Baz
### Code Blocks

At the moment this DSL only supports code fenced code blocks using three
backticks. An optional parameter can be used to specify the language of the code block.

```kotlin
println("Hello World")
```
The listing above is created by this code.

```kotlin
code("kotlin") {
"""
println("Hello World")
""".trimIndent()
}
```
Other languages can be specified by passing them in the first parameter.

```rust
fn main() {
println!("Hello World!");
}
```
The listing above is created by this code.

```kotlin
code("rust") {
"""
fn main() {
println!("Hello World!");
}
""".trimIndent()
}
```
### Tables

Tables can be created using the `table()` methods, which takes a variable amount of Strings for the headers of the table and as last parameter a lambda describing the table contents. Alternatively the table headers can be provided as Pairs of String to `MdTableHeader.Alignment` to configure the alignment of the content of a column.

This README.md is generated by the DSL as well. Take a look at
[ReadmeTest.kt](https://github.com/paulschuberth/mdgen/blob/main/src/test/kotlin/de/pschuberth/mdgen/ReadmeTest.kt) for details.

| Hello | Beautiful | World |
|:------|:---------:|------:|
| Foo | Bar | Baz |

The table above is created by the following code.

```kotlin
val md = Md(FileOutputStream(File("README.md")))
md.start {
table("Hello" to LEFT, "Beautiful" to CENTER, "World" to RIGHT) {
row {
+"Foo" + "Bar" + "Baz"
}
}
}
```