Initial syntax highlighting added
This commit is contained in:
parent
70592bfcd0
commit
259ef3e601
|
@ -3,7 +3,7 @@ testdata_dir = "testdata"
|
||||||
tmp_dir = "tmp"
|
tmp_dir = "tmp"
|
||||||
|
|
||||||
[build]
|
[build]
|
||||||
args_bin = []
|
args_bin = ["--debug"]
|
||||||
bin = "./tmp/main"
|
bin = "./tmp/main"
|
||||||
cmd = "just hot-reload"
|
cmd = "just hot-reload"
|
||||||
#cmd = "go build -o ./tmp/main"
|
#cmd = "go build -o ./tmp/main"
|
||||||
|
|
|
@ -2,6 +2,7 @@ package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -16,6 +17,7 @@ type smtp struct {
|
||||||
|
|
||||||
var SMTP smtp = smtp{}
|
var SMTP smtp = smtp{}
|
||||||
var BaseURL string = "localhost"
|
var BaseURL string = "localhost"
|
||||||
|
var Debug bool = false
|
||||||
|
|
||||||
func InitConfig() error {
|
func InitConfig() error {
|
||||||
viper.SetDefault("host", "[::]")
|
viper.SetDefault("host", "[::]")
|
||||||
|
@ -24,6 +26,7 @@ func InitConfig() error {
|
||||||
viper.SetDefault("smtp.enabled", false)
|
viper.SetDefault("smtp.enabled", false)
|
||||||
viper.SetDefault("user.registration_enabled", false)
|
viper.SetDefault("user.registration_enabled", false)
|
||||||
viper.SetDefault("base_url", "localhost")
|
viper.SetDefault("base_url", "localhost")
|
||||||
|
viper.SetDefault("debug", false)
|
||||||
|
|
||||||
viper.SetConfigName("config")
|
viper.SetConfigName("config")
|
||||||
viper.SetConfigType("toml")
|
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{
|
SMTP = smtp{
|
||||||
Enabled: viper.GetBool("smtp.enabled"),
|
Enabled: viper.GetBool("smtp.enabled"),
|
||||||
Host: viper.GetString("smtp.host"),
|
Host: viper.GetString("smtp.host"),
|
||||||
|
|
|
@ -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
22
main.go
|
@ -21,12 +21,21 @@ import (
|
||||||
var static embed.FS
|
var static embed.FS
|
||||||
|
|
||||||
func main() {
|
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{
|
logrus.SetFormatter(&prefixed.TextFormatter{
|
||||||
FullTimestamp: true,
|
FullTimestamp: true,
|
||||||
})
|
})
|
||||||
logrus.Infoln("Application started.")
|
|
||||||
config.InitConfig()
|
|
||||||
|
|
||||||
db_string := "bin.db"
|
db_string := "bin.db"
|
||||||
var err error
|
var err error
|
||||||
|
@ -40,9 +49,6 @@ func main() {
|
||||||
|
|
||||||
models.DatabaseMigrations(global.DB)
|
models.DatabaseMigrations(global.DB)
|
||||||
|
|
||||||
e := echo.New()
|
|
||||||
e.Debug = false
|
|
||||||
|
|
||||||
e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
|
e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
|
||||||
LogURI: true,
|
LogURI: true,
|
||||||
LogStatus: true,
|
LogStatus: true,
|
||||||
|
@ -62,6 +68,10 @@ func main() {
|
||||||
fs := echo.MustSubFS(static, "./static/")
|
fs := echo.MustSubFS(static, "./static/")
|
||||||
e.StaticFS("/static", fs)
|
e.StaticFS("/static", fs)
|
||||||
|
|
||||||
|
if e.Debug {
|
||||||
|
e.GET("/info", controllers.GetInfoHandler)
|
||||||
|
}
|
||||||
|
|
||||||
e.GET("/", controllers.IndexHandler)
|
e.GET("/", controllers.IndexHandler)
|
||||||
e.GET("/new", controllers.GetNewHandler)
|
e.GET("/new", controllers.GetNewHandler)
|
||||||
e.POST("/new", controllers.PostNewHandler)
|
e.POST("/new", controllers.PostNewHandler)
|
||||||
|
|
|
@ -767,6 +767,10 @@ video {
|
||||||
justify-self: start;
|
justify-self: start;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.overflow-x-scroll {
|
||||||
|
overflow-x: scroll;
|
||||||
|
}
|
||||||
|
|
||||||
.rounded-lg {
|
.rounded-lg {
|
||||||
border-radius: 0.5rem;
|
border-radius: 0.5rem;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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),
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ import (
|
||||||
|
|
||||||
"git.myrkvi.com/myrkvi/bin/config"
|
"git.myrkvi.com/myrkvi/bin/config"
|
||||||
"git.myrkvi.com/myrkvi/bin/models"
|
"git.myrkvi.com/myrkvi/bin/models"
|
||||||
|
"git.myrkvi.com/myrkvi/bin/utils"
|
||||||
"git.myrkvi.com/myrkvi/bin/views/components"
|
"git.myrkvi.com/myrkvi/bin/views/components"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,7 +26,7 @@ templ BinPartial(file models.File) {
|
||||||
}
|
}
|
||||||
</h1>
|
</h1>
|
||||||
<p class="italic mx-1 my-2">{ file.Description }</p>
|
<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="relative h-full">
|
||||||
<div class="absolute -bottom-32">
|
<div class="absolute -bottom-32">
|
||||||
<form
|
<form
|
||||||
|
|
|
@ -14,6 +14,7 @@ import (
|
||||||
|
|
||||||
"git.myrkvi.com/myrkvi/bin/config"
|
"git.myrkvi.com/myrkvi/bin/config"
|
||||||
"git.myrkvi.com/myrkvi/bin/models"
|
"git.myrkvi.com/myrkvi/bin/models"
|
||||||
|
"git.myrkvi.com/myrkvi/bin/utils"
|
||||||
"git.myrkvi.com/myrkvi/bin/views/components"
|
"git.myrkvi.com/myrkvi/bin/views/components"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -102,16 +103,15 @@ func BinPartial(file models.File) templ.Component {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = templBuffer.WriteString("</p><pre class=\"my-4\">")
|
_, err = templBuffer.WriteString("</p><div class=\"my-4 overflow-x-scroll\">")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var var_8 string = file.Data
|
err = utils.HighlightText(file.Data, file.Language, "").Render(ctx, templBuffer)
|
||||||
_, err = templBuffer.WriteString(templ.EscapeString(var_8))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -140,8 +140,8 @@ func BinPartial(file models.File) templ.Component {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var var_9 templ.SafeURL = templ.URL(fmt.Sprintf("/b/%s?delcode=%s", file.PageKey, file.AdminKey))
|
var var_8 templ.SafeURL = templ.URL(fmt.Sprintf("/b/%s?delcode=%s", file.PageKey, file.AdminKey))
|
||||||
_, err = templBuffer.WriteString(templ.EscapeString(string(var_9)))
|
_, err = templBuffer.WriteString(templ.EscapeString(string(var_8)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -149,8 +149,8 @@ func BinPartial(file models.File) templ.Component {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var_10 := `permalink with deletion key`
|
var_9 := `permalink with deletion key`
|
||||||
_, err = templBuffer.WriteString(var_10)
|
_, err = templBuffer.WriteString(var_9)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -163,8 +163,8 @@ func BinPartial(file models.File) templ.Component {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var_11 := `this is your deletion key! `
|
var_10 := `this is your deletion key! `
|
||||||
_, err = templBuffer.WriteString(var_11)
|
_, err = templBuffer.WriteString(var_10)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -172,8 +172,8 @@ func BinPartial(file models.File) templ.Component {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var_12 := `you will need to store it if you want to delete the file.`
|
var_11 := `you will need to store it if you want to delete the file.`
|
||||||
_, err = templBuffer.WriteString(var_12)
|
_, err = templBuffer.WriteString(var_11)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue