svgbob

repository·master·Indexed 26 days ago

https://github.com/ivanceras/svgbob

A tool for converting text-based ASCII diagrams into high-quality SVG images. It features a multi-stage conversion pipeline (StringBuffer, CellBuffer, PropertyBuffer, FragmentBuffer, Spans, Endorsement, and SVG Generation) and is available as a CLI tool and a REST API via the svgbob_server. It supports custom CSS styling through tagging patterns and is used by platforms such as Kroki, Asciidoctor, and Google's Comprehensive Rust documentation.

Tokens
5.9K
Snippets
5
Records
64
Agent score
88%

What's inside svgbob

  1. Overview of Svgbob

    master
    Svgbob is a tool that creates graphical representations of text-based diagrams. It provides a CLI that accepts text input and generates an SVG image as output. It is used by various platforms including Kroki, Asciidoctor, and Google's Comprehensive Rust documentation.
  2. Understand the Svgbob conversion pipeline

    master

    Svgbob converts ASCII art diagrams into SVG drawings through a multi-stage pipeline:

    1. StringBuffer: Input strings are sliced into rows and columns.
    2. CellBuffer: Identifies which characters occupy which cells.
    3. PropertyBuffer: Determines the properties of each cell based on its character.
    4. FragmentBuffer: Converts characters into small drawing fragments (lines, arcs) based on their 8 neighbors.
    5. Spans & Contact Groups: Groups neighboring spans and touching fragments to optimize processing.
    6. Endorsement: Merges small fragments into high-level shapes like rect, rounded_rect, circle, and arc.
    7. SVG Generation: Produces the final SVG document tree.
  3. Render Vertical Lines with `|`

    master

    The | (pipe) character is used for vertical lines:

    • As Text: If adjacent to an alphanumeric character, it is rendered as text (e.g., a||b).
    • As a Line: If adjacent to another drawing character or if used alone, it is rendered as a vertical line.
  4. Render Intersections with `+`

    master

    The + (cross) character creates intersections by connecting to adjacent lines:

    • If the left side is a horizontal line, it connects midway to the left.
    • If the right side is a horizontal line, it connects midway to the right.
    • If the top is a vertical line, it connects midway to the top.
    • If the bottom is a vertical line, it connects midway to the bottom.
  5. Tag SVG shapes using ASCII patterns

    master

    You can apply CSS styles to specific high-level shapes (like rectangles or circles) by using a tagging pattern within your ASCII art.

    Wrap text inside a shape using the pattern {<ident>}. This <ident> will be applied as the class attribute to the corresponding SVG DOM element (e.g., <rect class="your-tag">).

    Example usage in ASCII:

    {my-style}
    +---------+
    |  Shape  |
    +---------+
  6. Render Slanted Lines with `/` and `\`

    master

    Forward Slash /

    • As Text: If adjacent to an alphanumeric character, it is rendered as text (e.g., folder/).
    • As a Line: If at least one of its 8 neighbors (top, bottom, left, right, or diagonals) is a drawing character, it is rendered as a line slanted to the right.
    • Precedence: If used as text but next to a drawing element, the drawing element rendering takes precedence.

    Backward Slash \

    • As Text: If adjacent to an alphanumeric character, it is rendered as text (e.g., C:\users).
    • As a Line: If it connects to a drawing element or is used alone, it is rendered as a line slanted to the left.
  7. Render Horizontal Lines with `-` or `_`

    master

    The characters - (dash/hyphen) and _ (underscore) are used for horizontal lines based on their context:

    • As Text: If adjacent to an alphanumeric character, it is rendered as text.
    • As a Line: If adjacent to another drawing character or if used alone, it is rendered as a horizontal line.

    Example (Drawing vs Text):

    +---------------+--------
    |"-"    | -     |
    +-------+-------+--------
    |--""   | --    |
    +-------+-------+--------
    |"----" | ----  |
    +-------+-------+--------
  8. Use FragmentBuffer for SVG to ASCII conversion

    master

    The FragmentBuffer struct is a central component in the svgbob conversion pipeline. It manages drawing fragments for each cell, acting as an intermediate step between SVG input and ASCII output.

    Conversion flow:

    • SVG to ASCII: SVG $\rightarrow$ FragmentBuffer $\rightarrow$ StringBuffer $\rightarrow$ Ascii Diagrams
    • ASCII to SVG: Ascii Diagrams $\rightarrow$ StringBuffer $\rightarrow$ FragmentBuffer $\rightarrow$ SVG

    It stores FragmentSpan objects mapped to specific Cell coordinates.