goat

package module
v0.0.0-...-8dbeb1a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2020 License: MIT Imports: 16 Imported by: 0

README

GoAT: Go ASCII Tool

This is a Go implementation of markdeep.mini.js's ASCII diagram generation.

Usage

$ go get github.com/blampe/goat
$ cat my-cool-diagram.txt | goat >  my-cool-diagram.svg

By default, the program reads from stdin, unless -i infile is given. By default, the program writes to stdout, unless -o outfile is given or a binary format with -f is selected. By default, it writes in SVG format, unless another format is specified with -f.

TODO

  • Dashed lines signaled by : or =.
  • Bold lines signaled by ???.

Examples

Here are some SVGs and the ASCII input they were generated from:

Trees

Trees Example

          .               .                .               .--- 1          .-- 1     / 1
         / \              |                |           .---+            .-+         +
        /   \         .---+---.         .--+--.        |   '--- 2      |   '-- 2   / \ 2
       +     +        |       |        |       |    ---+            ---+          +
      / \   / \     .-+-.   .-+-.     .+.     .+.      |   .--- 3      |   .-- 3   \ / 3
     /   \ /   \    |   |   |   |    |   |   |   |     '---+            '-+         +
     1   2 3   4    1   2   3   4    1   2   3   4         '--- 4          '-- 4     \ 4
Overlaps

Overlaps Example

           .-.           .-.           .-.           .-.           .-.           .-.
          |   |         |   |         |   |         |   |         |   |         |   |
       .---------.   .--+---+--.   .--+---+--.   .--|   |--.   .--+   +--.   .------|--.
      |           | |           | |   |   |   | |   |   |   | |           | |   |   |   |
       '---------'   '--+---+--'   '--+---+--'   '--|   |--'   '--+   +--'   '--|------'
          |   |         |   |         |   |         |   |         |   |         |   |
           '-'           '-'           '-'           '-'           '-'           '-'
Line Decorations

Line Decorations Example

                ________                            o        *          *   .--------------.
   *---+--.    |        |     o   o      |         ^          \        /   |  .----------.  |
       |   |    '--*   -+-    |   |      v        /            \      /    | |  <------.  | |
       |    '----->       .---(---'  --->*<---   /      .+->*<--o----'     | |          | | |
   <--'  ^  ^             |   |                 |      | |  ^    \         |  '--------'  | |
          \/        *-----'   o     |<----->|   '-----'  |__|     v         '------------'  |
          /\                                                               *---------------'
Line Ends

Line Ends Example

   o--o    *--o     /  /   *  o  o o o o   * * * *   o o o o   * * * *      o o o o   * * * *
   o--*    *--*    v  v   ^  ^   | | | |   | | | |    \ \ \ \   \ \ \ \    / / / /   / / / /
   o-->    *-->   *  o   /  /    o * v '   o * v '     o * v \   o * v \  o * v /   o * v /
   o---    *---
                                 ^ ^ ^ ^   . . . .   ^ ^ ^ ^   \ \ \ \      ^ ^ ^ ^   / / / /
   |  |   *  o  \  \   *  o      | | | |   | | | |    \ \ \ \   \ \ \ \    / / / /   / / / /
   v  v   ^  ^   v  v   ^  ^     o * v '   o * v '     o * v \   o * v \  o * v /   o * v /
   *  o   |  |    *  o   \  \

   <--o   <--*   <-->   <---      ---o   ---*   --->   ----      *<--   o<--   -->o   -->*
Dot Grids

Dot Grids Example

  o o o o o  * * * * *  * * o o *    o o o      * * *      o o o     · * · · ·     · · ·
  o o o o o  * * * * *  o o o o *   o o o o    * * * *    * o * *    · * * · ·    · · · ·
  o o o o o  * * * * *  o * o o o  o o o o o  * * * * *  o o o o o   · o · · o   · · * * ·
  o o o o o  * * * * *  o * o o o   o o o o    * * * *    o * o o    · · · · o    · · * ·
  o o o o o  * * * * *  * * * * o    o o o      * * *      o * o     · · · · ·     · · *
Large Nodes

Large Node Example

   .---.       .-.        .-.       .-.                                       .-.
   | A +----->| 1 +<---->| 2 |<----+ 4 +------------------.                  | 8 |
   '---'       '-'        '+'       '-'                    |                  '-'
                           |         ^                     |                   ^
                           v         |                     v                   |
                          .-.      .-+-.        .-.      .-+-.      .-.       .+.       .---.
                         | 3 +---->| B |<----->| 5 +---->| C +---->| 6 +---->| 7 |<---->| D |
                          '-'      '---'        '-'      '---'      '-'       '-'       '---'
Small Grids

Small Grids Example

       ___     ___      .---+---+---+---+---.     .---+---+---+---.  .---.   .---.
   ___/   \___/   \     |   |   |   |   |   |    / \ / \ / \ / \ /   |   +---+   |
  /   \___/   \___/     +---+---+---+---+---+   +---+---+---+---+    +---+   +---+
  \___/ b \___/   \     |   |   | b |   |   |    \ / \a/ \b/ \ / \   |   +---+   |
  / a \___/   \___/     +---+---+---+---+---+     +---+---+---+---+  +---+ b +---+
  \___/   \___/   \     |   | a |   |   |   |    / \ / \ / \ / \ /   | a +---+   |
      \___/   \___/     '---+---+---+---+---'   '---+---+---+---'    '---'   '---'
Big Grids

Big Grids Example

    .----.        .----.
   /      \      /      \            .-----+-----+-----.
  +        +----+        +----.      |     |     |     |          .-----+-----+-----+-----+
   \      /      \      /      \     |     |     |     |         /     /     /     /     /
    +----+   B    +----+        +    +-----+-----+-----+        +-----+-----+-----+-----+
   /      \      /      \      /     |     |     |     |       /     /     /     /     /
  +   A    +----+        +----+      |     |  B  |     |      +-----+-----+-----+-----+
   \      /      \      /      \     +-----+-----+-----+     /     /  A  /  B  /     /
    '----+        +----+        +    |     |     |     |    +-----+-----+-----+-----+
          \      /      \      /     |  A  |     |     |   /     /     /     /     /
           '----'        '----'      '-----+-----+-----'  '-----+-----+-----+-----+
Complicated

Complicated Example

+-------------------+                           ^                      .---.
|    A Box          |__.--.__    __.-->         |      .-.             |   |
|                   |        '--'               v     | * |<---        |   |
+-------------------+                                  '-'             |   |
                       Round                                       *---(-. |
  .-----------------.  .-------.    .----------.         .-------.     | | |
 |   Mixed Rounded  | |         |  / Diagonals  \        |   |   |     | | |
 | & Square Corners |  '--. .--'  /              \       |---+---|     '-)-'       .--------.
 '--+------------+-'  .--. |     '-------+--------'      |   |   |       |        / Search /
    |            |   |    | '---.        |               '-------'       |       '-+------'
    |<---------->|   |    |      |       v                Interior                 |     ^
    '           <---'      '----'   .-----------.              ---.     .---       v     |
 .------------------.  Diag line    | .-------. +---.              \   /           .     |
 |   if (a > b)     +---.      .--->| |       | |    | Curved line  \ /           / \    |
 |   obj->fcn()     |    \    /     | '-------' |<--'                +           /   \   |
 '------------------'     '--'      '--+--------'      .--. .--.     |  .-.     +Done?+-'
    .---+-----.                        |   ^           |\ | | /|  .--+ |   |     \   /
    |   |     | Join        \|/        |   | Curved    | \| |/ | |    \    |      \ /
    |   |     +---->  o    --o--        '-'  Vertical  '--' '--'  '--  '--'        +  .---.
 <--+---+-----'       |     /|\                                                    |  | 3 |
                      v                             not:line    'quotes'        .-'   '---'
  .-.             .---+--------.            /            A || B   *bold*       |        ^
 |   |           |   Not a dot  |      <---+---<--    A dash--is not a line    v        |
  '-'             '---------+--'          /           Nor/is this.            ---

More examples are available here.

Documentation

Index

Constants

View Source
const (
	// FIXME what is the right scale?
	PngScale = 2
)

Variables

This section is empty.

Functions

func AsciiToPng

func AsciiToPng(canvas Canvas, out io.Writer) error

func AsciiToSvg

func AsciiToSvg(canvas Canvas, out io.Writer) error

func RenderAscii

func RenderAscii(canvas Canvas, gc draw2d.GraphicContext)

Types

type Bridge

type Bridge struct {
	// contains filtered or unexported fields
}

Bridge correspondes to combinations of "-)-" or "-(-" and is displayed as the vertical line "hopping over" the horizontal.

func (Bridge) Draw

func (b Bridge) Draw(gc draw2d.GraphicContext)

Draw a bridge as an SVG elliptical arc element.

type Canvas

type Canvas struct {
	Width  int
	Height int
	// contains filtered or unexported fields
}

Canvas represents a 2D ASCII rectangle.

func NewCanvas

func NewCanvas(in io.Reader) Canvas

NewCanvas creates a new canvas with contents read from the given io.Reader. Content should be newline delimited.

func (*Canvas) Bridges

func (c *Canvas) Bridges() []Drawable

Bridges returns a slice of all bridges, "-)-" or "-(-".

func (*Canvas) Circles

func (c *Canvas) Circles() []Circle

Circles returns a slice of all 'o' and '*' characters not considered text.

func (*Canvas) HalfSteps

func (c *Canvas) HalfSteps() []Line

func (*Canvas) Lines

func (c *Canvas) Lines() []Line

Lines returns a slice of all Line drawables that we can detect -- in all possible orientations.

func (*Canvas) RoundedCorners

func (c *Canvas) RoundedCorners() []RoundedCorner

RoundedCorners returns a slice of all curvy corners in the diagram.

func (*Canvas) String

func (c *Canvas) String() string

func (*Canvas) Text

func (c *Canvas) Text() []Drawable

Text returns a slice of all text characters not belonging to part of the diagram. How these characters are identified is rather complicated.

func (*Canvas) Triangles

func (c *Canvas) Triangles() []Drawable

Triangles returns a slice of all detectable Triangles.

type Circle

type Circle struct {
	// contains filtered or unexported fields
}

Circle corresponds to "o" or "*" runes in the absence of surrounding alphanumerics.

func (*Circle) Draw

func (c *Circle) Draw(gc draw2d.GraphicContext)

Draw a solid circle as an SVG circle element.

type Drawable

type Drawable interface {
	Draw(gc draw2d.GraphicContext)
}

Drawable represents anything that can Draw itself.

type FixDraw2DEscapeXml

type FixDraw2DEscapeXml struct {
	draw2d.GraphicContext
}

FIXME this probably needs to be fixed in draw2d when drawing text, XML is not escaped and generated SVG is invalid if contains < > & We wrap the GC only for the SVG case to escape the text here

func (*FixDraw2DEscapeXml) FillStringAt

func (gc *FixDraw2DEscapeXml) FillStringAt(text string, x, y float64) (width float64)

type Index

type Index struct {
	// contains filtered or unexported fields
}

Index represents a position within an ASCII diagram.

type Line

type Line struct {
	// contains filtered or unexported fields
}

Line represents a straight segment between two points.

func (Line) Draw

func (l Line) Draw(gc draw2d.GraphicContext)

Draw a straight line as an SVG path.

type Orientation

type Orientation int

Orientation represents the primary direction that a Drawable is facing.

const (
	NONE Orientation = iota // No orientation; no structure present.
	N                       // North
	NE                      // Northeast
	NW                      // Northwest
	S                       // South
	SE                      // Southeast
	SW                      // Southwest
	E                       // East
	W                       // West
)

type Pixel

type Pixel Index

Pixel represents the on-screen coordinates for an Index.

type RoundedCorner

type RoundedCorner struct {
	// contains filtered or unexported fields
}

RoundedCorner corresponds to combinations of "-." or "-'".

func (*RoundedCorner) Draw

func (c *RoundedCorner) Draw(gc draw2d.GraphicContext)

Draw a rounded corner as an SVG elliptical arc element.

type Text

type Text struct {
	// contains filtered or unexported fields
}

Text corresponds to any runes not reserved for diagrams, or reserved runes surrounded by alphanumerics.

func (Text) Draw

func (t Text) Draw(gc draw2d.GraphicContext)

Draw a single text character as an SVG text element.

type Triangle

type Triangle struct {
	// contains filtered or unexported fields
}

Triangle corresponds to "^", "v", "<" and ">" runes in the absence of surrounding alphanumerics.

func (Triangle) Draw

func (t Triangle) Draw(gc draw2d.GraphicContext)

Draw a solid triable as an SVG polygon element.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL