Skip to content

Commit

Permalink
table enhacements
Browse files Browse the repository at this point in the history
  • Loading branch information
halotukozak committed May 12, 2024
1 parent cd617ce commit 41e2b2a
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .idea/sbt.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/scala_compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ lazy val root = (project in file("."))
.settings(
name := "Smark",
idePackagePrefix := Some("halotukozak.smark"),
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.17" % Test,
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.18" % Test,
)
73 changes: 73 additions & 0 deletions src/main/scala/tables/Table.scala
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)
11 changes: 0 additions & 11 deletions src/main/scala/typography/Headings.scala
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")
5 changes: 1 addition & 4 deletions src/main/scala/typography/macros/Headings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,4 @@ package typography.macros

import typography.HeadingLevel

import scala.quoted.{Expr, Quotes}

inline def heading[N <: HeadingLevel : ValueOf](inline inner: String): String = ${ headingImpl('{ inner }, '{ valueOf[N] }) }
private def headingImpl(inner: Expr[String], n: Expr[Int])(using Quotes): Expr[String] = '{ "#" * $n + $inner }
inline def heading[N <: HeadingLevel : ValueOf](inline inner: String): String = "#" * valueOf[N] + inner
20 changes: 19 additions & 1 deletion src/test/scala/integration/IntegrationTest.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package halotukozak.smark
package integration

import tables.*
import typography.*
import typography.macros.*

Expand Down Expand Up @@ -37,6 +38,20 @@ class IntegrationTest extends AnyWordSpec with Matchers {
),
image["Google"](url = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"),
link("https://www.google.com", "Google"),
table {
row {
cell {
text[Bold]("a")
}
cell("b")
}
row {
cell {
text[Code](scala"val x = 1")
}
cell("d")
}
},
) shouldEqual
"""hello
|
Expand Down Expand Up @@ -71,7 +86,10 @@ class IntegrationTest extends AnyWordSpec with Matchers {
|
|![Google](https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png)
|
|[Google](https://www.google.com)""".stripMargin
|[Google](https://www.google.com)
|
|| **a** | b |
|| `val x = 1` | d |""".stripMargin
}
}

Expand Down
59 changes: 59 additions & 0 deletions src/test/scala/tables/TableTest.scala
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 |"
}
}

}
16 changes: 0 additions & 16 deletions src/test/scala/typography/HeadingTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package halotukozak.smark
package typography


import typography.headingUnsafe
import typography.macros.*

import org.scalatest.matchers.should.Matchers
Expand All @@ -11,7 +10,6 @@ import org.scalatest.wordspec.AnyWordSpec
final class HeadingTest extends AnyWordSpec with Matchers {

"Heading" should {

"be evaluated" when {
"level 1" in {
heading[1]("Hello") shouldBe "#Hello"
Expand Down Expand Up @@ -40,19 +38,5 @@ final class HeadingTest extends AnyWordSpec with Matchers {
assertDoesNotCompile("""heading[-5]("Hello")""")
}
}

"unsafe should be evaluated" when {
"in range" in {
for (i <- 1 to 6) do
headingUnsafe(i, "Hello") shouldBe "#" * i + "Hello"
}

"out of range" in {
for (i <- 7 to 10) do
assertThrows[IllegalArgumentException](headingUnsafe(i, "Hello"))
for (i <- -5 to 0) do
assertThrows[IllegalArgumentException](headingUnsafe(i, "Hello"))
}
}
}
}

0 comments on commit 41e2b2a

Please sign in to comment.