Skip to content

Commit ca67451

Browse files
giacomocavalieriGiacomo Cavalieri
authored and
Giacomo Cavalieri
committed
feat!: add plugin key to adapt links before they are added to the table
1 parent 0e7bf4b commit ca67451

6 files changed

+56
-16
lines changed

src/main/scala/dev/atedeg/EntityUtils.scala

+7-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ object EntityParsing {
3232

3333
object EntityConversion {
3434

35-
def entityToRow(namedEntity: (Option[String], Entity), baseDir: File, allEntities: Set[Entity]): Either[Error, Row] =
36-
extractTermAndDefinition(baseDir / namedEntity._2.sanitizedLink, namedEntity._2, allEntities).map {
35+
def entityToRow(
36+
namedEntity: (Option[String], Entity),
37+
baseDir: File,
38+
allEntities: Set[Entity],
39+
linkSolver: String => String,
40+
): Either[Error, Row] =
41+
extractTermAndDefinition(baseDir / namedEntity._2.sanitizedLink, namedEntity._2, allEntities, linkSolver).map {
3742
case (term, definition) => Row.from(namedEntity._1.getOrElse(term), definition)
3843
}
3944
}

src/main/scala/dev/atedeg/HtmlParsing.scala

+22-7
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,40 @@ object HtmlParsing {
1313
file: File,
1414
entity: Entity,
1515
allEntities: Set[Entity],
16+
linkSolver: String => String,
1617
): Either[Error, (String, String)] = {
1718
val document = JsoupBrowser().parseFile(file.toJava)
1819
val docQuery = s"${entity.entityId.map("div#" + _ + " ").getOrElse("")}div.cover > div.doc"
19-
extractTag(file, document, docQuery, allEntities).map((entity.name, _))
20+
extractTag(file, document, docQuery, allEntities, linkSolver).map((entity.name, _))
2021
}
2122

22-
private def extractTag(file: File, doc: Browser#DocumentType, tag: String, all: Set[Entity]): Either[Error, String] =
23-
doc.tryExtract(element(tag)).map(_.childNodes).toRight(ParseError(file, tag)).flatMap(toMarkdown(_, all))
23+
private def extractTag(
24+
file: File,
25+
doc: Browser#DocumentType,
26+
tag: String,
27+
all: Set[Entity],
28+
linkSolver: String => String,
29+
): Either[Error, String] =
30+
doc
31+
.tryExtract(element(tag))
32+
.map(_.childNodes)
33+
.toRight(ParseError(file, tag))
34+
.flatMap(toMarkdown(_, all, linkSolver))
2435

25-
private def toMarkdown(elems: Iterable[Node], allEntities: Set[Entity]): Either[Error, String] = {
36+
private def toMarkdown(
37+
elems: Iterable[Node],
38+
allEntities: Set[Entity],
39+
linkSolver: String => String,
40+
): Either[Error, String] = {
2641
def isLink(elem: Element) = elem.tagName === "a"
27-
def toMarkdownLink(elem: Element) = lookupLinkFor(extractName(elem)).map(l => s"[${elem.text}]($l)")
28-
def lookupLinkFor(name: String) = allEntities.find(_.name === name).map("../" + _.link).toRight(MissingLink(name))
42+
def toMarkdownLink(elem: Element) = lookupLinkFor(extractName(elem)).map(linkSolver).map(l => s"[${elem.text}]($l)")
43+
def lookupLinkFor(name: String) = allEntities.find(_.name === name).map(_.link).toRight(MissingLink(name))
2944
def extractName(elem: Element) = elem.attr("href").replace(".html", "").split('$').last.split("/").last
3045
elems.foldLeft("".asRight[Error]) { (acc, elem) =>
3146
elem match {
3247
case TextNode(s) => acc.map(_ + s)
3348
case ElementNode(e) if isLink(e) => acc.flatMap(a => toMarkdownLink(e).map(a + _))
34-
case ElementNode(e) => acc.flatMap(a => toMarkdown(e.childNodes, allEntities).map(a + _))
49+
case ElementNode(e) => acc.flatMap(a => toMarkdown(e.childNodes, allEntities, linkSolver).map(a + _))
3550
}
3651
}
3752
}

src/main/scala/dev/atedeg/TableUtils.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ object TableUtils {
1515
table: Table[(Option[String], Entity)],
1616
baseDir: File,
1717
allEntities: Set[Entity],
18+
linkSolver: String => String,
1819
): Either[Error, Table[Row]] =
1920
table.rows
20-
.traverseError(entityToRow(_, baseDir, allEntities))
21+
.traverseError(entityToRow(_, baseDir, allEntities, linkSolver))
2122
.map(Table(table.title, table.termName, table.definitionName, _))
2223

2324
def serialize(table: Table[Row], targetDir: File): Try[Unit] = {

src/main/scala/dev/atedeg/Ubidoc.scala

+16-4
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,24 @@ import ConfigurationValidation._
1515

1616
object Ubidoc {
1717

18-
def apply(lookupDir: JFile, targetDir: JFile, workingDir: JFile, logger: ManagedLogger): Unit =
19-
Internals.ubiquitousScaladocTask(lookupDir.toScala, targetDir.toScala, workingDir.toScala, logger)
18+
def apply(
19+
lookupDir: JFile,
20+
targetDir: JFile,
21+
workingDir: JFile,
22+
linkSolver: String => String,
23+
logger: ManagedLogger,
24+
): Unit =
25+
Internals.ubiquitousScaladocTask(lookupDir.toScala, targetDir.toScala, workingDir.toScala, linkSolver, logger)
2026

2127
private object Internals {
2228

23-
def ubiquitousScaladocTask(lookupDir: File, targetDir: File, workingDir: File, logger: ManagedLogger): Unit = {
29+
def ubiquitousScaladocTask(
30+
lookupDir: File,
31+
targetDir: File,
32+
workingDir: File,
33+
linkSolver: String => String,
34+
logger: ManagedLogger,
35+
): Unit = {
2436
val result = for {
2537
config <- readConfiguration(workingDir)
2638
allEntities <- readAllEntities(lookupDir)
@@ -32,7 +44,7 @@ object Ubidoc {
3244
config.ignored,
3345
logger,
3446
)
35-
tables <- tables.traverseError(entitiesToRows(_, lookupDir, allEntities))
47+
tables <- tables.traverseError(entitiesToRows(_, lookupDir, allEntities, linkSolver))
3648
} yield tables.foreach(serialize(_, targetDir))
3749
result match {
3850
case Left(err) => throw UbidocException(err)

src/main/scala/dev/atedeg/UbiquitousScaladocKeys.scala

+5
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ trait UbiquitousScaladocKeys {
1212

1313
val targetDirectory: SettingKey[File] =
1414
settingKey[File]("The target directory where to put the generated Markdown tables")
15+
16+
val linkSolver: SettingKey[String => String] =
17+
settingKey[String => String](
18+
"A function to adapt the base link in case it needs to be changed to accomodate the site structure",
19+
)
1520
}

src/main/scala/dev/atedeg/UbiquitousScaladocPlugin.scala

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dev.atedeg
22

33
import sbt.Keys.{ baseDirectory, streams }
4-
import sbt.{ AutoPlugin, Setting, Task }
4+
import sbt.{ AutoPlugin, Setting }
55

66
@SuppressWarnings(Array("org.wartremover.warts.Any", "org.wartremover.warts.Nothing"))
77
object UbiquitousScaladocPlugin extends AutoPlugin {
@@ -12,11 +12,13 @@ object UbiquitousScaladocPlugin extends AutoPlugin {
1212

1313
import autoImport.*
1414

15-
override lazy val buildSettings: Seq[Setting[Task[Unit]]] = Seq(
15+
override lazy val buildSettings: Seq[Setting[_]] = Seq(
16+
ubidoc / linkSolver := ((s: String) => s),
1617
ubidoc := Ubidoc(
1718
(ubidoc / lookupDirectory).value,
1819
(ubidoc / targetDirectory).value,
1920
(ubidoc / baseDirectory).value,
21+
(ubidoc / linkSolver).value,
2022
streams.value.log,
2123
),
2224
)

0 commit comments

Comments
 (0)