forked from fz0x1/go-org-py
go-org-py
This commit is contained in:
commit
48bddd4a93
9 changed files with 394 additions and 0 deletions
190
.gitignore
vendored
Normal file
190
.gitignore
vendored
Normal file
|
@ -0,0 +1,190 @@
|
||||||
|
# ---> Python
|
||||||
|
# Byte-compiled / optimized / DLL files
|
||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
*$py.class
|
||||||
|
|
||||||
|
# C extensions
|
||||||
|
*.so
|
||||||
|
|
||||||
|
# Distribution / packaging
|
||||||
|
.Python
|
||||||
|
build/
|
||||||
|
develop-eggs/
|
||||||
|
dist/
|
||||||
|
downloads/
|
||||||
|
eggs/
|
||||||
|
.eggs/
|
||||||
|
lib/
|
||||||
|
lib64/
|
||||||
|
parts/
|
||||||
|
sdist/
|
||||||
|
var/
|
||||||
|
wheels/
|
||||||
|
share/python-wheels/
|
||||||
|
*.egg-info/
|
||||||
|
.installed.cfg
|
||||||
|
*.egg
|
||||||
|
MANIFEST
|
||||||
|
|
||||||
|
# PyInstaller
|
||||||
|
# Usually these files are written by a python script from a template
|
||||||
|
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||||
|
*.manifest
|
||||||
|
*.spec
|
||||||
|
|
||||||
|
# Installer logs
|
||||||
|
pip-log.txt
|
||||||
|
pip-delete-this-directory.txt
|
||||||
|
|
||||||
|
# Unit test / coverage reports
|
||||||
|
htmlcov/
|
||||||
|
.tox/
|
||||||
|
.nox/
|
||||||
|
.coverage
|
||||||
|
.coverage.*
|
||||||
|
.cache
|
||||||
|
nosetests.xml
|
||||||
|
coverage.xml
|
||||||
|
*.cover
|
||||||
|
*.py,cover
|
||||||
|
.hypothesis/
|
||||||
|
.pytest_cache/
|
||||||
|
cover/
|
||||||
|
|
||||||
|
# Translations
|
||||||
|
*.mo
|
||||||
|
*.pot
|
||||||
|
|
||||||
|
# Django stuff:
|
||||||
|
*.log
|
||||||
|
local_settings.py
|
||||||
|
db.sqlite3
|
||||||
|
db.sqlite3-journal
|
||||||
|
|
||||||
|
# Flask stuff:
|
||||||
|
instance/
|
||||||
|
.webassets-cache
|
||||||
|
|
||||||
|
# Scrapy stuff:
|
||||||
|
.scrapy
|
||||||
|
|
||||||
|
# Sphinx documentation
|
||||||
|
docs/_build/
|
||||||
|
|
||||||
|
# PyBuilder
|
||||||
|
.pybuilder/
|
||||||
|
target/
|
||||||
|
|
||||||
|
# Jupyter Notebook
|
||||||
|
.ipynb_checkpoints
|
||||||
|
|
||||||
|
# IPython
|
||||||
|
profile_default/
|
||||||
|
ipython_config.py
|
||||||
|
|
||||||
|
# pyenv
|
||||||
|
# For a library or package, you might want to ignore these files since the code is
|
||||||
|
# intended to run in multiple environments; otherwise, check them in:
|
||||||
|
# .python-version
|
||||||
|
|
||||||
|
# pipenv
|
||||||
|
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||||
|
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||||
|
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||||
|
# install all needed dependencies.
|
||||||
|
#Pipfile.lock
|
||||||
|
|
||||||
|
# poetry
|
||||||
|
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||||
|
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||||
|
# commonly ignored for libraries.
|
||||||
|
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||||
|
#poetry.lock
|
||||||
|
|
||||||
|
# pdm
|
||||||
|
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||||
|
#pdm.lock
|
||||||
|
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||||
|
# in version control.
|
||||||
|
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
||||||
|
.pdm.toml
|
||||||
|
.pdm-python
|
||||||
|
.pdm-build/
|
||||||
|
|
||||||
|
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||||
|
__pypackages__/
|
||||||
|
|
||||||
|
# Celery stuff
|
||||||
|
celerybeat-schedule
|
||||||
|
celerybeat.pid
|
||||||
|
|
||||||
|
# SageMath parsed files
|
||||||
|
*.sage.py
|
||||||
|
|
||||||
|
# Environments
|
||||||
|
.env
|
||||||
|
.venv
|
||||||
|
env/
|
||||||
|
venv/
|
||||||
|
ENV/
|
||||||
|
env.bak/
|
||||||
|
venv.bak/
|
||||||
|
|
||||||
|
# Spyder project settings
|
||||||
|
.spyderproject
|
||||||
|
.spyproject
|
||||||
|
|
||||||
|
# Rope project settings
|
||||||
|
.ropeproject
|
||||||
|
|
||||||
|
# mkdocs documentation
|
||||||
|
/site
|
||||||
|
|
||||||
|
# mypy
|
||||||
|
.mypy_cache/
|
||||||
|
.dmypy.json
|
||||||
|
dmypy.json
|
||||||
|
|
||||||
|
# Pyre type checker
|
||||||
|
.pyre/
|
||||||
|
|
||||||
|
# pytype static type analyzer
|
||||||
|
.pytype/
|
||||||
|
|
||||||
|
# Cython debug symbols
|
||||||
|
cython_debug/
|
||||||
|
|
||||||
|
# PyCharm
|
||||||
|
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||||
|
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||||
|
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||||
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
|
#.idea/
|
||||||
|
|
||||||
|
# ---> Go
|
||||||
|
# If you prefer the allow list template instead of the deny list, see community template:
|
||||||
|
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.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/
|
||||||
|
|
||||||
|
# Go workspace file
|
||||||
|
go.work
|
||||||
|
go.work.sum
|
||||||
|
|
||||||
|
# env file
|
||||||
|
.env
|
3
Makefile
Normal file
3
Makefile
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
clean:
|
||||||
|
rm -rf gopy_build/ build/ go_org_py/go.py go_org_py/_convert.cpython*.so go_org_py/convert.py
|
||||||
|
|
24
README.md
Normal file
24
README.md
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
# go-org-py
|
||||||
|
|
||||||
|
go-org-py provides Python bindings for
|
||||||
|
[go-org](https://github.com/niklasfasching/go-org), a fast and accurate Org-mode
|
||||||
|
parser and renderer written in Go. The bindings are generated using gopy,
|
||||||
|
enabling direct access to go-org functionality from Python.
|
||||||
|
|
||||||
|
## install
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go install golang.org/x/tools/cmd/goimports@latest
|
||||||
|
go install github.com/go-python/gopy@master
|
||||||
|
pip install git+https://git.fz0x1.wtf/fz0x1/go-org-py.git
|
||||||
|
```
|
||||||
|
|
||||||
|
## using
|
||||||
|
|
||||||
|
```python
|
||||||
|
from go_org_py import convert
|
||||||
|
|
||||||
|
html = convert.OrgToHTML("* test")
|
||||||
|
# or
|
||||||
|
html = convert.Render("* test", <path file>, "html")
|
||||||
|
```
|
82
convert.go
Normal file
82
convert.go
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
package convert
|
||||||
|
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/alecthomas/chroma/v2"
|
||||||
|
"github.com/alecthomas/chroma/v2/formatters/html"
|
||||||
|
"github.com/alecthomas/chroma/v2/lexers"
|
||||||
|
"github.com/alecthomas/chroma/v2/styles"
|
||||||
|
"github.com/niklasfasching/go-org/org"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Render(src, path, format string) (string, error) {
|
||||||
|
doc := org.New().Parse(strings.NewReader(src), path)
|
||||||
|
|
||||||
|
write := func(w org.Writer) (string, error) {
|
||||||
|
out, wErr := doc.Write(w)
|
||||||
|
if pErr := doc.Error; pErr != nil {
|
||||||
|
return out, pErr
|
||||||
|
}
|
||||||
|
return out, wErr
|
||||||
|
}
|
||||||
|
|
||||||
|
switch strings.ToLower(format) {
|
||||||
|
case "org":
|
||||||
|
return write(org.NewOrgWriter())
|
||||||
|
case "html":
|
||||||
|
return write(org.NewHTMLWriter())
|
||||||
|
case "html-chroma":
|
||||||
|
w := org.NewHTMLWriter()
|
||||||
|
w.HighlightCodeBlock = highlightCodeBlock
|
||||||
|
return write(w)
|
||||||
|
default:
|
||||||
|
return "", errors.New("unknown format")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func OrgToHTML(src string) (string, error) {
|
||||||
|
return Render(src, "", "html")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// --- helpers ---------------------------------------------------------------
|
||||||
|
|
||||||
|
func highlightCodeBlock(source, lang string, inline bool,
|
||||||
|
params map[string]string) string {
|
||||||
|
var b strings.Builder
|
||||||
|
|
||||||
|
l := lexers.Get(lang)
|
||||||
|
if l == nil {
|
||||||
|
l = lexers.Fallback
|
||||||
|
}
|
||||||
|
l = chroma.Coalesce(l)
|
||||||
|
it, _ := l.Tokenise(nil, source)
|
||||||
|
|
||||||
|
opts := []html.Option{}
|
||||||
|
if hl := params[":hl_lines"]; hl != "" {
|
||||||
|
if r := org.ParseRanges(hl); r != nil {
|
||||||
|
opts = append(opts, html.HighlightLines(r))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
styleName := params[":style"]
|
||||||
|
if styleName == "" {
|
||||||
|
styleName = "friendly"
|
||||||
|
}
|
||||||
|
style := styles.Get(styleName)
|
||||||
|
if style == nil {
|
||||||
|
style = styles.Fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = html.New(opts...).Format(&b, style, it)
|
||||||
|
|
||||||
|
wrap := "highlight"
|
||||||
|
if inline {
|
||||||
|
wrap = "highlight-inline"
|
||||||
|
}
|
||||||
|
return `<div class="` + wrap + `">` + "\n" + b.String() + "\n</div>"
|
||||||
|
}
|
14
go.mod
Normal file
14
go.mod
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
module convert
|
||||||
|
|
||||||
|
go 1.24.4
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/alecthomas/chroma/v2 v2.18.0
|
||||||
|
github.com/go-python/gopy v0.4.10
|
||||||
|
github.com/niklasfasching/go-org v1.8.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/dlclark/regexp2 v1.11.5 // indirect
|
||||||
|
golang.org/x/net v0.38.0 // indirect
|
||||||
|
)
|
18
go.sum
Normal file
18
go.sum
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0=
|
||||||
|
github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
|
||||||
|
github.com/alecthomas/chroma/v2 v2.18.0 h1:6h53Q4hW83SuF+jcsp7CVhLsMozzvQvO8HBbKQW+gn4=
|
||||||
|
github.com/alecthomas/chroma/v2 v2.18.0/go.mod h1:RVX6AvYm4VfYe/zsk7mjHueLDZor3aWCNE14TFlepBk=
|
||||||
|
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
|
||||||
|
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
|
||||||
|
github.com/dlclark/regexp2 v1.11.5 h1:Q/sSnsKerHeCkc/jSTNq1oCm7KiVgUMZRDUoRu0JQZQ=
|
||||||
|
github.com/dlclark/regexp2 v1.11.5/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
|
||||||
|
github.com/go-python/gopy v0.4.10 h1:Ec3x+NTSzLsw9f6FTdDLwQCQlmlNmJIu4J6nSnyugqE=
|
||||||
|
github.com/go-python/gopy v0.4.10/go.mod h1:zMV/gSSYa9u/8Zp0WYR+L/z+kOIqIUtMg/a1/GRy5uw=
|
||||||
|
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
|
||||||
|
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
|
||||||
|
github.com/niklasfasching/go-org v1.8.0 h1:WyGLaajLLp8JbQzkmapZ1y0MOzKuKV47HkZRloi+HGY=
|
||||||
|
github.com/niklasfasching/go-org v1.8.0/go.mod h1:e2A9zJs7cdONrEGs3gvxCcaAEpwwPNPG7csDpXckMNg=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
|
||||||
|
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
|
3
go_org_py/__init__.py
Normal file
3
go_org_py/__init__.py
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
from .convert import OrgToHTML, Render
|
||||||
|
|
||||||
|
__all__ = ["Render", "OrgToHTML"]
|
23
pyproject.toml
Normal file
23
pyproject.toml
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
[build-system]
|
||||||
|
requires = ["setuptools>=69", "wheel", "pybindgen"]
|
||||||
|
build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
|
[project]
|
||||||
|
name = "go-org-py"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = "go-org bindings via gopy"
|
||||||
|
readme = "README.md"
|
||||||
|
requires-python = ">=3.11"
|
||||||
|
authors = [
|
||||||
|
{ name = "fz0x1", email = "fz0x1@posteo.net" },
|
||||||
|
]
|
||||||
|
classifiers = [
|
||||||
|
"Programming Language :: Python :: 3",
|
||||||
|
"Programming Language :: Go",
|
||||||
|
]
|
||||||
|
|
||||||
|
[tool.setuptools]
|
||||||
|
packages = ["go_org_py"]
|
||||||
|
|
||||||
|
[tool.setuptools.package-data]
|
||||||
|
"" = ["convert.go", "go.mod", "go.sum", "*.so"]
|
37
setup.py
Normal file
37
setup.py
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from setuptools import setup
|
||||||
|
from setuptools.command.build_py import build_py
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parent
|
||||||
|
PKG_DIR = ROOT / "go_org_py"
|
||||||
|
BUILD_DIR = ROOT / "gopy_build"
|
||||||
|
|
||||||
|
|
||||||
|
class GopyHook(build_py):
|
||||||
|
def run(self):
|
||||||
|
self._run_gopy()
|
||||||
|
subprocess.run(["ls", "-la"], check=True)
|
||||||
|
super().run()
|
||||||
|
|
||||||
|
def _run_gopy(self):
|
||||||
|
BUILD_DIR.mkdir(exist_ok=True)
|
||||||
|
env = os.environ.copy()
|
||||||
|
env["CGO_CFLAGS"] = "-std=gnu11 -O2"
|
||||||
|
|
||||||
|
subprocess.check_call(
|
||||||
|
["gopy", "build", "-output", str(BUILD_DIR), "-vm", "python3", "."],
|
||||||
|
env=env,
|
||||||
|
cwd=Path(__file__).resolve().parent,
|
||||||
|
)
|
||||||
|
|
||||||
|
shutil.copy2(BUILD_DIR / "convert.py", PKG_DIR)
|
||||||
|
shutil.copy2(BUILD_DIR / "go.py", PKG_DIR)
|
||||||
|
for lib in BUILD_DIR.glob("*/go_*.so"):
|
||||||
|
shutil.copy2(lib, PKG_DIR / lib.name)
|
||||||
|
|
||||||
|
|
||||||
|
setup(cmdclass={"build_py": GopyHook}, include_package_data=True)
|
Loading…
Add table
Add a link
Reference in a new issue