aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md7
-rw-r--r--formatter/formatter.go24
-rw-r--r--gsp.127
-rw-r--r--gsp.558
-rw-r--r--main.go22
-rw-r--r--parser/parser.go54
6 files changed, 42 insertions, 150 deletions
diff --git a/README.md b/README.md
index 6878d9f..fad71d3 100644
--- a/README.md
+++ b/README.md
@@ -7,11 +7,6 @@ far too bloated, visually polluting your documents.
support for such things, just use a programming- or macro language such as
Python or M4.
-`GSP` also supports transpilation into XML. This is exactly the same as
-transpilation to HTML except tags with no children take the form of `<tag/>`
-instead of `<tag>`. To enable XML transpilation, add an XML document type at
-the top of your document.
-
## Why Not Use Pug or [INSERT LANGUAGE HERE]
Simply put, they are all trash. Pug has decent syntax but requires you use
@@ -23,8 +18,6 @@ to write JavaScript/Ruby/etc. scripts, which just isn’t good enough.
## Syntax Example
```gsp
-!html{}
-
html lang="en" {
head {
meta charset="UTF-8"
diff --git a/formatter/formatter.go b/formatter/formatter.go
index 6524747..25ecafc 100644
--- a/formatter/formatter.go
+++ b/formatter/formatter.go
@@ -27,20 +27,12 @@ func PrintAst(ast parser.AstNode) {
switch ast.Type {
case parser.Text:
printText(ast.Text)
- case parser.DocType:
- printDocType(ast)
case parser.Normal:
fmt.Printf("<%s", ast.Text)
printAttrs(ast.Attrs)
+ fmt.Print(">")
- if len(ast.Children) == 0 {
- if parser.Xml {
- fmt.Print("/>")
- } else {
- fmt.Print(">")
- }
- } else {
- fmt.Print(">")
+ if len(ast.Children) > 0 {
printChildren(ast.Children)
fmt.Printf("</%s>", ast.Text)
}
@@ -86,18 +78,6 @@ func printAttrs(attrs []parser.Attr) {
}
}
-func printDocType(node parser.AstNode) {
- if parser.Xml {
- fmt.Print("<?xml")
- printAttrs(node.Attrs)
- fmt.Print("?>")
- } else {
- fmt.Print("<!DOCTYPE")
- printAttrs(node.Attrs)
- fmt.Print(">")
- }
-}
-
func printText(s string) {
for _, r := range s {
if v, ok := stringEscapes[r]; ok {
diff --git a/gsp.1 b/gsp.1
index 82bb9f4..a54744f 100644
--- a/gsp.1
+++ b/gsp.1
@@ -3,37 +3,26 @@
.Os
.Sh NAME
.Nm gsp
-.Nd better syntax for HTML and XML
+.Nd better syntax for HTML
.Sh SYNOPSIS
.Nm
-.Op Fl x
+.Op Fl d
.Op Ar
.Sh DESCRIPTION
.Nm
is a utility to transpile
.Xr gsp 5
-formatted plain\-text files into valid HTML and XML.
-Files provided as command\-line arguments will be transpiled with the result
+formatted plain-text files into valid HTML.
+Files provided as command-line arguments will be transpiled with the result
being written to the standard output.
If no arguments or the special filename
.Pa -
is provided, then input will be read from the standard input.
.Pp
-By default files are transpiled to HTML, where tags with no children take the
-form of
-.Ql <tag>
-as opposed to XML where they take the form of
-.Ql <tag/> .
-If the document begins with an XML document type or the
-.Fl x
-flag is specified, then
-.Nm
-will transpile to XML.
-.Pp
The options are as follows:
.Bl -tag -width Ds
-.It Fl x
-Transpile to XML instead of HTML.
+.It Fl d
+Do not automatically generate a document type at the beginning of the document.
.El
.Sh EXIT STATUS
.Ex -std gsp
@@ -51,6 +40,10 @@ Expand
macros before transpiling:
.Pp
.Dl $ m4 foo.gsp | gsp
+.Pp
+Use your own document type instead of the HTML5 one:
+.Pp
+.Dl $ printf \(aq%s%s\(aq \(dq$doctype\(dq \(dq$(gsp -d foo.gsp)\(dq
.Sh SEE ALSO
.Xr m4 1 ,
.Xr sed 1 ,
diff --git a/gsp.5 b/gsp.5
index a91f97f..a147547 100644
--- a/gsp.5
+++ b/gsp.5
@@ -7,20 +7,18 @@
.Sh DESCRIPTION
The
.Nm
-language is an alternative language to HTML and XML which can be transpiled into
-either by making use of the
+language is an alternative language to HTML which can be transpiled by making
+use of the
.Xr gsp 1
transpiler.
The
.Nm
-language allows you to structure data in the same manner as XML while offering
-an easier\-to\-read and less verbose syntax, and also some nice shortcuts for
-working with HTML. An example
+language allows you to structure data in the same manner as HTML while offering
+an easier\-to\-read and less verbose syntax, and also some nice shortcuts.
+An example
.Nm
document might look as follows:
.Bd -literal -offset indent
-!html{}
-
html lang="en" {
head {
meta charset="UTF-8"
@@ -85,7 +83,7 @@ footer foo bar { div{} div{} }
.Ed
.Ss Node names
Node names follow the exact same naming rules as names do in XML.
-See the XML references in
+See the XML reference in
.Sx SEE ALSO
for more details.
.Ss Attributes
@@ -119,9 +117,8 @@ Like with node names, the details about which characters are allowed within an
attribute name are detailed in the XML reference found in the
.Sx SEE ALSO
section of this manual.
-.Ss HTML IDs and -classes
-When transpiling to HTML, you will be wanting to use IDs and classes all of the
-time.
+.Ss IDs and classes
+When transpiling, you will be wanting to use IDs and classes all of the time.
Due to the frequency of use of these parts of HTML,
.Nm
offers a shorthand syntax for specifying them.
@@ -144,38 +141,13 @@ div id="foo" class="bar baz" {
}
.Ed
.Ss Document types
-Both HTML and XML make use of document types at the start of the document. In
-HTML the document type is of the form
-.Ql <!DOCTYPE attributes>
-while in XML the document type is of the form
-.Ql <?xml attributes?> .
-You can specify a document type in
.Nm
-by using the special node name
-.Sq \&!
-or
-.Sq \&?
-for an HTML- or XML document type respectively.
-Unlike with the usual node name, you do not need to include whitespace between
-the special node name and the attributes.
-The following example includes a document type that will transpile into
-.Ql <!DOCTYPE html> :
-.Bd -literal -offset indent
-!html{}
-html {
- head {...}
- body {...}
-}
-.Ed
-.Pp
-Here is an example of using an XML document type:
-.Bd -literal -offset indent
-?version="1.1" encoding="UTF-8"{}
-document {...}
-.Ed
-.Pp
-You may only specify one document type, and it must be the first node of the
-document.
+does not support document types.
+The HTML5 document type is automatically generated when transpiling to HTML.
+If you want to use a different document type, you’ll have to do that yourself.
+There is an example of this in the
+.Xr gsp 1
+manual.
.Ss Literal text
If you want to include literal text within a node you can make use of the
special node name
@@ -184,8 +156,6 @@ Unlike with the usual node name, you do not need to include whitespace between
the special node name and the attributes.
The following example shows how you can set a page title and paragraph text:
.Bd -literal -offset indent
-!html{}
-
html {
head {
title {-My Amazing Website}
diff --git a/main.go b/main.go
index c805fec..96dd479 100644
--- a/main.go
+++ b/main.go
@@ -9,22 +9,27 @@ import (
"git.thomasvoss.com/gsp/parser"
)
+var dflag bool
+
func main() {
- for opt := byte(0); getgopt.Getopt(len(os.Args), os.Args, "x", &opt); {
+ for opt := byte(0); getgopt.Getopt(len(os.Args), os.Args, "d", &opt); {
switch opt {
- case 'x':
- parser.Xml = true
+ case 'd':
+ dflag = true
+ default:
+ fmt.Fprintf(os.Stderr, "Usage: %s [-d] [file ...]\n", os.Args[0])
+ os.Exit(1)
}
}
- os.Args = os.Args[getgopt.Optind:]
+ args := os.Args[getgopt.Optind:]
- if len(os.Args) == 0 {
+ if len(args) == 0 {
process("-")
}
- for _, arg := range os.Args {
- process(arg)
+ for _, a := range args {
+ process(a)
}
}
@@ -47,6 +52,9 @@ func process(filename string) {
die(err)
}
+ if !dflag {
+ fmt.Print("<!DOCTYPE html>")
+ }
formatter.PrintAst(ast)
fmt.Print("\n")
}
diff --git a/parser/parser.go b/parser/parser.go
index 946cc97..d8f7eb6 100644
--- a/parser/parser.go
+++ b/parser/parser.go
@@ -12,14 +12,11 @@ import (
type nodeType uint
const (
- DocType nodeType = iota
- Normal
+ Normal = iota
Tagless
Text
)
-var Xml = false
-
type Attr struct {
Key string
Value string
@@ -39,12 +36,6 @@ func ParseFile(file *os.File) (AstNode, error) {
func (reader *reader) parseDocument() (AstNode, error) {
document := AstNode{Type: Tagless}
- if doctype, err, exists := reader.parseDocType(); err != nil {
- return AstNode{}, err
- } else if exists {
- document.Children = append(document.Children, doctype)
- }
-
if node, err := reader.parseNode(); err != nil {
return AstNode{}, err
} else {
@@ -54,49 +45,6 @@ func (reader *reader) parseDocument() (AstNode, error) {
return document, nil
}
-func (reader *reader) parseDocType() (AstNode, error, bool) {
- doctype := AstNode{}
-
- r, err := reader.readNonSpaceRune()
- if err != nil {
- return AstNode{}, err, false
- }
-
- switch r {
- case '?':
- Xml = true
- fallthrough
- case '!':
- doctype.Type = DocType
- default:
- return AstNode{}, reader.unreadRune(), false
- }
-
- if attrs, err := reader.parseAttrs(); err != nil {
- return AstNode{}, err, false
- } else {
- doctype.Attrs = attrs
- }
-
- // The above call to reader.parseAttrs() guarantees that we have the ‘{’
- // token.
- if _, err := reader.readRune(); err != nil {
- return AstNode{}, err, false
- }
-
- if r, err := reader.readNonSpaceRune(); err != nil {
- return AstNode{}, err, false
- } else if r != '}' {
- return AstNode{}, invalidSyntax{
- pos: reader.pos,
- expected: "empty body (doctypes must have empty bodies)",
- found: fmt.Sprintf("‘%c’\n", r),
- }, false
- }
-
- return doctype, nil, true
-}
-
func (reader *reader) parseNode() (AstNode, error) {
if err := reader.skipSpaces(); err != nil {
return AstNode{}, err