Skip to content

Commit

Permalink
Added a module/document project
Browse files Browse the repository at this point in the history
  • Loading branch information
labra committed Jun 4, 2021
1 parent 2dd5905 commit 45bfbd5
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 252 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/mdoc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Website
on:
push:
branches: [main]
tags: ["*"]
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: olafurpg/setup-scala@v10
with:
java-version: [email protected]
- uses: olafurpg/setup-gpg@v3
- run: sbt docs/docusaurusPublishGhpages
env:
GIT_DEPLOY_KEY: ${{ secrets.GIT_DEPLOY_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
10 changes: 5 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ def priorTo2_13(scalaVersion: String): Boolean =
case _ => false
}

lazy val root = project
.in(file("."))
.aggregate(document,docs)

lazy val document = project
.in(file("."))
.enablePlugins(
)
.in(file("modules/document"))
.settings(
commonSettings,
ThisBuild / turbo := true,
Expand All @@ -73,8 +75,6 @@ lazy val document = project
coverageHighlighting := true,
testFrameworks += new TestFramework("munit.Framework"),
)
// .aggregate(docs)


/* Docs generation */

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
= Document library

This project contains code that was developed as part of the Scala standard library but was later deprecated.

A basic pretty-printing library, based on
https://lindig.github.io/papers/strictly-pretty-2000.pdf[Lindig's strict version] of
https://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf[Wadler's adaptation] of
http://www.cse.chalmers.se/~rjmh/Papers/pretty.html[Hughes' pretty-printer] derived from
https://github.com/scala/scala/blob/v2.11.8/src/library/scala/text/Document.scala[Document.scala] whose original author was
https://people.epfl.ch/michel.schinz[Michel Schinz]

We maintain it because we had some code using it and because we want an experimental simple Scala code library to test documentation.

== ScalaDoc

link:latest/api/es/weso/document/Document.html[ScalaDoc]
= Document library

This project contains code that was developed as part of the Scala standard library but was later deprecated.

A basic pretty-printing library, based on
https://lindig.github.io/papers/strictly-pretty-2000.pdf[Lindig's strict version] of
https://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf[Wadler's adaptation] of
http://www.cse.chalmers.se/~rjmh/Papers/pretty.html[Hughes' pretty-printer] derived from
https://github.com/scala/scala/blob/v2.11.8/src/library/scala/text/Document.scala[Document.scala] whose original author was
https://people.epfl.ch/michel.schinz[Michel Schinz]

We maintain it because we had some code using it and because we want an experimental simple Scala code library to test documentation.

== ScalaDoc

link:latest/api/es/weso/document/Document.html[ScalaDoc]
Original file line number Diff line number Diff line change
@@ -1,136 +1,136 @@
package es.weso.document

import java.io.Writer

case object DocNil extends Document
case object DocBreak extends Document
case class DocText(txt: String) extends Document
case class DocGroup(doc: Document) extends Document
case class DocNest(indent: Int, doc: Document) extends Document
case class DocCons(hd: Document, tl: Document) extends Document

/**
* A basic pretty-printing library, based on Lindig's strict version
* of Wadler's adaptation of Hughes' pretty-printer.
*
* Derived from [[https://github.com/scala/scala/blob/v2.11.8/src/library/scala/text/Document.scala]]
* @author Michel Schinz
*/
sealed abstract class Document extends Product with Serializable {

/*Join two documents.*/
def ::(hd: Document): Document = DocCons(hd, this)

/*Join a document and a String.*/
def ::(hd: String): Document = DocCons(DocText(hd), this)

/*Join two documents with a line break.*/
def :/:(hd: Document): Document = hd :: DocBreak :: this

/*Join a document and a String with a line break.*/
def :/:(hd: String): Document = hd :: DocBreak :: this

/**
* Format this document on `writer` and try to set line
* breaks so that the result fits in `width` columns.
*/
def format(width: Int, writer: Writer): Unit = {

type FmtState = (Int, Boolean, Document)

def fits(width: Int, state: List[FmtState]): Boolean = state match {

case _ if width < 0 =>
false

case List() =>
true

case (_, _, DocNil) :: z =>
fits(width, z)

case (i, b, DocCons(h, t)) :: z =>
fits(width, (i,b,h) :: (i,b,t) :: z)

case (_, _, DocText(t)) :: z =>
fits(width - t.length(), z)

case (i, b, DocNest(ii, d)) :: z =>
fits(width, (i + ii, b, d) :: z)

case (_, false, DocBreak) :: z =>
fits(width - 1, z)

case (_, true, DocBreak) :: z =>
true

case (i, _, DocGroup(d)) :: z =>
fits(width, (i, false, d) :: z)
}

def spaces(n: Int): Unit = {
var rem = n
while (rem >= 16) { writer write " "; rem -= 16 }
if (rem >= 8) { writer write " "; rem -= 8 }
if (rem >= 4) { writer write " "; rem -= 4 }
if (rem >= 2) { writer write " "; rem -= 2}
if (rem == 1) { writer write " " }
}

def fmt(k: Int, state: List[FmtState]): Unit = state match {

case List() => ()

case (_, _, DocNil) :: z =>
fmt(k, z)

case (i, b, DocCons(h, t)) :: z =>
fmt(k, (i, b, h) :: (i, b, t) :: z)

case (i, _, DocText(t)) :: z =>
writer write t
fmt(k + t.length(), z)

case (i, b, DocNest(ii, d)) :: z =>
fmt(k, (i + ii, b, d) :: z)

case (i, true, DocBreak) :: z =>
writer write "\n"
spaces(i)
fmt(i, z)

case (i, false, DocBreak) :: z =>
writer write " "
fmt(k + 1, z)

case (i, b, DocGroup(d)) :: z =>
val fitsFlat = fits(width - k, (i, false, d) :: z)
fmt(k, (i, !fitsFlat, d) :: z)

// case null => ()
}

fmt(0, (0, false, DocGroup(this)) :: Nil)
}
}

object Document {

/** The empty document */
def empty: Document = DocNil

/** A break, which will either be turned into a space or a line break */
def break: Document = DocBreak

/** A document consisting of some text literal */
def text(s: String): Document = DocText(s)

/**
* A group of documents, whose components will either be printed with all breaks
* rendered as spaces, or with all breaks rendered as line breaks.
*/
def group(d: Document): Document = DocGroup(d)

/** A nested document, which will be indented as specified. */
def nest(i: Int, d: Document): Document = DocNest(i, d)
package es.weso.document

import java.io.Writer

case object DocNil extends Document
case object DocBreak extends Document
case class DocText(txt: String) extends Document
case class DocGroup(doc: Document) extends Document
case class DocNest(indent: Int, doc: Document) extends Document
case class DocCons(hd: Document, tl: Document) extends Document

/**
* A basic pretty-printing library, based on Lindig's strict version
* of Wadler's adaptation of Hughes' pretty-printer.
*
* Derived from [[https://github.com/scala/scala/blob/v2.11.8/src/library/scala/text/Document.scala]]
* @author Michel Schinz
*/
sealed abstract class Document extends Product with Serializable {

/*Join two documents.*/
def ::(hd: Document): Document = DocCons(hd, this)

/*Join a document and a String.*/
def ::(hd: String): Document = DocCons(DocText(hd), this)

/*Join two documents with a line break.*/
def :/:(hd: Document): Document = hd :: DocBreak :: this

/*Join a document and a String with a line break.*/
def :/:(hd: String): Document = hd :: DocBreak :: this

/**
* Format this document on `writer` and try to set line
* breaks so that the result fits in `width` columns.
*/
def format(width: Int, writer: Writer): Unit = {

type FmtState = (Int, Boolean, Document)

def fits(width: Int, state: List[FmtState]): Boolean = state match {

case _ if width < 0 =>
false

case List() =>
true

case (_, _, DocNil) :: z =>
fits(width, z)

case (i, b, DocCons(h, t)) :: z =>
fits(width, (i,b,h) :: (i,b,t) :: z)

case (_, _, DocText(t)) :: z =>
fits(width - t.length(), z)

case (i, b, DocNest(ii, d)) :: z =>
fits(width, (i + ii, b, d) :: z)

case (_, false, DocBreak) :: z =>
fits(width - 1, z)

case (_, true, DocBreak) :: z =>
true

case (i, _, DocGroup(d)) :: z =>
fits(width, (i, false, d) :: z)
}

def spaces(n: Int): Unit = {
var rem = n
while (rem >= 16) { writer write " "; rem -= 16 }
if (rem >= 8) { writer write " "; rem -= 8 }
if (rem >= 4) { writer write " "; rem -= 4 }
if (rem >= 2) { writer write " "; rem -= 2}
if (rem == 1) { writer write " " }
}

def fmt(k: Int, state: List[FmtState]): Unit = state match {

case List() => ()

case (_, _, DocNil) :: z =>
fmt(k, z)

case (i, b, DocCons(h, t)) :: z =>
fmt(k, (i, b, h) :: (i, b, t) :: z)

case (i, _, DocText(t)) :: z =>
writer write t
fmt(k + t.length(), z)

case (i, b, DocNest(ii, d)) :: z =>
fmt(k, (i + ii, b, d) :: z)

case (i, true, DocBreak) :: z =>
writer write "\n"
spaces(i)
fmt(i, z)

case (i, false, DocBreak) :: z =>
writer write " "
fmt(k + 1, z)

case (i, b, DocGroup(d)) :: z =>
val fitsFlat = fits(width - k, (i, false, d) :: z)
fmt(k, (i, !fitsFlat, d) :: z)

// case null => ()
}

fmt(0, (0, false, DocGroup(this)) :: Nil)
}
}

object Document {

/** The empty document */
def empty: Document = DocNil

/** A break, which will either be turned into a space or a line break */
def break: Document = DocBreak

/** A document consisting of some text literal */
def text(s: String): Document = DocText(s)

/**
* A group of documents, whose components will either be printed with all breaks
* rendered as spaces, or with all breaks rendered as line breaks.
*/
def group(d: Document): Document = DocGroup(d)

/** A nested document, which will be indented as specified. */
def nest(i: Int, d: Document): Document = DocNest(i, d)
}
Loading

0 comments on commit 45bfbd5

Please sign in to comment.