Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/sparsetech/cmark-scala

Parse, manipulate and render CommonMark in Scala Native
https://github.com/sparsetech/cmark-scala

commonmark scala scala-native

Last synced: 25 days ago
JSON representation

Parse, manipulate and render CommonMark in Scala Native

Awesome Lists containing this project

README

        

#
cmark-scala provides [Scala Native](http://www.scala-native.org/) bindings for [cmark](https://github.com/commonmark/cmark). cmark allows to parse, manipulate and render CommonMark documents.

The bindings were directly derived from [cmark.h](https://github.com/commonmark/cmark/blob/master/src/cmark.h). Comments were retained and adapted if necessary. The naming of functions and their encapsulation follows Scala's conventions. Note that `*_new` functions were renamed to `create` as to prevent name collisions with the eponymous Scala keyword.

## Example
```scala
import cmark._
import scalanative.unsafe._
import scalanative.unsigned._

var level = -1
def onNode(eventType: EventType, node: Ptr[Node]): Unit = {
eventType match {
case EventType.Enter => level += 1
case EventType.Exit => level -= 1
}

val levelStr = " " * level
val startLine = Node.getStartLine(node)
val endLine = Node.getEndLine(node)

Node.getType(node) match {
case NodeType.Text =>
val text = fromCString(Node.getLiteral(node))
println(s"${levelStr}text node @ line $startLine-$endLine: $text")

case _ =>
val nodeTypeStr = fromCString(Node.getTypeString(node))
println(s"$levelStr$nodeTypeStr node @ $startLine-$endLine")
}
}

val test =
"""# Chapter
|## Section
|### Sub-section
|
|Hello World from *cmark-scala*!
""".stripMargin

println("cmark version: " + fromCString(cmark.versionString()))
println()

val docNode = Parser.parseDocument(
toCString(test), test.length.toULong, Options.SourcePosition)
val iter = Iter.create(docNode)
var evType = Iter.next(iter)
while (evType != EventType.Done) {
onNode(evType, Iter.getNode(iter))
evType = Iter.next(iter)
}
Iter.free(iter)

val html = fromCString(Render.html(docNode, Options.Default))
println()
println(html)

Node.free(docNode)
```

**Output:**

```
cmark version: 0.27.1

document node @ 1-6
heading node @ 1-1
text node @ line 0-0: Chapter
heading node @ 1-1
heading node @ 2-2
text node @ line 0-0: Section
heading node @ 2-2
heading node @ 3-3
text node @ line 0-0: Sub-section
heading node @ 3-3
paragraph node @ 5-5
text node @ line 0-0: Hello World from
emph node @ 0-0
text node @ line 0-0: cmark-scala
emph node @ 0-0
text node @ line 0-0: !
paragraph node @ 5-5
document node @ 1-6

Chapter


Section


Sub-section


Hello World from cmark-scala!


```

## Dependency
```scala
libraryDependencies += "tech.sparse" %% "cmark-scala" % "0.2.0-SNAPSHOT"
```

## Install Native Library
In order to use this library you need to install `cmark` which installs `libcmark`.

* macOS users can use the following command.

```
$ brew install cmark
```

* Linux/Ubuntu users can use the following commands.

```
$ sudo apt update
$ sudo apt install cmark
```

## License
cmark-scala is licensed under the terms of the Apache v2.0 license. Its function interfaces and comments were derived from `cmark.h`, which is licensed under BSD-2-Clause.

## Authors
* Tim Nieradzik