Skip to content

Commit

Permalink
move GitHubFlavor to Markdown companion
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshalm committed Sep 15, 2023
1 parent f692a95 commit dd90bc5
Show file tree
Hide file tree
Showing 28 changed files with 114 additions and 159 deletions.
51 changes: 50 additions & 1 deletion core/shared/src/main/scala/laika/format/Markdown.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@

package laika.format

import laika.api.bundle.{ BlockParserBuilder, ExtensionBundle, SpanParserBuilder }
import laika.api.bundle.{
BlockParserBuilder,
BundleOrigin,
ExtensionBundle,
ParserBundle,
SpanParserBuilder
}
import laika.api.format.MarkupFormat
import laika.ast.Block
import laika.markdown.bundle.VerbatimHTML
import laika.markdown.github.*
import laika.markdown.{ BlockParsers, InlineParsers, ListParsers }
import laika.parse.Parser
import laika.parse.text.{ CharGroup, TextParsers }
Expand Down Expand Up @@ -170,6 +177,48 @@ case object Markdown extends MarkupFormat {

val extensions: Seq[ExtensionBundle] = Seq(VerbatimHTML)

/** Extension bundle that enables GitHub-Flavored Markdown on top of standard Markdown.
*
* The extension can be added to a transformer like any other extension:
*
* {{{
* val transformer = Transformer
* .from(Markdown)
* .to(HTML)
* .using(GitHubFlavor)
* .build
* }}}
*
* These are the parsers this extension adds to standard Markdown:
*
* - strikethrough
* - auto-links (urls and email addresses)
* - fenced code blocks
* - tables
*
* @author Jens Halm
*/
object GitHubFlavor extends ExtensionBundle {

val description: String = "Github-flavored Markdown"

override val origin: BundleOrigin = BundleOrigin.Parser

override def parsers: ParserBundle = new ParserBundle(
blockParsers = Seq(
Tables.parser
) ++ FencedCodeBlocks.parsers,
spanParsers = Seq(
Strikethrough.parser,
AutoLinks.parsers.www,
AutoLinks.parsers.http,
AutoLinks.parsers.email
)
)

override def forStrictMode: Option[ExtensionBundle] = None
}

override def createBlockListParser(parser: Parser[Block]): Parser[Seq[Block]] =
super.createBlockListParser(BlockParsers.insignificantSpaces ~> parser)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import laika.parse.uri.AutoLinkParsers
*
* @author Jens Halm
*/
private[github] object AutoLinks {
private[laika] object AutoLinks {

private val startChars = NonEmptySet.of('*', '_', '~', '(', ' ', '\n')
private val endChars = NonEmptySet.of('*', '_', '~', ')', '?', '!', '.', ',', ':', ' ', '\n')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import laika.parse.{ BlockSource, Failure, Parser, Success }
*
* @author Jens Halm
*/
private[github] object FencedCodeBlocks {
private[laika] object FencedCodeBlocks {

private def reverse(offset: Int, p: => Parser[String]): Parser[String] = Parser { in =>
p.parse(in.reverse.consume(offset)) match {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import laika.markdown.InlineParsers.enclosedByDoubleChar
*
* @author Jens Halm
*/
private[github] object Strikethrough {
private[laika] object Strikethrough {

val parser: SpanParserBuilder = SpanParserBuilder.recursive { implicit recParsers =>
enclosedByDoubleChar('~').map { Deleted(_) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import laika.parse.implicits._
*
* @author Jens Halm
*/
private[github] object Tables {
private[laika] object Tables {

val parser: BlockParserBuilder = BlockParserBuilder.withSpans { spanParsers =>
def cell(textParser: Parser[LineSource], cellType: CellType): Parser[Cell] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import laika.bundle.BundleProvider
import MarkupFormat.MarkupParsers
import laika.api.bundle.{ BlockParserBuilder, BundleOrigin, ExtensionBundle, SpanParserBuilder }
import laika.markdown.bundle.VerbatimHTML
import laika.markdown.github.GitHubFlavor
import laika.format.Markdown.GitHubFlavor
import munit.FunSuite

/** @author Jens Halm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
package laika.markdown

import laika.api.builder.OperationConfig
import laika.ast._
import laika.ast.*
import laika.ast.sample.ParagraphCompanionShortcuts
import laika.format.Markdown
import laika.markdown.github.GitHubFlavor
import laika.parse.Parser
import laika.parse.markup.RootParser
import munit.FunSuite
Expand All @@ -30,7 +29,7 @@ class GitHubFlavorSpec extends FunSuite with ParagraphCompanionShortcuts {
val rootParser = new RootParser(
Markdown,
new OperationConfig(Markdown.extensions)
.withBundles(Seq(GitHubFlavor)).markupExtensions
.withBundles(Seq(Markdown.GitHubFlavor)).markupExtensions
)

val defaultParser: Parser[RootElement] = rootParser.rootElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ package laika.parse
import laika.api.MarkupParser
import laika.api.errors.ParserError
import laika.format.Markdown
import laika.markdown.github.GitHubFlavor
import munit.FunSuite

/** @author Jens Halm
*/
class PositionTrackingSpec extends FunSuite {

private val parser = MarkupParser.of(Markdown).using(GitHubFlavor).build
private val parser = MarkupParser.of(Markdown).using(Markdown.GitHubFlavor).build

def parseAndExtractMessage(input: String): String = parser.parse(input) match {
case Left(ParserError(message)) => message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import laika.api.errors.ParserError
import laika.ast.*
import laika.config.{ LaikaKeys, SyntaxHighlighting }
import laika.format.Markdown
import laika.markdown.github.GitHubFlavor
import laika.parse.code.CodeCategory
import laika.parse.code.CodeCategory.*
import munit.FunSuite
Expand All @@ -32,7 +31,7 @@ class LanguageSpec extends FunSuite {

private val parser = MarkupParser
.of(Markdown)
.using(GitHubFlavor, SyntaxHighlighting)
.using(Markdown.GitHubFlavor, SyntaxHighlighting)
.withConfigValue(LaikaKeys.firstHeaderAsTitle, true)
.build

Expand Down
4 changes: 2 additions & 2 deletions docs/src/02-running-laika/01-sbt-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,10 @@ Register implementations of the `ExtensionBundle` API, either provided by the li
Example:

```scala mdoc:compile-only
import laika.markdown.github.GitHubFlavor
import laika.format.Markdown
import laika.config.SyntaxHighlighting

laikaExtensions := Seq(GitHubFlavor, SyntaxHighlighting)
laikaExtensions := Seq(Markdown.GitHubFlavor, SyntaxHighlighting)
```

- [Overriding Renderers]: adjust the rendered output for specific AST node types.
Expand Down
17 changes: 7 additions & 10 deletions docs/src/02-running-laika/02-library-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,10 @@ The instances are immutable and thread-safe.
Example for creating a transformer for Markdown, including the GitHub-Flavor extensions:

```scala mdoc:silent
import laika.markdown.github.GitHubFlavor

val transformer = Transformer
.from(Markdown)
.to(HTML)
.using(GitHubFlavor)
.using(Markdown.GitHubFlavor)
.build
```

Expand Down Expand Up @@ -194,13 +192,12 @@ from cats.IO for initialization:
```scala mdoc
import cats.effect.{ Async, Resource }
import laika.io.api.TreeTransformer
import laika.markdown.github.GitHubFlavor

def createTransformer[F[_]: Async]: Resource[F, TreeTransformer[F]] =
Transformer
.from(Markdown)
.to(HTML)
.using(GitHubFlavor)
.using(Markdown.GitHubFlavor)
.parallel[F]
.build
```
Expand Down Expand Up @@ -338,7 +335,7 @@ as well as copying static files over to the target directory.
val transformerIO = Transformer
.from(Markdown)
.to(HTML)
.using(GitHubFlavor)
.using(Markdown.GitHubFlavor)
.parallel[IO]
.build
```
Expand Down Expand Up @@ -366,7 +363,7 @@ merging all content from the input directories into a single, linearized e-book:
val epubTransformer = Transformer
.from(Markdown)
.to(EPUB)
.using(GitHubFlavor)
.using(Markdown.GitHubFlavor)
.parallel[IO]
.build

Expand Down Expand Up @@ -511,7 +508,7 @@ First we set up a parser and its configuration.
We create a parallel parser that is capable of reading from a directory:

```scala mdoc:silent
val parserBuilder = MarkupParser.of(Markdown).using(GitHubFlavor)
val parserBuilder = MarkupParser.of(Markdown).using(Markdown.GitHubFlavor)

val parser = parserBuilder.parallel[IO].build

Expand Down Expand Up @@ -628,7 +625,7 @@ val theme = Helium.defaults
val heliumTransformer = Transformer
.from(Markdown)
.to(HTML)
.using(GitHubFlavor)
.using(Markdown.GitHubFlavor)
.parallel[IO]
.withTheme(theme)
.build
Expand Down Expand Up @@ -697,7 +694,7 @@ import laika.preview.ServerBuilder

val parser = MarkupParser
.of(Markdown)
.using(GitHubFlavor)
.using(Markdown.GitHubFlavor)
.parallel[IO]
.build

Expand Down
12 changes: 4 additions & 8 deletions docs/src/02-running-laika/03-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,11 @@ laikaConfig := LaikaConfig.defaults.strict
```scala mdoc:compile-only
import laika.api._
import laika.format._
import laika.markdown.github.GitHubFlavor

val transformer = Transformer
.from(Markdown)
.to(HTML)
.using(GitHubFlavor)
.using(Markdown.GitHubFlavor)
.strict
.build
```
Expand Down Expand Up @@ -151,12 +150,11 @@ laikaConfig := LaikaConfig.defaults.withRawContent
```scala mdoc:compile-only
import laika.api._
import laika.format._
import laika.markdown.github.GitHubFlavor

val transformer = Transformer
.from(Markdown)
.to(HTML)
.using(GitHubFlavor)
.using(Markdown.GitHubFlavor)
.withRawContent
.build
```
Expand Down Expand Up @@ -254,12 +252,11 @@ laikaConfig := LaikaConfig.defaults
import laika.api._
import laika.format._
import laika.ast.MessageFilter
import laika.markdown.github.GitHubFlavor

val transformer = Transformer
.from(Markdown)
.to(HTML)
.using(GitHubFlavor)
.using(Markdown.GitHubFlavor)
.failOnMessages(MessageFilter.None)
.renderMessages(MessageFilter.Error)
.build
Expand Down Expand Up @@ -347,12 +344,11 @@ laikaConfig := LaikaConfig.defaults
```scala mdoc:compile-only
import laika.api._
import laika.format._
import laika.markdown.github.GitHubFlavor

val transformer = Transformer
.from(Markdown)
.to(HTML)
.using(GitHubFlavor)
.using(Markdown.GitHubFlavor)
.withConfigValue("version.latest", "2.4.6")
.withConfigValue("license", "Apache 2.0")
.build
Expand Down
3 changes: 1 addition & 2 deletions docs/src/03-preparing-content/01-directory-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,11 @@ laikaConfig := LaikaConfig.defaults
import laika.config.LaikaKeys
import laika.api._
import laika.format._
import laika.markdown.github.GitHubFlavor

val transformer = Transformer
.from(Markdown)
.to(HTML)
.using(GitHubFlavor)
.using(Markdown.GitHubFlavor)
.withConfigValue(LaikaKeys.titleDocuments.inputName, "title")
.withConfigValue(LaikaKeys.titleDocuments.outputName, "title")
.build
Expand Down
Loading

0 comments on commit dd90bc5

Please sign in to comment.