Skip to content

Commit

Permalink
shrink public API surface in laika.render.*
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshalm committed Jul 14, 2023
1 parent a591be5 commit f2ffad0
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 44 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)

}

0 comments on commit f2ffad0

Please sign in to comment.