crst

Pipeline Status

A reStructuredText (RST) parser for Crystal that converts RST markup to HTML, XML (DocBook), and plain text.

Documentation

API documentation is available at: https://renich.gitlab.io/crst/

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      crst:
        gitlab: renich/crst
  2. Run shards install

Usage

Basic Usage

require "crst"

# Parse RST to AST
doc = Crst.parse("Hello World\n===========\n\nThis is a paragraph.")

# Convert to HTML
html = Crst.to_html("Hello World\n===========\n\nThis is a paragraph.")
puts html  # <div><h1>Hello World</h1><p>This is a paragraph.</p></div>

# Convert to XML (DocBook)
xml = Crst.to_xml("Hello World\n===========\n\nThis is a paragraph.")

# Convert to plain text
text = Crst.to_text("Hello World\n===========\n\nThis is a paragraph.")

Advanced Usage with Configuration

require "crst"

# Create custom configuration
config = Crst::Config.new
config.roles["highlight"] = "bg-yellow"  # Custom role styling

# Register custom directive
config.custom_directives["mydir"] = ->(name : String, args : Array(String), opts : Hash(String, String), children : Array(Crst::Node), cfg : Crst::Config) {
  # Custom directive implementation
  [Crst::Paragraph.new("Custom: #{args.join(' ')}")]
}

# Parse with configuration
rst_content = <<-RST
Custom Role
===========

This text has :highlight:`highlighted content`.

.. mydir:: argument1 argument2

   Custom directive content.
RST

html = Crst.to_html(rst_content, config)

API Reference

Core Methods

Configuration

The Crst::Config class allows customization:

config = Crst::Config.new
config.tab_width = 4                    # Tab width for indentation
config.language = "crystal"             # Default language for code blocks
config.roles["custom"] = "css-class"    # Custom role definitions
config.custom_directives["name"] = proc # Custom directive handlers

Examples

Basic RST Document

My Document Title
=================

Introduction paragraph with *emphasis* and **strong** text.

Section Header
--------------

* Bullet list item 1
* Bullet list item 2

#. Numbered list item 1
#. Numbered list item 2

Code Example::

    def hello
      puts "Hello, World!"
    end

.. note::

   This is an admonition note.

Advanced Features

Hyperlinks and References:

Link to `external site <https://example.com>`_.
Reference to `internal target`_.

.. _internal target: https://internal.com

Tables:

+-------+-------+-------+
| Header| Header| Header|
+=======+=======+=======+
| Cell  | Cell  | Cell  |
+-------+-------+-------+

Footnotes and Citations:

Text with footnote [1]_ and citation [Smith2023]_.

.. [1] Footnote content here.
.. [Smith2023] Smith, J. (2023). Title of work.

Custom Roles and Directives:

Text with :custom:`custom role`.

.. figure:: diagram.png

   Figure caption text.

See docs/examples/ for comprehensive examples demonstrating various RST features.

API Documentation

Generated API documentation is available in the docs/technical/api/ directory after running make docs.

Development

Setup

To set up pre-push hooks for automatic testing and linting:

cp hooks/pre-push .git/hooks/
chmod +x .git/hooks/pre-push

This ensures code quality before pushing.

Supported RST Features

Structure

Lists

Inline Markup

Blocks

Tables

References

Directives

Roles

Output Formats

Advanced Features

Contributing

Please read CONTRIBUTING.rst for details on our code of conduct, and the process for submitting pull requests to us.

Contributors