corgi

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2023 License: MIT Imports: 20 Imported by: 0

README

corgi

GitBook Test Code Coverage Go Report Card License MIT


About

Corgi is an HTML template language for Go, inspired by pug (hence the name). Just like pug, corgi also uses code generation to generate its templates.

Main Features

  • 👀 Highly readable syntax that models HTML, not just replacing placeholders
  • ➕ Mixins—functions that render repeated pieces of corgi
  • 🌀 Conditional classes that are actually readable
  • 🗄 Import other files to use their mixins
  • 🖇 Split large templates into multiple files
  • 👪 Support for inheritance
  • ✨ Import any Go package and use any of its types, functions and constants—no need for FuncMaps
  • 🤏 Generates compile-time minified HTML, CSS, and JS
  • 🔒 Context-aware auto-escaping and filtering of HTML, CSS, JS, and special HTML attributes
  • 🛡️ Script CSP nonce injection
  • ⚠️ Descriptive, Rust-style errors

Example

First impressions matter, so here is an example of a simple template:

import "strings"

// corgi has a stdlib with a handful of useful mixins
use "fmt"

// These are the name and params of the function that corgi will generate.
// Besides the params you specify here, corgi will also add an io.Writer that
// it'll write the output to, and an error return, that returns any errors
// it encounters when writing to the io.Writer.
//
// The real signature will look like this:
// func LearnCorgi(w io.Writer, name string, knowsPug bool, friends []string) error
func LearnCorgi(name string, knowsPug bool, friends []string)

mixin greet(name string) Hello, #{name}!

doctype html
html(lang="en")
  head
    title Learn Corgi
  body
    h1 Learn Corgi
    p#greeting.greeting
      // the & allows you to add additional attributes to an element
      if strings.HasPrefix(name, "M"): &.font-size--big
      +greet(name=name)

    p
      if knowsPug
        > #{name}, since you already know pug,
          learning corgi will be even more of #strong[a breeze] for you!#[ ]

      > Head over to #a(href="https://mavolin.gitbook.io/corgi")[GitBook]
        to learn it.

    p And make sure to tell #+fmt.List(val=friends) about corgi too!

Pretty-Printed output:

<!-- LearnCorgi(myWriter, "Maxi", true, []string{"Huey", "Dewey", "Louie"}) -->

<!doctype html>
<html lang=en>
<head><title>Learn Corgi</title></head>
<body>
<h1>Learn Corgi</h1>
<p id=greeting class="greeting font-size--big">Hello, Maxi!</p>
<p>
    Maxi, since you already know pug,
    learning corgi will be even more of <strong>a breeze</strong> for you! Head over to
    <a href=https://mavolin.gitbook.io/corgi>GitBook</a> to learn it.
</p>
<p>And make sure to tell Huey, Dewey, and Louie about corgi too!</p>
</body>
</html>

If you're interested in the generated code, have a look at the examples directory.

Want to know more?

Have a look at the guide on GitBook.

License

Built with ❤ by Maximilian von Lindern. Available under the MIT License.

Documentation

Overview

Package corgi provides parsing for corgi files.

Index

Constants

View Source
const (
	PrecompFileName = "lib.precorgi"
	Ext             = ".corgi"
	LibExt          = ".corgil"
)

Variables

View Source
var ErrNotExists = errors.New("file does not exist")

Functions

func LoadLibrary

func LoadLibrary(sysPath string, o LoadOptions) (*file.Library, error)

LoadLibrary parses and links the library located at the passed file system path.

func LoadMain

func LoadMain(sysPath string, o LoadOptions) (*file.File, error)

func LoadMainData

func LoadMainData(in []byte, o LoadOptions) (*file.File, error)

LoadMainData parses and links the passed main file's raw data.

Callers should set the Name of the returned file, so that error messages are correctly printed.

Types

type LoadOptions

type LoadOptions struct {
	// GoExecPath is the path to the go binary to used.
	//
	// If not set, $GOROOT/bin/go will be used.
	GoExecPath string

	// NoPrecompile forces the loader to always read corgi files instead of
	// loading a precompiled file.
	NoPrecompile bool

	// Logger is used to log the individual steps of the logging process.
	//
	// If left as nil, nothing will be logged
	Logger *slog.Logger
}

Directories

Path Synopsis
cmd
corgidebug
Command corgidebug is a utility for debugging problems when the compiler doesn't do what it should.
Command corgidebug is a utility for debugging problems when the compiler doesn't do what it should.
examples
Package file provides structs that represent the structure of a corgi file.
Package file provides structs that represent the structure of a corgi file.
fileutil
Package fileutil provides utilities for interacting with a corgi AST and its contents.
Package fileutil provides utilities for interacting with a corgi AST and its contents.
precomp
Package precomp allows encoding and decoding of precompiled libraries.
Package precomp allows encoding and decoding of precompiled libraries.
internal
meta
Package meta contains metadata about the compiler.
Package meta contains metadata about the compiler.
stack
Package stack implements a stack built using a linked list.
Package stack implements a stack built using a linked list.
voidelem
Package voidelem provides utilities for working with HTML void elements.
Package voidelem provides utilities for working with HTML void elements.
Package link links implements a linker for corgi files.
Package link links implements a linker for corgi files.
test
internal/compile
Package compile provides an utility function to compile files in preparation tests.
Package compile provides an utility function to compile files in preparation tests.
internal/extra
Package extra provides variables, constants, types, and function to be imported by corgi files.
Package extra provides variables, constants, types, and function to be imported by corgi files.
internal/outcheck
Package outcheck provides a utility to check if the output of a template function matches a file.
Package outcheck provides a utility to check if the output of a template function matches a file.
Package validate provides contextual validation for [file.File].
Package validate provides contextual validation for [file.File].
Package woof provides serialization of Go values to be used in HTML documents.
Package woof provides serialization of Go values to be used in HTML documents.
Package writer provides a writer that allows converting a file.File to Go code.
Package writer provides a writer that allows converting a file.File to Go code.

Jump to

Keyboard shortcuts

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