Packages

o

laika.rst.ext

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.

Implementing a Directive

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

  • The name with which it can be referred to by both, a span of interpreted text and a role directive to further customize it.
  • The default value, that should get passed to the role function in case it is used directly in interpreted text without customization through a role directive.
  • The role directive that specifies how the role can be customized. The options for role directives are almost identical to regular directives, the only difference being that role directives do not support arguments, only fields and body elements.
  • The actual role function. It gets invoked for each occurrence of interpreted text that refers to this role, either directly by name or to the name of a role directive that customized this role. The first argument is either the default value or the result of the role directive, the second is the actual text of the interpreted text span. The return value of the role function is the actual Span instance that the original interpreted text should be replaced with.

Basic Example

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.rst.ext.Directives.

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

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

object MyDirectives extends RstExtensionRegistry {
  val textRoles = Seq(textRole)
  val spanDirectives = Seq()
  val blockDirectives = Seq()
}

val transformer = Transformer
  .from(ReStructuredText)
  .to(HTML)
  .using(MyDirectives)
  .build

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.rst.ext.Directives.

Using the Text Role in Markup

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
Visibility
  1. Public
  2. All

Type Members

  1. trait RoleDirectiveParserBuilder extends AnyRef

    API to implement by the actual directive parser.

    API to implement by the actual directive parser.

    This allows directive parts to specify the expected elements within the parsed directive. In contrast to Laika's directive syntax which allows to have a single directive parser for any kind of directive implementation, the one for ReStructuredText has a separate parser for each directive depending on its configuration.

    The API for text roles is a subset of the API for block and span directives.

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

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

  3. abstract class RoleDirectivePartBuilder[+A] extends (RoleDirectiveParserBuilder) ⇒ (RoleDirectiveParserBuilder, RoleDirectivePart[A])

    Represents a single part (argument, field or body) of a text role.

  4. class TextRole extends RstExtension[RoleDirectivePartBuilder[(String) ⇒ Span]]

    Represents a single text role implementation.

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  6. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  8. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  9. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  10. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  11. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  12. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  13. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  14. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  15. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  16. def toString(): String
    Definition Classes
    AnyRef → Any
  17. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  18. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  19. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... ) @native()
  20. 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.

  21. object TextRole

    API entry point for setting up a text role that.

Inherited from AnyRef

Inherited from Any

Ungrouped