Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to laika 1.0.0 #140

Merged
merged 6 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ val circeV = "0.14.6"
val fs2V = "3.9.2"
val http4sDomV = "0.2.10"
val http4sV = "0.23.23"
val laikaV = "0.19.5"
val laikaV = "1.0.0"
val lucilleV = "0.0.1"
val munitCatsEffectV = "2.0.0-M3"
val munitV = "1.0.0-M10"
Expand Down Expand Up @@ -90,8 +90,8 @@ lazy val laikaIO = crossProject(JVMPlatform)
"co.fs2" %%% "fs2-io" % fs2V,
"org.scodec" %%% "scodec-core" % scodecV(scalaVersion.value),
"pink.cozydev" %%% "lucille" % lucilleV,
"org.planet42" %%% "laika-core" % laikaV,
"org.planet42" %%% "laika-io" % laikaV,
"org.typelevel" %%% "laika-core" % laikaV,
"org.typelevel" %%% "laika-io" % laikaV,
"org.scalameta" %%% "munit" % munitV % Test,
"org.typelevel" %%% "munit-cats-effect" % munitCatsEffectV % Test,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,43 @@

package pink.cozydev.protosearch.analysis

import cats.data.NonEmptyList
import cats.effect.IO
import laika.api.MarkupParser
import laika.format.Markdown
import laika.io.implicits._
import laika.markdown.github.GitHubFlavor
import laika.parse.code.SyntaxHighlighting
import laika.parse.markup.DocumentParser.RendererError
import laika.format.Markdown.GitHubFlavor
import laika.config.SyntaxHighlighting
import laika.io.syntax._
import laika.api.Renderer
import cats.effect.IOApp
import cats.effect.kernel.Resource
import laika.api.errors.RendererError
import cats.data.NonEmptyList

object DocsDirectory {
object DocsDirectory extends IOApp.Simple {

val mdParser = MarkupParser
val parserBuilder = MarkupParser
.of(Markdown)
.using(GitHubFlavor, SyntaxHighlighting)
.withConfigValue("laika.validateLinks", false)

val parser = parserBuilder
.parallel[IO]
.build

val config = parserBuilder.config

val plaintextRenderer = Renderer.of(Plaintext).withConfig(config).parallel[IO].build

val run = Resource
.both(parser, plaintextRenderer)
.use { case (parser, renderer) =>
parser
.fromDirectory("/home/andrew/src/github.com/cozydev/protosearch/docs")
.parse
.flatMap(tree => renderer.from(tree.root).toDirectory("outputDir").render.void)
}

def dirToDocs(dirPath: String): IO[Seq[Either[RendererError, NonEmptyList[SubDocument]]]] =
mdParser.use(parser =>
parser.use(parser =>
parser.fromDirectory(dirPath).parse.map { tree =>
tree.root.allDocuments.map(IngestMarkdown.renderSubDocuments)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2022 CozyDev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package pink.cozydev.protosearch.analysis

import cats.effect.{Async, Resource}
import fs2.Stream
import laika.api.format.{BinaryPostProcessor, Formatter, TwoPhaseRenderFormat, RenderFormat}
import laika.ast.*
import laika.io.model.{BinaryOutput, RenderedTreeRoot}
import laika.api.builder.OperationConfig
import laika.api.config.Config
import laika.theme.Theme
import java.io.OutputStream

case object IndexFormat extends TwoPhaseRenderFormat[Formatter, BinaryPostProcessor.Builder] {

override def description: String = "Protosearch Index"

def interimFormat: RenderFormat[Formatter] = Plaintext

def prepareTree(tree: DocumentTreeRoot): Either[Throwable, DocumentTreeRoot] =
Right(tree) // no-op

/** Post processor that produces the final result based on the interim format.
*/
def postProcessor: BinaryPostProcessor.Builder = new BinaryPostProcessor.Builder {

def build[F[_]: Async](config: Config, theme: Theme[F]): Resource[F, BinaryPostProcessor[F]] =
Resource.pure[F, BinaryPostProcessor[F]](new BinaryPostProcessor[F] {

def process(
result: RenderedTreeRoot[F],
output: BinaryOutput[F],
config: OperationConfig,
): F[Unit] = {
val strs: List[String] = result.allDocuments.map(_.content).toList
val bytes: Stream[F, Byte] = fs2.Stream.emits[F, String](strs).map(_.toByte)
val outputStream: Resource[F, OutputStream] = output.resource
outputStream.use { os =>
val fos = Async[F].pure(os)
val pipe = fs2.io.writeOutputStream(fos)
bytes.through(pipe).compile.drain
}
}
})

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import laika.ast.RootElement
import laika.ast.Section
import laika.ast.Text
import laika.format.Markdown
import laika.markdown.github.GitHubFlavor
import laika.parse.code.SyntaxHighlighting
import laika.parse.markup.DocumentParser.RendererError
import laika.format.Markdown.GitHubFlavor
import laika.config.SyntaxHighlighting
import laika.api.errors.{RendererError, TransformationError}

case class SubDocument(fileName: String, anchor: Option[String], title: String, content: String)

Expand Down Expand Up @@ -87,10 +87,9 @@ object IngestMarkdown {
subDocs.sequence
}

def transform(input: String): Either[RendererError, NonEmptyList[SubDocument]] =
def transform(input: String): Either[TransformationError, NonEmptyList[SubDocument]] =
parser
.parse(input)
.leftMap(e => RendererError(e.message, e.path))
.flatMap(renderSubDocuments)

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@
package pink.cozydev.protosearch.analysis

import laika.ast._
import laika.render.TextFormatter
import laika.factory.RenderFormat
import laika.factory.RenderContext
import laika.render.Indentation
import laika.api.format.{Formatter, RenderFormat}

object PlaintextRenderer extends ((TextFormatter, Element) => String) {
object PlaintextRenderer extends ((Formatter, Element) => String) {

private case class Content(content: Seq[Element], options: Options = NoOpt)
private case class Content(content: Seq[Element], options: Options = Options.empty)
extends Element
with ElementContainer[Element] {
type Self = Content
def withOptions(options: Options): Content = copy(options = options)
}

def apply(fmt: TextFormatter, element: Element): String = {
def apply(fmt: Formatter, element: Element): String = {

def renderElement(e: Element): String = {
val (elements, _) = e.productIterator.partition(_.isInstanceOf[Element])
Expand All @@ -56,15 +53,12 @@ object PlaintextRenderer extends ((TextFormatter, Element) => String) {
}
}

case object Plaintext extends RenderFormat[TextFormatter] {
case object Plaintext extends RenderFormat[Formatter] {
val fileSuffix = "txt"

val defaultRenderer: (TextFormatter, Element) => String = PlaintextRenderer
val defaultRenderer: (Formatter, Element) => String =
PlaintextRenderer

val formatterFactory: RenderContext[TextFormatter] => TextFormatter = PlaintextFormatter
}

object PlaintextFormatter extends (RenderContext[TextFormatter] => TextFormatter) {
def apply(context: RenderContext[TextFormatter]): TextFormatter =
TextFormatter(context.renderChild, context.root, Nil, Indentation.default)
val formatterFactory: Formatter.Context[Formatter] => Formatter =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor thing: to reduce a bit if boilerplate you could also simply inline this like here:
https://github.com/typelevel/Laika/blob/main/core/shared/src/main/scala/laika/format/AST.scala#L41

context => Formatter.defaultFactory(context.withIndentation(Formatter.Indentation.default))
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import io.circe.syntax._
import fs2.{Chunk, Stream}
import fs2.io.file.{Files, Path}
import fs2.text.utf8
import laika.parse.markup.DocumentParser.RendererError
import laika.api.errors.TransformationError
import pink.cozydev.protosearch.analysis.DocsDirectory
import pink.cozydev.protosearch.analysis.SubDocument

Expand All @@ -35,7 +35,7 @@ object DocumentationIndexWriterApp extends IOApp.Simple {
val pathHttp4sDocs = pathHttp4s / "docs/docs/"

def collectDocs(
subDocs: Either[RendererError, NonEmptyList[SubDocument]]
subDocs: Either[TransformationError, NonEmptyList[SubDocument]]
): List[Doc] =
subDocs match {
case Left(_) => Nil // swallow errors
Expand Down