Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolSpring8 committed Oct 13, 2020
0 parents commit 90f7c74
Show file tree
Hide file tree
Showing 16 changed files with 1,471 additions and 0 deletions.
97 changes: 97 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# JetBrains.gitignore
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
*.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser


# Go.gitignore
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/


# Other files
release/
*.def
*.a
.rustc_info.json
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lol-html"]
path = lol-html
url = https://github.com/cloudflare/lol-html/
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2020, CoolSpring8
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# lolhtml

Go bindings for [cloudflare/lol-html](https://github.com/cloudflare/lol-html/), the *Low Output Latency streaming HTML rewriter/parser with CSS-selector based API.*

**Status:** All abilities provided by C-API implemented, except for customized user data in handlers. The code is at its early stage and the API is therefore subject to change. If you have any ideas on how API can be better structured, feel free to open a PR or an issue.

## Installation

Rust is required to build the lol-html library.

For Linux:

```bash
git clone --recursive https://github.com/coolspring8/lolhtml.git
cargo build --release --manifest-path ./lol-html/c-api/ --target-dir ./
go intall
```

For Windows users, as Rust relies on MSVC toolchain by default, one more step is needed between `cargo build` and `go install`: create a `.a` file from compiled artifacts. This snippet works for me:

```powershell
gendef ./release/lolhtml.dll
dlltool --as-flags=--64 -m i386:x86-64 -k --output-lib ./lolhtml.a --input-def lolhtml.def
cp ./release/lolhtml.dll ./
```

Now let's initialize a project and create `main.go`:

```go
package main

import (
"fmt"
"github.com/coolspring8/lolhtml"
)

func main() {
rb := lolhtml.NewRewriterBuilder()
defer rb.Free()
s, _ := lolhtml.NewSelector("span")
defer s.Free()
rb.AddElementContentHandlers(
s,
func(e *lolhtml.Element) lolhtml.RewriterDirective {
e.SetInnerContentAsRaw("World")
return lolhtml.Continue
},
nil,
func(*lolhtml.TextChunk) lolhtml.RewriterDirective {
return lolhtml.Continue
},
)
r, _ := rb.Build(
lolhtml.Config{
Encoding: "utf-8",
Memory: &lolhtml.MemorySettings{
PreallocatedParsingBufferSize: 1024,
MaxAllowedMemoryUsage: 1<<63 - 1,
},
Sink: func(s string) { fmt.Print(s) },
Strict: true,
},
)
defer r.Free()
r.WriteString("<p>Hello <span>")
r.WriteString("LOL-HTML</span>!</p>")
r.End()
}
```

This program takes chunked input `<p>Hello <span>LOL-HTML</span>!</p>` and rewrites texts in `span` tag to "World". The output is ``<p>Hello <span>World</span>!</p>`` .

## Documentation

Available at [pkg.go.dev](https://pkg.go.dev/github.com/coolspring8/lolhtml). (WIP)

## Known Issue

- For now, to use `Rewriter.End()` without causing panic, you will probably need to assign a stub `DocEndHandler` function when calling `AddDocumentContentHandlers()`.

## Other Bindings

- Rust (native), C, JavaScript - [cloudflare/lol-html](https://github.com/cloudflare/lol-html/)
- Lua - [jdesgats/lua-lolhtml](https://github.com/jdesgats/lua-lolhtml/)

## License

BSD 3-Clause "New" or "Revised" License
43 changes: 43 additions & 0 deletions callback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package lolhtml

/*
#include <stdio.h>
#include "lol_html.h"
extern void callbackSink(const char *chunk, size_t chunk_len, void *);
extern lol_html_rewriter_directive_t callbackDoctype(lol_html_doctype_t *doctype, void *user_data);
extern lol_html_rewriter_directive_t callbackComment(lol_html_comment_t *comment, void *user_data);
extern lol_html_rewriter_directive_t callbackTextChunk(lol_html_text_chunk_t *text_chunk, void *user_data);
extern lol_html_rewriter_directive_t callbackElement(lol_html_element_t *element, void *user_data);
extern lol_html_rewriter_directive_t callbackDocEnd(lol_html_doc_end_t *doc_end, void *user_data);
void callback_sink(const char *chunk, size_t chunk_len, void *user_data) {
return callbackSink(chunk, chunk_len, user_data);
}
lol_html_rewriter_directive_t callback_doctype(lol_html_doctype_t *doctype, void *user_data) {
return callbackDoctype(doctype, user_data);
}
lol_html_rewriter_directive_t callback_comment(lol_html_comment_t *comment, void *user_data) {
return callbackComment(comment, user_data);
}
lol_html_rewriter_directive_t callback_text_chunk(lol_html_text_chunk_t *text_chunk, void *user_data) {
return callbackTextChunk(text_chunk, user_data);
}
lol_html_rewriter_directive_t callback_element(lol_html_element_t *element, void *user_data){
return callbackElement(element, user_data);
}
lol_html_rewriter_directive_t callback_doc_end(lol_html_doc_end_t *doc_end, void *user_data) {
return callbackDocEnd(doc_end, user_data);
}
*/
import "C"
5 changes: 5 additions & 0 deletions export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package lolhtml

// export some internal functions for test

var GetError = getError
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/coolspring8/lolhtml

go 1.15

require github.com/mattn/go-pointer v0.0.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/mattn/go-pointer v0.0.1 h1:n+XhsuGeVO6MEAp7xyEukFINEa+Quek5psIR/ylA6o0=
github.com/mattn/go-pointer v0.0.1/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc=
1 change: 1 addition & 0 deletions lol-html
Submodule lol-html added at e67557
Loading

0 comments on commit 90f7c74

Please sign in to comment.