Skip to content

Commit

Permalink
Merge pull request #474 from typelevel/api/internal
Browse files Browse the repository at this point in the history
shrink public API surface in core runtime
  • Loading branch information
jenshalm authored Jul 14, 2023
2 parents a591be5 + d6bf9e8 commit a5b143d
Show file tree
Hide file tree
Showing 21 changed files with 107 additions and 85 deletions.
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/laika/render/ASTRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import laika.ast._
*
* @author Jens Halm
*/
object ASTRenderer extends ((TextFormatter, Element) => String) {
private[laika] object ASTRenderer extends ((TextFormatter, Element) => String) {

/** The maximum width of a single text element.
* For any text that exceeds this limit only the beginning
Expand All @@ -31,7 +31,7 @@ object ASTRenderer extends ((TextFormatter, Element) => String) {
* for the majority of cases where primarily the document
* structure is relevant.
*/
val maxTextWidth = 50
private val maxTextWidth = 50

private case class Content(content: Seq[Element], desc: String, options: Options = NoOpt)
extends Element with ElementContainer[Element] {
Expand Down
42 changes: 30 additions & 12 deletions core/shared/src/main/scala/laika/render/FOFormatter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package laika.render

import laika.ast.Path.Root
import laika.ast.{ InternalTarget, _ }
import laika.ast.*
import laika.factory.RenderContext
import laika.rewrite.nav.PathTranslator

Expand All @@ -35,13 +35,13 @@ import laika.rewrite.nav.PathTranslator
*
* @author Jens Halm
*/
case class FOFormatter(
class FOFormatter private (
renderChild: (FOFormatter, Element) => String,
currentElement: Element,
parents: List[Element],
pathTranslator: PathTranslator,
path: Path,
styles: StyleDeclarationSet,
val parents: List[Element],
val pathTranslator: PathTranslator,
val path: Path,
val styles: StyleDeclarationSet,
indentation: Indentation,
messageFilter: MessageFilter
) extends TagFormatter[FOFormatter](
Expand All @@ -54,10 +54,28 @@ case class FOFormatter(
type StyleHint = Element

protected def withChild(element: Element): FOFormatter =
copy(parents = currentElement :: parents, currentElement = element)
new FOFormatter(
renderChild,
element,
currentElement :: parents,
pathTranslator,
path,
styles,
indentation,
messageFilter
)

protected def withIndentation(newIndentation: Indentation): FOFormatter =
copy(indentation = newIndentation)
new FOFormatter(
renderChild,
currentElement,
parents,
pathTranslator,
path,
styles,
newIndentation,
messageFilter
)

private lazy val (footnotes, citations) = parents.lastOption.getOrElse(currentElement) match {
case et: ElementTraversal =>
Expand Down Expand Up @@ -100,7 +118,7 @@ case class FOFormatter(
/** Generates an id that is unique within the entire document tree for the
* specified local reference.
*/
def buildLocalId(ref: String): String = {
private def buildLocalId(ref: String): String = {
if (ref.isEmpty) buildId(path)
else if (path == Root) buildId(Path.parse(s"#$ref"))
else buildId(path.withFragment(ref))
Expand Down Expand Up @@ -128,8 +146,8 @@ case class FOFormatter(
def block(styleHint: Element, attr: (String, String)*): String =
emptyElement("fo:block", styleHint, attr: _*)

/** Renders an FO `block` element and the specified nested spans, preserving
* all whitespace within the text elements of those spans.
/** Renders an FO `block` element and the specified nested spans,
* preserving all whitespace within the text elements of those spans.
*/
def blockWithWS(styleHint: Element, content: Seq[Span], attr: (String, String)*): String =
withoutIndentation(_.element("fo:block", styleHint, content, attr: _*))
Expand Down Expand Up @@ -385,7 +403,7 @@ object FOFormatter extends (RenderContext[FOFormatter] => FOFormatter) {
/** Creates a new formatter instance based on the specified render context.
*/
def apply(context: RenderContext[FOFormatter]): FOFormatter =
FOFormatter(
new FOFormatter(
context.renderChild,
context.root,
Nil,
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/laika/render/FOProperties.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package laika.render
*
* @author Jens Halm
*/
trait FOProperties {
private[laika] trait FOProperties {

private val border = Set(
"border",
Expand Down Expand Up @@ -338,7 +338,7 @@ trait FOProperties {

}

object FOProperties {
private[laika] object FOProperties {

/* https://www.w3.org/TR/xsl11/#prtab1 */
private val inherited = Set(
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/laika/render/FORenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import laika.rst.ast.{ Line, LineBlock }
*
* @author Jens Halm
*/
object FORenderer extends ((FOFormatter, Element) => String) {
private[laika] object FORenderer extends ((FOFormatter, Element) => String) {

private val formats: NonEmptySet[String] = NonEmptySet.of("pdf", "fo", "xslfo", "xsl-fo")

Expand Down
36 changes: 27 additions & 9 deletions core/shared/src/main/scala/laika/render/HTMLFormatter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package laika.render

import laika.ast._
import laika.ast.*
import laika.factory.RenderContext
import laika.rewrite.nav.PathTranslator

Expand All @@ -33,12 +33,12 @@ import laika.rewrite.nav.PathTranslator
*
* @author Jens Halm
*/
case class HTMLFormatter(
class HTMLFormatter private[render] (
renderChild: (HTMLFormatter, Element) => String,
currentElement: Element,
parents: List[Element],
pathTranslator: PathTranslator,
path: Path,
val pathTranslator: PathTranslator,
val path: Path,
indentation: Indentation,
messageFilter: MessageFilter,
closeEmptyTags: Boolean
Expand All @@ -49,15 +49,33 @@ case class HTMLFormatter(
messageFilter
) {

val emptyTagClosingChar: String = if (closeEmptyTags) "/" else ""
private val emptyTagClosingChar: String = if (closeEmptyTags) "/" else ""

type StyleHint = Options

protected def withChild(element: Element): HTMLFormatter =
copy(parents = currentElement :: parents, currentElement = element)
new HTMLFormatter(
renderChild,
element,
currentElement :: parents,
pathTranslator,
path,
indentation,
messageFilter,
closeEmptyTags
)

protected def withIndentation(newIndentation: Indentation): HTMLFormatter =
copy(indentation = newIndentation)
new HTMLFormatter(
renderChild,
currentElement,
parents,
pathTranslator,
path,
newIndentation,
messageFilter,
closeEmptyTags
)

def attributes(tag: String, styleHint: StyleHint, attrs: Seq[(String, String)]): String = {
val id = styleHint.id.map("id" -> _).toSeq
Expand All @@ -82,7 +100,7 @@ case class HTMLFormatter(
object HTMLFormatter extends (RenderContext[HTMLFormatter] => HTMLFormatter) {

def apply(context: RenderContext[HTMLFormatter]): HTMLFormatter =
HTMLFormatter(
new HTMLFormatter(
context.renderChild,
context.root,
Nil,
Expand All @@ -103,7 +121,7 @@ object HTMLFormatter extends (RenderContext[HTMLFormatter] => HTMLFormatter) {
object XHTMLFormatter extends (RenderContext[HTMLFormatter] => HTMLFormatter) {

def apply(context: RenderContext[HTMLFormatter]): HTMLFormatter =
HTMLFormatter(
new HTMLFormatter(
context.renderChild,
context.root,
Nil,
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/laika/render/HTMLRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import laika.ast._
*
* @author Jens Halm
*/
class HTMLRenderer(format: String)
private[laika] class HTMLRenderer(format: String)
extends ((HTMLFormatter, Element) => String) {

def apply(fmt: HTMLFormatter, element: Element): String = {
Expand Down Expand Up @@ -387,4 +387,4 @@ class HTMLRenderer(format: String)

}

object HTMLRenderer extends HTMLRenderer(format = "html")
private[laika] object HTMLRenderer extends HTMLRenderer(format = "html")
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/laika/render/TagFormatter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ abstract class TagFormatter[Rep <: BaseFormatter[Rep]](
type StyleHint

/** Renders the specified string on the same line,
* with all special XML/HTML characters converted to entities.
* with all special XML/HTML characters converted to entities.
*/
def text(str: String): String = TagFormatter.escape(str, newLine)

Expand Down Expand Up @@ -128,7 +128,7 @@ abstract class TagFormatter[Rep <: BaseFormatter[Rep]](

}

object TagFormatter {
private[laika] object TagFormatter {

/** Replaces all special XML/HTML characters with entities. */
def escape(str: String, newLine: String = "\n"): String = {
Expand Down
19 changes: 5 additions & 14 deletions core/shared/src/main/scala/laika/render/TextFormatter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import laika.factory.RenderContext
*
* @author Jens Halm
*/
case class TextFormatter(
class TextFormatter private[render] (
renderChild: (TextFormatter, Element) => String,
currentElement: Element,
parents: List[Element],
Expand All @@ -42,27 +42,18 @@ case class TextFormatter(
) {

protected def withChild(element: Element): TextFormatter =
copy(parents = currentElement :: parents, currentElement = element)
new TextFormatter(renderChild, element, currentElement :: parents, indentation)

protected def withIndentation(newIndentation: Indentation): TextFormatter =
copy(indentation = newIndentation)

}

/** Default factory for TextFormatters, based on a provided RenderContext.
*/
object TextFormatter extends (RenderContext[TextFormatter] => TextFormatter) {

def apply(context: RenderContext[TextFormatter]): TextFormatter =
TextFormatter(context.renderChild, context.root, Nil, context.indentation)
new TextFormatter(renderChild, currentElement, parents, newIndentation)

}

/** Default factory for ASTFormatters, based on a provided RenderContext.
*/
object ASTFormatter extends (RenderContext[TextFormatter] => TextFormatter) {
private[laika] object ASTFormatter extends (RenderContext[TextFormatter] => TextFormatter) {

def apply(context: RenderContext[TextFormatter]): TextFormatter =
TextFormatter(context.renderChild, context.root, Nil, Indentation.dotted)
new TextFormatter(context.renderChild, context.root, Nil, Indentation.dotted)

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object DefaultTemplatePath {

private val base: Path = Root / "default"

def forSuffix(suffix: String): Path = base.withSuffix(s"template.$suffix")
private[laika] def forSuffix(suffix: String): Path = base.withSuffix(s"template.$suffix")

def forHTML: Path = forSuffix("html")
def forEPUB: Path = forSuffix("epub.xhtml")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ case class ReferenceResolver(config: Config) {

}

/** Companion for constructing ReferenceResolvers for a particular
* target Document.
/** Companion for constructing ReferenceResolvers for a particular target Document.
*/
object ReferenceResolver {
private[laika] object ReferenceResolver {

object CursorKeys {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import scala.annotation.tailrec
*
* @author Jens Halm
*/
case class DocumentTargets(document: Document, slugBuilder: String => String) {
private[link] class DocumentTargets(document: Document, slugBuilder: String => String) {

/** Generates symbol identifiers.
* Contains a predefined list of ten symbols to generate.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import laika.config.{ ConfigEncoder, DefaultKey, LaikaKeys }

/** Registers Icon AST elements for use with the `@:icon` directive and the `IconReference` AST element.
*/
case class IconRegistry(icons: Map[String, Icon])
case class IconRegistry private (icons: Map[String, Icon])

object IconRegistry {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import scala.annotation.tailrec
*
* @author Jens Halm
*/
class LinkResolver(root: DocumentTreeRoot, slugBuilder: String => String)
private[laika] class LinkResolver(root: DocumentTreeRoot, slugBuilder: String => String)
extends RewriteRulesBuilder {

val targets = new TreeTargets(root, slugBuilder)
Expand Down
18 changes: 9 additions & 9 deletions core/shared/src/main/scala/laika/rewrite/link/Selector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import laika.ast.Path
* from both, the ids rendered in the final document
* and the ids used for display.
*/
sealed trait Selector {
private[link] sealed trait Selector {

/** Indicates whether this selector is applicable
* beyond the boundaries of a single document.
Expand All @@ -43,45 +43,45 @@ sealed trait Selector {

/** A selector that can be used for a sequence of targets.
*/
sealed trait SequenceSelector extends Selector {
private[link] sealed trait SequenceSelector extends Selector {
val global = false
val unique = false
}

/** A selector that can is a globally unique identifier.
*/
sealed trait UniqueSelector extends Selector {
private[link] sealed trait UniqueSelector extends Selector {
val global = true
val unique = true
def description: String
}

/** A selector for a rendered target in a document.
*/
case class TargetIdSelector(id: String) extends UniqueSelector {
private[link] case class TargetIdSelector(id: String) extends UniqueSelector {
val description = s"link target with id '$id'"
}

/** A selector for a definition for an internal or external link.
*/
case class LinkDefinitionSelector(id: String) extends UniqueSelector {
private[link] case class LinkDefinitionSelector(id: String) extends UniqueSelector {
val description = s"link definition with id '$id'"
}

/** A selector based on a path, optionally including a fragment component.
*/
case class PathSelector(path: Path) extends UniqueSelector {
private[link] case class PathSelector(path: Path) extends UniqueSelector {
val description = s"link target with path '$path'"
}

/** An anonymous selector (usually matched by position).
*/
case object AnonymousSelector extends SequenceSelector
private[link] case object AnonymousSelector extends SequenceSelector

/** An auto-number selector (usually matched by position).
*/
case object AutonumberSelector extends SequenceSelector
private[link] case object AutonumberSelector extends SequenceSelector

/** An auto-symbol selector (usually matched by position).
*/
case object AutosymbolSelector extends SequenceSelector
private[link] case object AutosymbolSelector extends SequenceSelector
Loading

0 comments on commit a5b143d

Please sign in to comment.