Giter VIP home page Giter VIP logo

cmark-scala's Introduction

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._

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, 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.1.0-SNAPSHOT"

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

cmark-scala's People

Contributors

da-liii avatar tindzk avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.