svgbob
repository·master·Indexed 26 days ago
https://github.com/ivanceras/svgbobA 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.
What's inside svgbob
- 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.
Understand the Svgbob conversion pipeline
masterSvgbob converts ASCII art diagrams into SVG drawings through a multi-stage pipeline:
- StringBuffer: Input strings are sliced into rows and columns.
- CellBuffer: Identifies which characters occupy which cells.
- PropertyBuffer: Determines the properties of each cell based on its character.
- FragmentBuffer: Converts characters into small drawing fragments (lines, arcs) based on their 8 neighbors.
- Spans & Contact Groups: Groups neighboring spans and touching fragments to optimize processing.
- Endorsement: Merges small fragments into high-level shapes like
rect,rounded_rect,circle, andarc. - SVG Generation: Produces the final SVG document tree.
Render Vertical Lines with `|`
masterThe
|(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.
- As Text: If adjacent to an alphanumeric character, it is rendered as text (e.g.,
Install and run the svgbob_server
masterYou can runsvgbobas a REST API by installing thesvgbob_servercrate and running the binary. You can specify the port using thePORTenvironment variable.Render Intersections with `+`
masterThe
+(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.
Tag SVG shapes using ASCII patterns
masterYou 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 theclassattribute to the corresponding SVG DOM element (e.g.,<rect class="your-tag">).Example usage in ASCII:
{my-style} +---------+ | Shape | +---------+Render Slanted Lines with `/` and `\`
masterForward 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.
- As Text: If adjacent to an alphanumeric character, it is rendered as text (e.g.,
Create Rounded Corners with `.` and `,`
masterThe primary purpose of the dot.and comma,characters is to create rounded corners, specifically for the top-left and top-right corners of a shape.Render Horizontal Lines with `-` or `_`
masterThe 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):
+---------------+-------- |"-" | - | +-------+-------+-------- |--"" | -- | +-------+-------+-------- |"----" | ---- | +-------+-------+--------Use FragmentBuffer for SVG to ASCII conversion
masterThe
FragmentBufferstruct is a central component in thesvgbobconversion 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
FragmentSpanobjects mapped to specificCellcoordinates.- SVG to ASCII:
Convert text diagrams to SVG via the svgbob_server API
masterOnce the server is running, you can convert ASCII/text diagrams into SVG files by sending a POST request to the server. Use the
asciiform field to provide the diagram text.curl -X POST -F 'ascii=o------>' http://localhost:3000 > output.svgConfigure the svgbob server port
masterThesvgbob_serverlistens on all interfaces (0.0.0.0). You can specify the port to use by setting thePORTenvironment variable. IfPORTis not set or is invalid, the server defaults to port3000.