Initial syntax highlighting added

This commit is contained in:
Vegard Berg 2023-09-13 20:03:34 +02:00
parent 70592bfcd0
commit 259ef3e601
8 changed files with 135 additions and 20 deletions

View File

@ -3,7 +3,7 @@ testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
args_bin = []
args_bin = ["--debug"]
bin = "./tmp/main"
cmd = "just hot-reload"
#cmd = "go build -o ./tmp/main"

View File

@ -2,6 +2,7 @@ package config
import (
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
@ -16,6 +17,7 @@ type smtp struct {
var SMTP smtp = smtp{}
var BaseURL string = "localhost"
var Debug bool = false
func InitConfig() error {
viper.SetDefault("host", "[::]")
@ -24,6 +26,7 @@ func InitConfig() error {
viper.SetDefault("smtp.enabled", false)
viper.SetDefault("user.registration_enabled", false)
viper.SetDefault("base_url", "localhost")
viper.SetDefault("debug", false)
viper.SetConfigName("config")
viper.SetConfigType("toml")
@ -40,6 +43,13 @@ func InitConfig() error {
}
}
pflag.BoolP("debug", "D", false, "toggle debug mode")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
Debug = viper.GetBool("debug")
SMTP = smtp{
Enabled: viper.GetBool("smtp.enabled"),
Host: viper.GetString("smtp.host"),

20
controllers/info.go Normal file
View File

@ -0,0 +1,20 @@
package controllers
import (
"github.com/alecthomas/chroma/formatters"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
"github.com/labstack/echo/v4"
)
func GetInfoHandler(c echo.Context) error {
lexs := lexers.Names(false)
fmts := formatters.Names()
stls := styles.Names()
return c.JSON(200, map[string][]string{
"lexers": lexs,
"formatters": fmts,
"styles": stls,
})
}

22
main.go
View File

@ -21,12 +21,21 @@ import (
var static embed.FS
func main() {
logrus.SetLevel(logrus.DebugLevel)
logrus.Infoln("Application started.")
config.InitConfig()
e := echo.New()
e.Debug = config.Debug
if e.Debug {
logrus.SetLevel(logrus.DebugLevel)
logrus.Infoln("Debug mode enabled.")
} else {
logrus.SetLevel(logrus.WarnLevel)
}
logrus.SetFormatter(&prefixed.TextFormatter{
FullTimestamp: true,
})
logrus.Infoln("Application started.")
config.InitConfig()
db_string := "bin.db"
var err error
@ -40,9 +49,6 @@ func main() {
models.DatabaseMigrations(global.DB)
e := echo.New()
e.Debug = false
e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
LogURI: true,
LogStatus: true,
@ -62,6 +68,10 @@ func main() {
fs := echo.MustSubFS(static, "./static/")
e.StaticFS("/static", fs)
if e.Debug {
e.GET("/info", controllers.GetInfoHandler)
}
e.GET("/", controllers.IndexHandler)
e.GET("/new", controllers.GetNewHandler)
e.POST("/new", controllers.PostNewHandler)

View File

@ -767,6 +767,10 @@ video {
justify-self: start;
}
.overflow-x-scroll {
overflow-x: scroll;
}
.rounded-lg {
border-radius: 0.5rem;
}

70
utils/highlight_text.go Normal file
View File

@ -0,0 +1,70 @@
package utils
import (
"bytes"
"context"
"fmt"
"io"
"github.com/alecthomas/chroma/formatters/html"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
"github.com/sirupsen/logrus"
)
type HighlightedText struct {
text string
}
func (ht HighlightedText) Render(ctx context.Context, w io.Writer) error {
_, err := w.Write([]byte(ht.text))
return err
}
func NewPreText(text string) HighlightedText {
return HighlightedText{
text: fmt.Sprintf("<pre>%s</pre>", text),
}
}
func HighlightText(text, lang, style_name string) HighlightedText {
if style_name == "" {
style_name = "solarized-light"
}
style := styles.Get(style_name)
if style == nil {
logrus.WithField("style", style_name).Warnln("Cannot find style")
return NewPreText(text)
}
lexer := lexers.Get(lang)
if lexer == nil {
return NewPreText(text)
}
formatter := html.New(html.WithLineNumbers(true))
if formatter == nil {
return NewPreText(text)
}
tokens, err := lexer.Tokenise(nil, text)
if err != nil {
return NewPreText(text)
}
buf := new(bytes.Buffer)
err = formatter.Format(buf, style, tokens)
if err != nil {
return NewPreText(text)
}
b, err := io.ReadAll(buf)
if err != nil {
return NewPreText(text)
}
return HighlightedText{
text: string(b),
}
}

View File

@ -5,6 +5,7 @@ import (
"git.myrkvi.com/myrkvi/bin/config"
"git.myrkvi.com/myrkvi/bin/models"
"git.myrkvi.com/myrkvi/bin/utils"
"git.myrkvi.com/myrkvi/bin/views/components"
)
@ -25,7 +26,7 @@ templ BinPartial(file models.File) {
}
</h1>
<p class="italic mx-1 my-2">{ file.Description }</p>
<pre class="my-4">{ file.Data }</pre>
<div class="my-4 overflow-x-scroll">@utils.HighlightText(file.Data, file.Language, "")</div>
<div class="relative h-full">
<div class="absolute -bottom-32">
<form

View File

@ -14,6 +14,7 @@ import (
"git.myrkvi.com/myrkvi/bin/config"
"git.myrkvi.com/myrkvi/bin/models"
"git.myrkvi.com/myrkvi/bin/utils"
"git.myrkvi.com/myrkvi/bin/views/components"
)
@ -102,16 +103,15 @@ func BinPartial(file models.File) templ.Component {
if err != nil {
return err
}
_, err = templBuffer.WriteString("</p><pre class=\"my-4\">")
_, err = templBuffer.WriteString("</p><div class=\"my-4 overflow-x-scroll\">")
if err != nil {
return err
}
var var_8 string = file.Data
_, err = templBuffer.WriteString(templ.EscapeString(var_8))
err = utils.HighlightText(file.Data, file.Language, "").Render(ctx, templBuffer)
if err != nil {
return err
}
_, err = templBuffer.WriteString("</pre><div class=\"relative h-full\"><div class=\"absolute -bottom-32\"><form hx-post=\"")
_, err = templBuffer.WriteString("</div><div class=\"relative h-full\"><div class=\"absolute -bottom-32\"><form hx-post=\"")
if err != nil {
return err
}
@ -140,8 +140,8 @@ func BinPartial(file models.File) templ.Component {
if err != nil {
return err
}
var var_9 templ.SafeURL = templ.URL(fmt.Sprintf("/b/%s?delcode=%s", file.PageKey, file.AdminKey))
_, err = templBuffer.WriteString(templ.EscapeString(string(var_9)))
var var_8 templ.SafeURL = templ.URL(fmt.Sprintf("/b/%s?delcode=%s", file.PageKey, file.AdminKey))
_, err = templBuffer.WriteString(templ.EscapeString(string(var_8)))
if err != nil {
return err
}
@ -149,8 +149,8 @@ func BinPartial(file models.File) templ.Component {
if err != nil {
return err
}
var_10 := `permalink with deletion key`
_, err = templBuffer.WriteString(var_10)
var_9 := `permalink with deletion key`
_, err = templBuffer.WriteString(var_9)
if err != nil {
return err
}
@ -163,8 +163,8 @@ func BinPartial(file models.File) templ.Component {
if err != nil {
return err
}
var_11 := `this is your deletion key! `
_, err = templBuffer.WriteString(var_11)
var_10 := `this is your deletion key! `
_, err = templBuffer.WriteString(var_10)
if err != nil {
return err
}
@ -172,8 +172,8 @@ func BinPartial(file models.File) templ.Component {
if err != nil {
return err
}
var_12 := `you will need to store it if you want to delete the file.`
_, err = templBuffer.WriteString(var_12)
var_11 := `you will need to store it if you want to delete the file.`
_, err = templBuffer.WriteString(var_11)
if err != nil {
return err
}