Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/sparsetech/cmark-scala
- Owner: sparsetech
- Created: 2017-05-14T00:02:35.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-09-14T08:19:11.000Z (about 3 years ago)
- Last Synced: 2024-08-04T00:05:05.220Z (4 months ago)
- Topics: commonmark, scala, scala-native
- Language: Scala
- Size: 12.7 KB
- Stars: 15
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-scala-native - cmark - Bindings for the [cmark](https://github.com/commonmark/cmark) CommonMark parser library. (Bindings)
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*!
""".stripMarginprintln("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.1document 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-6Chapter
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