Skip to content

Commit

Permalink
add list, task list, link, img support
Browse files Browse the repository at this point in the history
  • Loading branch information
halotukozak committed Apr 6, 2024
1 parent 159f866 commit 241ffdb
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/main/scala/Markdown.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package halotukozak.smark

def markdown(input: String*): String = input.mkString("\n")
5 changes: 5 additions & 0 deletions src/main/scala/typography/Code.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ implicit class ScalaHelper(private val sc: StringContext) extends AnyVal {
def java(args: Any*): java = sc.s(args *).asInstanceOf[java]

def kotlin(args: Any*): kotlin = sc.s(args *).asInstanceOf[kotlin]

def sql(args: Any*): sql = sc.s(args *).asInstanceOf[sql]

def bash(args: Any*): bash = sc.s(args *).asInstanceOf[bash]


def html(args: Any*): String = "\n" + sc.s(args *) + "\n"
}
13 changes: 13 additions & 0 deletions src/main/scala/typography/List.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package halotukozak.smark
package typography

import utils.Default

type Asterisk
type Plus
type Hyphen
type Unordered = Asterisk | Plus | Hyphen
type Ordered

private[typography] type ListStyle = Unordered | Ordered

13 changes: 13 additions & 0 deletions src/main/scala/typography/Reference.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package halotukozak.smark
package typography

import utils.nameOf

private inline def titleFrom[Title] = nameOf[Title].filterNot(_ == '"') match
case "scala.Predef.String" => ""
case t => t
inline def link[Title <: String](inline url: String): String = "[" + titleFrom[Title] + "](" + url + ")"
inline def link(inline url: String, inline title: String): String = "[" + title + "](" + url + ")"

inline def image[title <: String](inline url: String): String = "![" + titleFrom[title] + "](" + url + ")"
inline def image(inline url: String, inline title: String): String = "![" + title + "](" + url + ")"
31 changes: 31 additions & 0 deletions src/main/scala/typography/macros/List.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package halotukozak.smark
package typography.macros

import typography.*

import _root_.scala.quoted.{Expr, Quotes, Type}

inline def list[Style <: ListStyle](inline inner: String*): String = ${ listImpl[Style]('{ inner }) }
private def listImpl[Style <: ListStyle : Type](inner: Expr[Seq[String]])(using Quotes): Expr[String] = {
Type.of[Style] match {
case '[Ordered] => '{ $inner.zipWithIndex.map((s, i) => s"${i + 1}. $s").mkString("\n") }
case '[Unordered] =>
'{
val marker = ${ Expr(Type.of[Style] match
case '[Plus] => "+"
case '[Hyphen] => "-"
case _ => "*"
) }
$inner.map(s => s"$marker $s").mkString("\n")
}
}
}

inline def taskList(inline points: ((Boolean, String) | String)*): String = ${ taskListImpl('{ points }) }
private def taskListImpl(points: Expr[Seq[(Boolean, String) | String]])(using Quotes): Expr[String] = {
'{ $points.map {
case (true, s) => s"- [x] $s"
case (false, s) => s"- [ ] $s"
case s: String => s"- [ ] $s"
}.mkString("\n") }
}
6 changes: 3 additions & 3 deletions src/main/scala/typography/macros/Text.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import typography.*

import _root_.scala.quoted.{Expr, Quotes, Type}

inline def text[Styles <: TextStyle](inline inner: String): String = ${ textImpl[Styles]('{ inner }) }
private def textImpl[Styles <: TextStyle : Type](inner: Expr[String])(using Quotes): Expr[String] = {
Type.of[Styles] match {
inline def text[Style <: TextStyle](inline inner: String): String = ${ textImpl[Style]('{ inner }) }
private def textImpl[Style <: TextStyle : Type](inner: Expr[String])(using Quotes): Expr[String] = {
Type.of[Style] match {
case '[Normal] => inner
case '[Bold & Italic] => '{ "***" + $inner + "***" }
case '[Bold] => '{ "**" + $inner + "**" }
Expand Down
5 changes: 5 additions & 0 deletions src/main/scala/typography/other.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package halotukozak.smark
package typography


inline def hr: String = "***"
File renamed without changes.
6 changes: 6 additions & 0 deletions src/test/scala/typography/CodeTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ final class CodeTest extends AnyWordSpec with Matchers {
|echo "HelloWorld"
|```""".stripMargin
}
"html" in {
html"""<h1>HelloWorld</h1>""" shouldBe
"""
|<h1>HelloWorld</h1>
|""".stripMargin
}
"Custom" in {
codeUnsafe(
"custom",
Expand Down
58 changes: 58 additions & 0 deletions src/test/scala/typography/ListTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package halotukozak.smark
package typography

import typography.macros.{list, taskList}

import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

final class ListTest extends AnyWordSpec with Matchers {

"List" should {
"be evaluated" when {
"asterisk" in {
list[Asterisk]("a", "b", "c") shouldBe "* a\n* b\n* c"
}
"plus" in {
list[Plus]("a", "b", "c") shouldBe "+ a\n+ b\n+ c"
}
"hyphen" in {
list[Hyphen]("a", "b", "c") shouldBe "- a\n- b\n- c"
}
"unordered" in {
list[Unordered]("a", "b", "c") shouldBe "* a\n* b\n* c"
}
"ordered" in {
list[Ordered]("a", "b", "c") shouldBe "1. a\n2. b\n3. c"
}
"default" ignore {
// list("a", "b", "c") shouldBe "* a\n* b\n* c"
}


}
}

"task list" should {
"be evaluated" when {
"only task" in {
taskList("a", "b", "c") shouldBe "- [ ] a\n- [ ] b\n- [ ] c"
}
"with completion status" in {
taskList(
true -> "a",
false -> "b",
true -> "c",
) shouldBe "- [x] a\n- [ ] b\n- [x] c"
}
"mixed" in {
taskList(
true -> "a",
"b",
false -> "c",
) shouldBe "- [x] a\n- [ ] b\n- [ ] c"
}
}
}

}
37 changes: 37 additions & 0 deletions src/test/scala/typography/ReferenceTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package halotukozak.smark
package typography

import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

final class ReferenceTest extends AnyWordSpec with Matchers {

"link" should {
"be evaluated correctly" in {
link["Google"]("https://www.google.com") shouldBe "[Google](https://www.google.com)"
link[""]("https://www.google.com") shouldBe "[](https://www.google.com)"
link("https://www.google.com") shouldBe "[](https://www.google.com)"

for name <- Seq("google", "facebook", "twitter") do
link("https://www." + name + ".com") shouldBe s"[](https://www.$name.com)"

for name <- Seq("google", "facebook", "twitter") do
link("https://www." + name + ".com", name) shouldBe s"[$name](https://www.$name.com)"

}
}

"image" should {
"be evaluated correctly" in {
image["img"]("https://www.google.com") shouldBe "![img](https://www.google.com)"
image[""]("https://www.google.com") shouldBe "![](https://www.google.com)"
image("https://www.google.com") shouldBe "![](https://www.google.com)"

for name <- Seq("google", "facebook", "twitter") do
image("https://www." + name + ".com") shouldBe s"![](https://www.$name.com)"

for name <- Seq("google", "facebook", "twitter") do
image("https://www." + name + ".com", name) shouldBe s"![$name](https://www.$name.com)"
}
}
}

0 comments on commit 241ffdb

Please sign in to comment.