From 45bfbd5ac6acc958a5ee689a97defec6b6fd82a2 Mon Sep 17 00:00:00 2001 From: Jose Emilio Labra Gayo Date: Fri, 4 Jun 2021 07:48:20 +0200 Subject: [PATCH] Added a module/document project --- .github/workflows/mdoc.yml | 20 ++ build.sbt | 10 +- .../document/src}/asciidoctor/index.adoc | 32 +-- .../scala/es/weso/document/Document.scala | 270 +++++++++--------- .../scala/es/weso/document/DocumentTest.scala | 192 ++++++------- 5 files changed, 272 insertions(+), 252 deletions(-) create mode 100644 .github/workflows/mdoc.yml rename {src => modules/document/src}/asciidoctor/index.adoc (98%) rename {src => modules/document/src}/main/scala/es/weso/document/Document.scala (96%) rename {src => modules/document/src}/test/scala/es/weso/document/DocumentTest.scala (96%) diff --git a/.github/workflows/mdoc.yml b/.github/workflows/mdoc.yml new file mode 100644 index 0000000..c088724 --- /dev/null +++ b/.github/workflows/mdoc.yml @@ -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: adopt@1.11 + - uses: olafurpg/setup-gpg@v3 + - run: sbt docs/docusaurusPublishGhpages + env: + GIT_DEPLOY_KEY: ${{ secrets.GIT_DEPLOY_KEY }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/build.sbt b/build.sbt index be1e208..0c49658 100644 --- a/build.sbt +++ b/build.sbt @@ -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, @@ -73,8 +75,6 @@ lazy val document = project coverageHighlighting := true, testFrameworks += new TestFramework("munit.Framework"), ) -// .aggregate(docs) - /* Docs generation */ diff --git a/src/asciidoctor/index.adoc b/modules/document/src/asciidoctor/index.adoc similarity index 98% rename from src/asciidoctor/index.adoc rename to modules/document/src/asciidoctor/index.adoc index 250963c..50dc411 100644 --- a/src/asciidoctor/index.adoc +++ b/modules/document/src/asciidoctor/index.adoc @@ -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] diff --git a/src/main/scala/es/weso/document/Document.scala b/modules/document/src/main/scala/es/weso/document/Document.scala similarity index 96% rename from src/main/scala/es/weso/document/Document.scala rename to modules/document/src/main/scala/es/weso/document/Document.scala index 96fdf25..92e145b 100644 --- a/src/main/scala/es/weso/document/Document.scala +++ b/modules/document/src/main/scala/es/weso/document/Document.scala @@ -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) } \ No newline at end of file diff --git a/src/test/scala/es/weso/document/DocumentTest.scala b/modules/document/src/test/scala/es/weso/document/DocumentTest.scala similarity index 96% rename from src/test/scala/es/weso/document/DocumentTest.scala rename to modules/document/src/test/scala/es/weso/document/DocumentTest.scala index a0deff2..fd714a0 100644 --- a/src/test/scala/es/weso/document/DocumentTest.scala +++ b/modules/document/src/test/scala/es/weso/document/DocumentTest.scala @@ -1,97 +1,97 @@ -package es.weso.document -import java.io.StringWriter -import munit.FunSuite - -class DocumentTest extends FunSuite { - - test(s"Combine 2 null documents same line") { - val w: StringWriter = new StringWriter - (DocNil :: DocNil).format(1,w) - assertEquals(w.toString, "") - } - - test(s"Combine 2 null documents next line") { - val w: StringWriter = new StringWriter - (DocNil :: DocNil).format(1,w) - assertEquals(w.toString, "") - } - - - test("Combine 2 text documents same line") { - val w: StringWriter = new StringWriter - (DocText("a") :: DocText("b")).format(1,w) - assertEquals(w.toString, "ab") - } - - test("Combine 2 text documents next line") { - val w: StringWriter = new StringWriter - (DocText("a") :/: DocText("b")).format(1,w) - assertEquals(w.toString, "a\nb") - } - - test("Combine 2 group documents same line") { - val w: StringWriter = new StringWriter - (DocGroup(DocText("a")) :: DocGroup(DocText("b"))).format(1,w) - assertEquals(w.toString, "ab") - } - - test("Combine 2 group documents next line") { - val w: StringWriter = new StringWriter - (DocGroup(DocText("a")) :/: DocGroup(DocText("b"))).format(1,w) - assertEquals(w.toString, "a\nb") - } - - test("Combine 2 nested documents same line") { - val w: StringWriter = new StringWriter - (DocNest(1, DocText("a")) :: DocNest(1, DocText("b"))).format(1,w) - assertEquals(w.toString, "ab") - } - - test("Combine 2 nested documents next line") { - val w: StringWriter = new StringWriter - (DocNest(1, DocText("a")) :/: DocNest(1, DocText("b"))).format(1,w) - assertEquals(w.toString, "a\nb") - } - - test("Combine a text document and a string in same line") { - val w: StringWriter = new StringWriter - ("a" :: DocText("b")).format(1,w) - assertEquals(w.toString, "ab") - } - - test("Combine a text document and a string in next line") { - val w: StringWriter = new StringWriter - ("a" :/: DocText("b")).format(1,w) - assertEquals(w.toString, "a\nb") - } - - test("Create an empty text document") { - val w: StringWriter = new StringWriter - Document.empty.format(1,w) - assertEquals(w.toString, "") - } - - test("Create a break document") { - val w: StringWriter = new StringWriter - Document.break.format(1,w) - assertEquals(w.toString, " ") - } - - test("Create a text document from a string") { - val w: StringWriter = new StringWriter - Document.text("a").format(1,w) - assertEquals(w.toString, "a") - } - - test("Create a document group from a single doc") { - val w: StringWriter = new StringWriter - Document.group(DocText("a")).format(1,w) - assertEquals(w.toString, "a") - } - - test("Create a document nest from a single doc") { - val w: StringWriter = new StringWriter - Document.nest(1, DocText("a")).format(1,w) - assertEquals(w.toString, "a") - } +package es.weso.document +import java.io.StringWriter +import munit.FunSuite + +class DocumentTest extends FunSuite { + + test(s"Combine 2 null documents same line") { + val w: StringWriter = new StringWriter + (DocNil :: DocNil).format(1,w) + assertEquals(w.toString, "") + } + + test(s"Combine 2 null documents next line") { + val w: StringWriter = new StringWriter + (DocNil :: DocNil).format(1,w) + assertEquals(w.toString, "") + } + + + test("Combine 2 text documents same line") { + val w: StringWriter = new StringWriter + (DocText("a") :: DocText("b")).format(1,w) + assertEquals(w.toString, "ab") + } + + test("Combine 2 text documents next line") { + val w: StringWriter = new StringWriter + (DocText("a") :/: DocText("b")).format(1,w) + assertEquals(w.toString, "a\nb") + } + + test("Combine 2 group documents same line") { + val w: StringWriter = new StringWriter + (DocGroup(DocText("a")) :: DocGroup(DocText("b"))).format(1,w) + assertEquals(w.toString, "ab") + } + + test("Combine 2 group documents next line") { + val w: StringWriter = new StringWriter + (DocGroup(DocText("a")) :/: DocGroup(DocText("b"))).format(1,w) + assertEquals(w.toString, "a\nb") + } + + test("Combine 2 nested documents same line") { + val w: StringWriter = new StringWriter + (DocNest(1, DocText("a")) :: DocNest(1, DocText("b"))).format(1,w) + assertEquals(w.toString, "ab") + } + + test("Combine 2 nested documents next line") { + val w: StringWriter = new StringWriter + (DocNest(1, DocText("a")) :/: DocNest(1, DocText("b"))).format(1,w) + assertEquals(w.toString, "a\nb") + } + + test("Combine a text document and a string in same line") { + val w: StringWriter = new StringWriter + ("a" :: DocText("b")).format(1,w) + assertEquals(w.toString, "ab") + } + + test("Combine a text document and a string in next line") { + val w: StringWriter = new StringWriter + ("a" :/: DocText("b")).format(1,w) + assertEquals(w.toString, "a\nb") + } + + test("Create an empty text document") { + val w: StringWriter = new StringWriter + Document.empty.format(1,w) + assertEquals(w.toString, "") + } + + test("Create a break document") { + val w: StringWriter = new StringWriter + Document.break.format(1,w) + assertEquals(w.toString, " ") + } + + test("Create a text document from a string") { + val w: StringWriter = new StringWriter + Document.text("a").format(1,w) + assertEquals(w.toString, "a") + } + + test("Create a document group from a single doc") { + val w: StringWriter = new StringWriter + Document.group(DocText("a")).format(1,w) + assertEquals(w.toString, "a") + } + + test("Create a document nest from a single doc") { + val w: StringWriter = new StringWriter + Document.nest(1, DocText("a")).format(1,w) + assertEquals(w.toString, "a") + } } \ No newline at end of file