Help us shape the future of Go! Take the 2026 Go Developer Survey

pangaea

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2014 License: MIT Imports: 10 Imported by: 0

README

pangaea

Powerful pre-processor for text files - powered by JavaScript.

Pangaea works on any kind of text file including HTML pages, source code files, documents, etc. The easy-to-use command line tool lets you effortlessly bake Pangaea into your build process, providing you with an extremely powerful text pre-processor wherever you need it.

How it works
This is just a normal text file that has pangaea scripts embedded, e.g. JavaScript:

<pangaea type="text/javascript">
  function name(){
    return "Pangaea";
  }
</pangaea>

We can call the scripts to get the name, which is <%= name() %>.

Other JavaScript also works, so we know that 5 + 10 is <%= 5 + 10 %>.

The above file, when processed will output as:

This is just a normal text file that has scripts embedded:

We can call the scripts to get the name, which is Pangaea.

Other JavaScript also works, so we know that 5 + 10 is 15.
Usage
Embedding JavaScript

To embed JavaScript, just use the <pangaea> script tag like this:

<pangaea type="text/javascript">
  // code goes here
</pangaea>

For inline script that will print out the return, use the <%= and %> special tags:

My name is <%= name %>.
Command line tool

The pangaea command reads from the standard in pipe, and writes to the standard out pipe.

cat ./example/source.txt | pangaea >> ./example/output.txt

Or you can use it in an interactive mode:

pangaea
  • See some examples of how you can use Pangaea.
Parameters

Pangaea supports parameters that are made available to your scripts via the $$params global object. You can specify parameters using the -params flag and a URL encoded query string:

pangaea -params="name=Mat&age=30"

This will make the $$params["name"] and $$params["age"] variables available to your scripts.

  • All parameter values are strings, if you want to use other types you can cast them in your code.

Built-in methods

Pangaea comes with a series of useful built-in methods that would otherwise be impossible when running JavaScript in the browser.

$$contentsOf - loads the contents of a file
(string) $$contentsOf(filename)
<pangaea type="text/javascript">
  var name = $$contentsOf("name.txt");
</pangaea>
My name is <%= name %>.
$$run - runs a shall command and returns the result
(string) $$run(command, [arg1, [arg2, [arg3...]]])
<pangaea type="text/javascript">
  var now = $$run("date");
</pangaea>
The date is <%= now %>.
Download latest

Check out releases, or download the source and build from source using go install.

Rules
  • Keep <pangaea type="text/javascript"> and </pangaea> tags on their own lines
  • Please report any issues
  • There are no rules

Development

To build and run the command inside the cmd directory, do:

clear; go build -o pangaea; cat ./example/source.txt | ./pangaea >> ./example/output.txt

Documentation

Overview

Pangaea is a pre-processor for any text file. Statements and instructions are written in JavaScript. Script blocks are contained within <pangaea type="text/javascript">...</pangaea> tags and the <%= code %> inline script will cause the output to be written in place.

Example

<pangaea type="text/javascript">
  function name() {
    return "Pangaea";
  }
</pangaea>
Hello from <%= name() %>.

Produces:

Hello from Pangaea.

Index

Constants

This section is empty.

Variables

View Source
var (
	ScriptStartLine   []byte = []byte(`<pangaea`)
	ScriptEndLine     []byte = []byte("</pangaea>")
	Linefeed          []byte = []byte("\n")
	InlineScriptStart []byte = []byte("<%=")
	InlineScriptEnd   []byte = []byte("%>")
	FunctionPrefix    string = "$$"
	GlobalVarArgs     string = FunctionPrefix + "params"
)

Functions

This section is empty.

Types

type LineReader

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

LineReader reads from an io.Reader one line at a time.

func NewLineReader

func NewLineReader(source io.Reader) *LineReader

NewLineReader creates a new LineReader capable of reading lines from the specified source io.Reader.

func (*LineReader) ReadLine

func (r *LineReader) ReadLine() ([]byte, error)

ReadLine reads the next line from the io.Reader that was specified in NewLineReader.

type Method added in v0.2.1

type Method struct {
	// Name is the name of the method.
	Name string
	// Func is the MethodFunc that will be executed if this method
	// is called in the script.
	Func MethodFunc
}

Method represents a Pangaea method that is available to pangaea scripts.

func (*Method) Bind added in v0.2.1

func (m *Method) Bind(runtime *otto.Otto) error

Bind binds the method to the specified runtime.

type MethodFunc added in v0.2.1

type MethodFunc func(call otto.FunctionCall) otto.Value

MethodFunc represents the Go func that does the work for a Method.

type Methods added in v0.2.1

type Methods []*Method

Methods represents a slice of *Method objects.

func (Methods) Bind added in v0.2.1

func (m Methods) Bind(runtime *otto.Otto) error

Bind binds all methods in ths slice to the specified runtime.

type Parser

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

func New

func New(reader io.Reader, writer io.Writer) (*Parser, error)

func (*Parser) Parse

func (p *Parser) Parse() error

Parse processes the input and writes to the output.

func (*Parser) SetParamsFromURLStr added in v0.2.0

func (p *Parser) SetParamsFromURLStr(s string) error

SetParamsFromURLStr sets the parameter string containing the parameters to make avilable to the scripts.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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