kramdown Documentation

repository·master·Indexed 23 days ago

https://github.com/gettalong/kramdown

A fast, pure Ruby Markdown superset converter that uses an internal AST to support multiple input formats (kramdown, Markdown, GFM, HTML) and output formats (HTML, kramdown, LaTeX, PDF). It provides a Ruby API via the Kramdown::Document class, a command-line interface for file conversion, and a customizable converter system allowing developers to implement custom output formats by inheriting from Kramdown::Converter::Base.

Tokens
6.1K
Snippets
4
Records
41
Agent score
82%

What's inside kramdown

  1. Overview of kramdown supported input and output formats

    master

    kramdown is a fast, pure Ruby Markdown superset converter that uses an internal AST (Abstract Syntax Tree) to support multiple formats.

    Supported Input Formats:

    • kramdown (a Markdown superset)
    • Markdown
    • GFM (GitHub Flavored Markdown)
    • HTML

    Supported Output Formats:

    • HTML
    • kramdown
    • LaTeX (which can be used to generate PDF)
    • PDF (via Prawn)
  2. Convert Markdown to HTML with kramdown

    master

    kramdown provides a simple Ruby API for converting text into various formats. To convert Markdown text to HTML, require the kramdown library and use the Kramdown::Document.new method followed by .to_html.

    require 'kramdown'
    
    Kramdown::Document.new(text).to_html
  3. Use Inline Attribute Lists (IAL) for elements

    master

    Kramdown supports Inline Attribute Lists (IAL) to attach attributes like id, class, or custom key-value pairs to inline elements (like spans).

    An IAL is defined using the {: syntax.

    • IDs: Use # followed by the ID name (e.g., {#my-id}).
    • Classes: Use . followed by the class name (e.g., {.my-class}).
    • Key-Value Pairs: Use key="value" or key='value' (e.g., {title="Hello"}).
    • References: You can also include references within the list.

    Example syntax for an inline attribute list: {:.my-class #my-id key="value"}

  4. Configure LaTeX math and abbreviation packages

    master

    The Kramdown::Converter::Latex automatically tracks and adds required LaTeX packages to the @data[:packages] set based on the elements encountered during conversion:

    • Math: When math elements are processed, the following packages are added: amssymb, amsmath, amsthm, and amsfonts.
    • Abbreviations: When abbreviation elements are processed, the acronym package is added.
    • Images: When images are processed, the graphicx package is added.
    • Tables: When tables are processed, the longtable package is added.
    • Footnotes: When footnotes are processed, the fancyvrb package is added.
    • Minted Syntax Highlighting: If the :syntax_highlighter option is set to :minted, the minted package is added.
  5. How to implement a custom Kramdown converter

    master

    To create a new output format (like a custom HTML flavor or a different markup language), you must derive a new class from Kramdown::Converter::Base.

    Implementation Steps:

    1. Inherit: Create a class that inherits from Kramdown::Converter::Base.
    2. Namespace: Place your class within the Kramdown::Converter module to ensure auto-detection works correctly.
    3. Implement #convert: You must implement the convert(el) method. This method receives an element (el) and must return the converted string/object for that element.

    While you can transform the document tree in any way, the standard approach used by the built-in Html and Latex converters is to write one method per element type to handle the transformation logic.

    Usage:

    Converter objects are intended to be transient "throw-away" objects used to store state during a single conversion pass. You should not instantiate them directly; instead, use the Kramdown::Converter::Base.convert class method.

  6. Customize the HTML converter by sub-classing `Kramdown::Converter::Html`

    master

    You can extend the default HTML output behavior by creating a subclass of Kramdown::Converter::Html. To change how specific Markdown elements are rendered, override the corresponding convert_NAME methods.

    Each overridden method must accept the following parameters:

    • el: The element of type NAME to be converted.
    • indent: An integer representing the current amount of spaces for indentation (primarily used for block-level elements).

    The method must return a String containing the element el formatted as HTML.

    Example of overriding a method:

    class MyCustomConverter < Kramdown::Converter::Html
      def convert_p(el, indent)
        # Custom paragraph logic here
        "<div class='custom-p'>#{inner(el, indent)}</div>\n"
      end
    end
  7. Supported and unsupported elements in Man converter

    master

    When converting to manpage format, the following behaviors apply to specific Markdown elements:

    Supported Elements:

    • Headers: Supports up to level 3. Level 1 is used for the .TH (Title Header) and NAME sections. Level 2 uses .SH (Section Header), and Level 3 uses .SS (Sub-section Header).
    • Code Blocks: Converted to groff EX (Example) blocks.
    • Blockquotes: Converted to groff RS (Region Start) and RE (Region End) blocks.
    • Lists (UL, OL, DL): Supported. Unordered lists use the \(bu bullet symbol.
    • Tables: Supported using groff TS (Table Start) syntax. Alignment can be set via left, center, or right.
    • Links: Standard URLs are converted using .UR and .UE macros. mailto: links are converted using .MT and .UE macros.
    • Text Styles: em (italics) uses \fI, strong (bold) uses \fB, and codespan uses \fB.

    Unsupported Elements (will trigger warnings):

    • HTML elements
    • Images
    • Footnotes
    • Raw content
    • Table cells containing links
  8. Customize the LaTeX converter by sub-classing

    master

    You can extend the functionality of the LaTeX output converter by creating a subclass of Kramdown::Converter::Latex and overriding specific convert_NAME methods.

    Each overridden method must follow this signature:

    • convert_NAME(el, opts)

    Parameters:

    • el: The element of type NAME to be converted.
    • opts: A hash containing processing options. The key :parent is always present and contains the parent element.

    The method must return a string containing the element formatted as valid LaTeX markup.

  9. How link references and footnotes are generated

    master

    The converter automatically manages link references and footnotes to produce clean markdown.

    1. Link References: When convert_a encounters a link that is an external URL (starting with http or ftp) or contains parentheses, it adds the element to @linkrefs and uses the reference syntax [text][index]. At the end of the conversion process, create_link_defs generates the corresponding [index]: url definitions.
    2. Footnotes: When convert_footnote is called, it adds the footnote content to @footnotes. The create_footnote_defs method then generates the [^name]: definitions at the end of the document.
    3. Abbreviations: If abbrev_defs are provided in the root options, create_abbrev_defs generates the *[name]: text definitions.
  10. How to implement a custom Kramdown parser

    master

    To implement a new parser in Kramdown, you must derive a new class from Kramdown::Parser::Base and place it within the Kramdown::Parser module. This placement is required for Kramdown's auto-detection mechanism to work.

    Your subclass must implement the parse method, which contains the actual parsing logic. The parse method is responsible for parsing the content provided in the @source instance variable and building an element tree that is stored in the @root instance variable.

    Note that you should not instantiate the parser directly using .new. Instead, use the Base.parse class method, which handles instantiation and execution for you.

  11. Use the kramdown CLI to convert files

    master

    The kramdown command-line tool converts Markdown (or other input formats) into various output formats like HTML, LaTeX, or Man pages. It can process one or multiple files provided as arguments. By default, it reads from standard input or files provided via ARGF and outputs to standard output.

    Basic Usage:

    kramdown [options] [FILE FILE ...]