-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd617ce
commit 41e2b2a
Showing
9 changed files
with
155 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package halotukozak.smark | ||
package tables | ||
|
||
|
||
import tables.Column.symbol | ||
|
||
import scala.collection.mutable.ArrayBuffer | ||
|
||
private final class Table(private[tables] val rows: ArrayBuffer[Row | Header] = new ArrayBuffer[Row | Header]) extends AnyVal: | ||
extension (ab: Iterable[String]) | ||
private def mkRow = ab.mkString("| ", " | ", " |") | ||
|
||
private[tables] def eval = rows.map { | ||
case r: Row => r.cells.map(_.elem).mkRow | ||
case h: Header => h.elements.map(_.name).mkRow + "\n" + h.elements.map(symbol).mkRow | ||
}.mkString("\n") | ||
|
||
|
||
private class Row(private[tables] val cells: ArrayBuffer[Cell] = new ArrayBuffer[Cell]) extends AnyVal | ||
|
||
private class Header(private[tables] val elements: Seq[Column]) extends AnyVal | ||
|
||
|
||
sealed abstract class Column(val name: String) | ||
|
||
private object Column: | ||
final case class Left(override val name: String) extends Column(name) | ||
|
||
final case class Right(override val name: String) extends Column(name) | ||
|
||
final case class Center(override val name: String) extends Column(name) | ||
|
||
final case class None(override val name: String) extends Column(name) | ||
|
||
def symbol: Column => String = (_: Column) match | ||
case Left(name) => ":---" | ||
case Right(name) => "---:" | ||
case Center(name) => ":---:" | ||
case None(name) => "---" | ||
|
||
end Column | ||
|
||
type Left = Column.Left | ||
type Right = Column.Right | ||
type Center = Column.Center | ||
type None = Column.None | ||
given Conversion[String, Left] = Column.Left(_) | ||
given Conversion[String, Right] = Column.Right(_) | ||
given Conversion[String, Center] = Column.Center(_) | ||
given Conversion[String, None] = Column.None(_) | ||
|
||
|
||
private final class Cell(private[tables] val elem: String) extends AnyVal | ||
|
||
def table(init: Table ?=> Unit): String = | ||
given t: Table = Table() | ||
|
||
init(using t) | ||
t.eval | ||
|
||
def row(init: Row ?=> Unit)(using t: Table): Unit = | ||
given r: Row = Row() | ||
|
||
init(using r) | ||
t.rows += r | ||
|
||
def header(columns: (Column | String)*)(using t: Table): Unit = | ||
t.rows += Header(columns.map { | ||
case c: Column => c | ||
case s: String => s: None | ||
}) | ||
|
||
def cell(str: String)(using r: Row): Unit = r.cells += Cell(str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,5 @@ | ||
package halotukozak.smark | ||
package typography | ||
|
||
import typography.macros.heading | ||
|
||
|
||
private[typography] type HeadingLevel = 1 | 2 | 3 | 4 | 5 | 6 | ||
|
||
def headingUnsafe(n: Int, inner: String): String = n match | ||
case 1 => heading[1](inner) | ||
case 2 => heading[2](inner) | ||
case 3 => heading[3](inner) | ||
case 4 => heading[4](inner) | ||
case 5 => heading[5](inner) | ||
case 6 => heading[6](inner) | ||
case _ => throw IllegalArgumentException(s"Invalid heading level: $n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package halotukozak.smark | ||
package tables | ||
|
||
import typography.macros.text | ||
import typography.{Bold, Code, ScalaHelper} | ||
|
||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
|
||
final class TableTest extends AnyWordSpec with Matchers { | ||
|
||
"table" should { | ||
"be evaluated" in { | ||
table { | ||
row {} | ||
} shouldBe "| |" | ||
|
||
table { | ||
row { | ||
cell("a") | ||
cell("b") | ||
} | ||
} shouldBe "| a | b |" | ||
|
||
table { | ||
row { | ||
cell { | ||
text[Bold]("a") | ||
} | ||
cell("b") | ||
} | ||
row { | ||
cell { | ||
text[Code](scala"val x = 1") | ||
} | ||
cell("d") | ||
} | ||
} shouldBe "| **a** | b |\n| `val x = 1` | d |" | ||
|
||
table { | ||
header("a": Left, "b") | ||
row { | ||
cell("1") | ||
cell("2") | ||
} | ||
} shouldBe "| a | b |\n| :--- | --- |\n| 1 | 2 |" | ||
|
||
table { | ||
header("a": Right, "b": Center) | ||
row { | ||
cell("1") | ||
cell("2") | ||
} | ||
} shouldBe "| a | b |\n| ---: | :---: |\n| 1 | 2 |" | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters