Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,6 @@ docs/docs/public/c-reference

# Clangd config
compile_flags.txt

# Go
!/go/herb/go.mod
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ namespace :prism do
"Rakefile",
"src/",
"include/",
"templates/"
"templates/",
"build/"
]

files.each do |file|
Expand Down
14 changes: 14 additions & 0 deletions go/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# generates go bindings
build:
c-for-go herb.yml

# cleanup generated files
clean:
rm -f herb/cgo_helpers.go herb/cgo_helpers.h herb/cgo_helpers.c
rm -f herb/const.go herb/doc.go herb/types.go
rm -f herb/herb.go
rm -rf bin/

# test build and run tests
test:
cd herb && go build && go test
67 changes: 67 additions & 0 deletions go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Herb Go Bindings

Go bindings for Herb - Powerful and seamless HTML-aware ERB parsing and tooling.

## Building

### Prerequisites

- Go 1.20 or later
- C compiler (gcc/clang)
- Herb C library built from parent directory
- [c-for-go](https://github.com/xlab/c-for-go) go-lang bindings generator

### Build

```bash
# Build herb C library from project root
cd ..
make build/libherb.a
make prism

# Generate bindings
cd go
make build
```

## Usage

### As a Library

```go
package main

import (
"fmt"
"github.com/marcoroth/herb"
)

func main() {
template := "<h1><%= title %></h1>"

// Lex ERB template
tokens := herb.Lex(template)
if tokens != nil {
fmt.Println("Lexing successful")
}

// Parse ERB template
ast := herb.Parse(template, nil)
if ast != nil {
fmt.Println("Parsing successful")
}

// Extract Ruby code
ruby := herb.Extract(template, 0)
if ruby != nil {
fmt.Printf("Ruby: %s\n", string(*ruby))
}
}
```

## Testing

```bash
cd herb
go test -v
```
59 changes: 59 additions & 0 deletions go/herb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
GENERATOR:
PackageName: herb
PackageDescription: "Go bindings for Herb - HTML-aware ERB parser"
PackageLicense: "MIT"
Includes:
- herb_go.h
Options:
SafeStrings: true
FlagGroups:
- {name: CFLAGS, flags: ["-I..", "-I../src/include", "-I../src"]}
- {name: LDFLAGS, flags: ["${SRCDIR}/../../build/libherb.a", "${SRCDIR}/../../vendor/prism/build/libprism.a"]}

PARSER:
IncludePaths:
- "."
SourcesPaths:
- ./herb/herb_go.h

TRANSLATOR:
Rules:
global:
- { action: accept, from: "^herb_" }
- { action: accept, from: "^hb_" }
- { action: accept, from: "^AST_" }
- { action: accept, from: "^parser_" }

function:
- { action: accept, from: "^herb_" }
- { action: replace, from: "^herb_", to: "" }
- { load: snakecase, transform: lower }
- { transform: export }

type:
- { action: accept, from: "_[tT]$" }
- { action: replace, from: "_[tT]$", to: "" }
- { load: snakecase, transform: pascalcase }

MemTips:
- { target: "^hb_", self: raw }
- { target: "^AST_", self: raw }
- { target: "^parser_", self: raw }
- { target: "char$", self: raw }

PtrTips:
function:
- { target: "^herb_lex$", tips: [0, ref] }
- { target: "^herb_lex_file$", tips: [0, ref] }
- { target: "^herb_lex_to_buffer$", tips: [0, ref] }
- { target: "^herb_parse$", tips: [0, ref, ref] }
- { target: "^herb_version$", tips: [ref] }
- { target: "^herb_prism_version$", tips: [ref] }
- { target: "^herb_free_tokens$", tips: [ref] }
- { target: "^herb_extract_ruby_to_buffer$", tips: [0, ref] }
- { target: "^herb_extract_html_to_buffer$", tips: [0, ref] }
- { target: "^herb_extract_ruby_with_semicolons$", tips: [0, ref, ref] }
- { target: "^herb_extract_ruby_to_buffer_with_semicolons$", tips: [0, ref] }
- { target: "^herb_extract$", tips: [0, ref, ref] }
- { target: "^herb_extract_from_file$", tips: [0, ref, ref] }
Loading
Loading