Skip to content

Commit

Permalink
remove DocumentParser and DocumentInput from public API
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshalm committed Sep 14, 2023
1 parent d31bf53 commit 489b210
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 40 deletions.
5 changes: 2 additions & 3 deletions core/shared/src/main/scala/laika/bundle/ParserBundle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

package laika.bundle

import laika.ast._
import laika.ast.*
import laika.parse.Parser
import laika.parse.markup.DocumentParser.DocumentInput

/** Bundles a collection of all types of parsers used in a transformation.
*
Expand Down Expand Up @@ -87,7 +86,7 @@ class ParserBundle(
class ParserHooks(
val postProcessBlocks: Seq[Block] => Seq[Block] = identity,
val postProcessDocument: UnresolvedDocument => UnresolvedDocument = identity,
val preProcessInput: DocumentInput => DocumentInput = identity
val preProcessInput: String => String = identity
) {

/** Merges this instance with the specified base.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ case object ReStructuredText extends MarkupFormat { self =>
override val parsers: ParserBundle = new ParserBundle(
markupParserHooks = Some(
new ParserHooks(
preProcessInput = WhitespacePreprocessor.forInput,
preProcessInput = WhitespacePreprocessor.forString,
postProcessDocument = DocInfoExtractor,
postProcessBlocks = LinkTargetProcessor
)
Expand Down
35 changes: 20 additions & 15 deletions core/shared/src/main/scala/laika/parse/markup/DocumentParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import laika.parse.implicits.*
*
* @author Jens Halm
*/
object DocumentParser {
private[laika] object DocumentParser {

case class DocumentInput(path: Path, source: SourceCursor)
private[laika] case class DocumentInput(path: Path, source: SourceCursor)

object DocumentInput {
private[laika] object DocumentInput {

def apply(path: Path, input: String): DocumentInput =
new DocumentInput(path, SourceCursor(input, path))
Expand All @@ -55,15 +55,21 @@ object DocumentParser {
/** Combines the specified markup parsers and extensions and the parser for (optional) configuration
* headers to create a parser function for an entire text markup document.
*/
private[laika] def forMarkup(
def forMarkup(
markupParser: MarkupFormat,
markupExtensions: MarkupExtensions,
configProvider: ConfigProvider
): DocumentInput => Either[ParserError, UnresolvedDocument] = {

val rootParser = new RootParser(markupParser, markupExtensions).rootElement

markupExtensions.parserHooks.preProcessInput andThen
val preProcess: DocumentInput => DocumentInput = input => {
val raw = input.source.input
val preprocessed = markupExtensions.parserHooks.preProcessInput(raw)
input.copy(source = SourceCursor(preprocessed, input.path))
}

preProcess andThen
forMarkup(rootParser, configProvider) andThen {
_.map(markupExtensions.parserHooks.postProcessDocument)
}
Expand All @@ -72,7 +78,7 @@ object DocumentParser {
/** Combines the specified parsers for the root element and for (optional) configuration
* headers to create a parser function for an entire text markup document.
*/
private[laika] def forMarkup(
def forMarkup(
rootParser: Parser[RootElement],
configProvider: ConfigProvider
): DocumentInput => Either[ParserError, UnresolvedDocument] =
Expand All @@ -84,7 +90,7 @@ object DocumentParser {
/** Combines the specified parsers for the root element and for (optional) configuration
* headers to create a parser function for an entire template document.
*/
private[laika] def forTemplate(
def forTemplate(
rootParser: Parser[TemplateRoot],
configProvider: ConfigProvider
): DocumentInput => Either[ParserError, TemplateDocument] =
Expand All @@ -94,7 +100,7 @@ object DocumentParser {

/** Builds a document parser for CSS documents based on the specified parser for style declarations.
*/
private[laika] def forStyleSheets(
def forStyleSheets(
parser: Parser[Set[StyleDeclaration]]
): DocumentInput => Either[ParserError, StyleDeclarationSet] =
forParser { path => parser.map(res => StyleDeclarationSet.forPath(path, res)) }
Expand All @@ -105,13 +111,12 @@ object DocumentParser {
* The specified function is invoked for each parsed document, so that a parser
* dependent on the input path can be created.
*/
private[laika] def forParser[T](p: Path => Parser[T]): DocumentInput => Either[ParserError, T] = {
in =>
Parsers
.consumeAll(p(in.path))
.parse(in.source)
.toEither
.left.map(ParserError(_))
def forParser[T](p: Path => Parser[T]): DocumentInput => Either[ParserError, T] = { in =>
Parsers
.consumeAll(p(in.path))
.parse(in.source)
.toEither
.left.map(ParserError(_))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package laika.parse.text

import laika.parse.SourceCursor
import laika.parse.markup.DocumentParser.DocumentInput

/** Processes whitespace, removing or replacing most whitespace characters except
* for newline and space.
*
Expand Down Expand Up @@ -92,14 +89,4 @@ object WhitespacePreprocessor {
*/
val forString: String => String = new WhitespacePreprocessor

/** Processes the specified Input instance and returns
* a new instance with the same path, but all whitespace
* pre-processed.
*/
val forInput: DocumentInput => DocumentInput = { input =>
val raw = input.source.input
val preprocessed = forString(raw)
input.copy(source = SourceCursor(preprocessed, input.path))
}

}
5 changes: 2 additions & 3 deletions core/shared/src/test/scala/laika/bundle/BundleProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ package laika.bundle

import laika.ast.RewriteRules.RewritePhaseBuilder
import laika.config.{ Config, ConfigParser }
import laika.ast._
import laika.ast.*
import laika.directive.{ DirectiveRegistry, Templates }
import laika.parse.Parser
import laika.parse.markup.DocumentParser.DocumentInput
import laika.rewrite.nav.PathTranslator

/** @author Jens Halm
Expand Down Expand Up @@ -49,7 +48,7 @@ object BundleProvider {
def forParserHooks(
postProcessBlocks: Seq[Block] => Seq[Block] = identity,
postProcessDocument: UnresolvedDocument => UnresolvedDocument = identity,
preProcessInput: DocumentInput => DocumentInput = identity,
preProcessInput: String => String = identity,
origin: BundleOrigin = BundleOrigin.User
): ExtensionBundle = new TestExtensionBundle(origin) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import laika.parse.combinator.Parsers
import laika.parse.css.CSSParsers
import laika.parse.directive.ConfigHeaderParser
import laika.parse.implicits.*
import laika.parse.markup.DocumentParser.DocumentInput
import laika.parse.text.TextParsers
import laika.rewrite.ReferenceResolver.CursorKeys
import munit.FunSuite
Expand Down Expand Up @@ -264,9 +263,8 @@ class ParserHookSpec extends FunSuite with ParserSetup {
)
)

def preProcess(append: String): DocumentInput => DocumentInput = { input =>
val raw = input.source.input
input.copy(source = SourceCursor(raw + append, input.path))
def preProcess(append: String): String => String = { raw =>
raw + append
}

def appendString(root: RootElement, append: String): RootElement =
Expand Down
2 changes: 1 addition & 1 deletion io/src/main/scala/laika/io/model/TextInput.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class TextInput[F[_]: Functor] private (
val sourceFile: Option[FilePath] = None
) extends Navigatable {

lazy val asDocumentInput: F[DocumentInput] = input.map(DocumentInput(path, _))
private[laika] lazy val asDocumentInput: F[DocumentInput] = input.map(DocumentInput(path, _))
}

object TextInput {
Expand Down

0 comments on commit 489b210

Please sign in to comment.