-
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.
add list, task list, link, img support
- Loading branch information
1 parent
159f866
commit 8c23762
Showing
11 changed files
with
172 additions
and
3 deletions.
There are no files selected for viewing
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,3 @@ | ||
package halotukozak.smark | ||
|
||
def markdown(input: String*): String = input.mkString("\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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package halotukozak.smark | ||
package typography | ||
|
||
type Asterisk | ||
type Plus | ||
type Hyphen | ||
type Unordered = Asterisk | Plus | Hyphen | ||
type Ordered | ||
|
||
private[typography] type ListStyle = Unordered | Ordered | ||
|
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,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 + ")" |
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,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") } | ||
} |
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,5 @@ | ||
package halotukozak.smark | ||
package typography | ||
|
||
|
||
inline def hr: String = "***" |
File renamed without changes.
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,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" | ||
} | ||
} | ||
} | ||
|
||
} |
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,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)" | ||
} | ||
} | ||
} |