laika.parse.rst

TextRoles

object TextRoles

API for creating interpreted text roles, the extension mechanism for inline elements of reStructuredText. The API did not aim to mimic the API of the original Python reference implementation. Instead the goal was to create an API that is idiomatic Scala, fully typesafe and as concise as possible. Yet it should be flexible enough to semantically support the options of the Python text roles, so that ideally most existing Python text roles could theoretically get ported to Laika.

Entry point for creating a new role is the TextRole object. It allows to specify the following aspects that define a text role:

A role directive may consist of any combination of fields and body elements:

.. role:: ticket(link)
:base-url: http://www.company.com/tickets/

In the example above ticket is the name of the customized role, link the name of the base role and base-url the value that overrides the default defined in the base role. For the specification details on role directives see http://docutils.sourceforge.net/docs/ref/rst/directives.html#custom-interpreted-text-roles.

Before such a role directive can be used, an implementation has to be provided for the base role with the name link. For more details on implementing directives see laika.parse.rst.Directives.

The implementation of the link text role could look like this:

val rst = ReStructuredText withTextRoles (
  TextRole("link", "http://www.company.com/main/")(field("base-url")) {
    (base, text) => Link(List(Text(text)), base + text)
  }
)

Transform from rst to HTML fromFile "hello.rst" toFile "hello.html"

We specify the name of the role to be link, and the default value the URL provided as the second argument. The second parameter list specifies the role directive implementation, in this case only consisting of a call to field("base-url") which specifies a required field of type String (since no conversion function was supplied). The type of the result of the directive has to match the type of the default value. Finally the role function is defined that accepts two arguments. The first is the base url, either the default in case the base role is used directly, or the value specified with the base-url field in a customized role. The second is the actual text from the interpreted text span. Finally the directive gets registered with the ReStructuredText parser.

If you need to define more fields or body content they can be added with the ~ combinator just like with normal directives. Likewise you can specify validators and converters for fields and body values like documented in laika.parse.rst.Directives.

Our example role can then be used in the following ways:

Using the base role directly:

For details read our :link:`documentation`.

This would result in the following HTML:

For details read our <a href="http://www.company.com/main/documentation">documentation</a>.

Using the customized role called ticket:

For details see ticket :ticket:`344`.

This would result in the following HTML:

For details see ticket <a href="http://www.company.com/ticket/344">344</a>.
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. TextRoles
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Type Members

  1. trait RoleDirectiveParser extends AnyRef

    API to implement by the actual role directive parser.

  2. abstract class RoleDirectivePart[+A] extends (RoleDirectiveParser) ⇒ Result[A]

    Represents a single part (field or body) of a directive.

  3. class TextRole extends AnyRef

    Represents a single text role implementation.

Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. implicit object CanBuildRoleDirectivePart extends CanBuild[RoleDirectivePart]

    Type class required for using the generic Builders API with directives.

  7. object Parts

    The public user API for specifying the required and optional parts of a directive (fields or body) together with optional converter/validator functions.

  8. object TextRole

    API entry point for setting up a text role that.

  9. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  10. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  11. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  12. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  13. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  14. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  15. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  16. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  17. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  18. final def notify(): Unit

    Definition Classes
    AnyRef
  19. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  20. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  21. def toString(): String

    Definition Classes
    AnyRef → Any
  22. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  23. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  24. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped