brashtag

package module
v0.0.0-...-23f8724 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2023 License: MIT Imports: 2 Imported by: 3

README

Brashtag

Brashtag is a notation for specifying trees. Purpose of the notation is to allow you to surround a piece of text with hints that programs can later exploit.

The notation

There are three kinds of nodes blobs, bags, codes.

  • Blobs contain text.
  • Bags contain blobs, codes and other bags. They can be tagged.
  • Codes also contain text. More on them later.

The following snippet constructs tree with a bag tagged "card" that contains two bags tagged "front" and "back". The bag tagged "front" contains a blob with text "This is front.". The bag tagged back contains a blob with text "This is back.".

#card{
   #front{
      This is front.
   } 
   #back{
      This is back.
   }
}

Any text is allowed inside tags and blobs except these four characters #, {, }, ` (tick). Because these characters are also used by the notation. If you want to put text with those characters in the tree you have to surround it by ` ticks. That creates a node of kind code. You need to surround the code node with more ticks than maximum number of consecutive ticks inside its text.

Following snippet constructs a code node with tag ` and func main() { fmt.Println("Hello") } text.

`
func main() { fmt.Println("Hello") }
`

And this snippet constructs a code node with tag `` and text Some `code` here. .

``
Some `code` here.
``
Examples Texts and Corresponding Trees
One
Hello, World!

Two
Hello, World!

#comment{
    A first program!
}

Three
Hello, World!

#comment{
    A first program!
}

Code to compute nth fibonacci number.

`
def fibs(n):
    if n == 0 or n == 1:
        return 1
    return fibs(n-1) + fibs(n-2)
`

Four
Hello, World!

#comment{
    A first program!
}

Code to compute nth fibonacci number.

`
def fibs(n):
    if n == 0 or n == 1:
        return 1
    return fibs(n-1) + fibs(n-2)
`

That is all.

Some Programs

These are quickly hacked together, unpolished programs. Making this kind of hacking easy is what this notation is invented for.

  • barkdown - converts brashtag to html.
  • carter - flashcards study program.
  • todot - outputs brashtag tree in dot format from a brashtag document.
TODO
  • Write tests.
  • Better error reporting.
  • Write tutorial.
  • Write docs.

Documentation

Overview

Package brashtag provides parser for parsing files written in brashtag notation.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bag

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

Bag has a tag and children.

func NewBag

func NewBag(tag string, kids ...Node) Bag

func (Bag) AddKids

func (b Bag) AddKids(kids ...Node)

func (Bag) Kids

func (b Bag) Kids() []Node

func (Bag) RemoveKid

func (b Bag) RemoveKid(i int)

func (Bag) SetTag

func (b Bag) SetTag(tag string)

func (Bag) String

func (b Bag) String() string

func (Bag) Tag

func (b Bag) Tag() string

type Blob

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

func NewBlob

func NewBlob(text string) Blob

func (Blob) SetText

func (b Blob) SetText(text string)

func (Blob) String

func (b Blob) String() string

func (Blob) Text

func (b Blob) Text() string

type Code

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

func NewCode

func NewCode(tag, text string) Code

func (Code) SetTag

func (c Code) SetTag(tag string)

func (Code) SetText

func (c Code) SetText(text string)

func (Code) String

func (c Code) String() string

func (Code) Tag

func (c Code) Tag() string

func (Code) Text

func (c Code) Text() string

type Node

type Node interface {
	fmt.Stringer
	// contains filtered or unexported methods
}

func Parse

func Parse(text string) (Node, error)

Parse parses the text and returns its brashtag tree.

Example
package main

import (
	"fmt"
	"strings"
)

func main() {

	text := strings.Join([]string{
		"#div{",
		"	#b{A very short story}",
		"	#p{Lorem and impsum met for a coffee.}",
		"	` print 'Hello' `",
		"}",
	}, "\n")

	tree, _ := Parse(text)
	fmt.Println(toHTML(tree))

	// Input:
	/*
		#div{
			#b{A very short story}
			#p{Lorem and impsum met for a coffee.}
			` print 'Hello' `
		}
	*/

}

func toHTML(r Node) string {
	s := ""
	switch x := r.(type) {
	case Blob:
		return x.Text()
	case Code:
		return fmt.Sprintf("<code>%s</code>", x.Text())
	case Bag:
		s := ""
		for _, kid := range x.Kids() {
			s += toHTML(kid)
		}
		if x.Tag() != "" {
			return fmt.Sprintf("<%s>%s</%s>", x.Tag(), s, x.Tag())
		}
		return s
	}
	return s
}
Output:

<div>
	<b>A very short story</b>
	<p>Lorem and impsum met for a coffee.</p>
	<code> print 'Hello' </code>
</div>

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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