GitHub - sparsetech/cmark-scala: Parse, manipulate and render CommonMark in Scala Native (original) (raw)

cmark-scala provides Scala Native bindings for cmark. cmark allows to parse, manipulate and render CommonMark documents.

The bindings were directly derived from 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

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−startLine-startLineendLine: $text")

case _ =>
  val nodeTypeStr = fromCString(Node.getTypeString(node))
  println(s"$levelStr$nodeTypeStr node @ <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>a</mi><mi>r</mi><mi>t</mi><mi>L</mi><mi>i</mi><mi>n</mi><mi>e</mi><mo>−</mo></mrow><annotation encoding="application/x-tex">startLine-</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7667em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">t</span><span class="mord mathnormal">L</span><span class="mord mathnormal">in</span><span class="mord mathnormal">e</span><span class="mord">−</span></span></span></span>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

<h1>Chapter</h1>
<h2>Section</h2>
<h3>Sub-section</h3>
<p>Hello World from <em>cmark-scala</em>!</p>

Dependency

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.

$ 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