diff options
author | Matt Arnold <matt@thegnuguru.org> | 2023-09-01 07:16:31 -0400 |
---|---|---|
committer | Matt Arnold <matt@thegnuguru.org> | 2023-09-01 07:16:31 -0400 |
commit | 3915b164c64058b1555e2eb131e4975662d8a3c0 (patch) | |
tree | ee3dde58eb4bf8baf24ebd6570467d9a64aa324f | |
parent | b6e5f0c7b76950a29f7ffef3a6ceae951dbd02a4 (diff) |
Fix a ton of html bugs, bugs in the rss feed bugs everywhere
-rw-r--r-- | assets/creatures/gregory.png | bin | 0 -> 1024365 bytes | |||
-rw-r--r-- | assets/style.css | 51 | ||||
-rw-r--r-- | cmd/setupdb/main.go | 5 | ||||
-rw-r--r-- | config.go | 1 | ||||
-rw-r--r-- | go.mod | 3 | ||||
-rw-r--r-- | go.sum | 4 | ||||
-rw-r--r-- | main.go | 12 | ||||
-rw-r--r-- | mdparser_custom.go | 119 | ||||
-rw-r--r-- | templates/base.html | 2 | ||||
-rw-r--r-- | templates/creature.html | 9 |
10 files changed, 198 insertions, 8 deletions
diff --git a/assets/creatures/gregory.png b/assets/creatures/gregory.png new file mode 100644 index 0000000..9b41919 --- /dev/null +++ b/assets/creatures/gregory.png Binary files differdiff --git a/assets/style.css b/assets/style.css index 3cb2b85..cfc4f0e 100644 --- a/assets/style.css +++ b/assets/style.css @@ -133,3 +133,54 @@ footer { max-width: 50%; height: auto; } + + .conversation { + margin-top: 0.25rem; + display: flex; + max-width: calc(65ch + 7rem); + } + .conversation-standalone { + flex: 1; + min-width: 6rem; + max-width: 6rem; + padding-right: 1rem; + } + + .conversation-smol { + flex: 1; + min-width: 4.5rem; + max-width: 4.5rem; + padding-right: 1rem; + } + + .conversation-chat { + background-color: #3c3836; + border-radius: 5px; + min-width: 0; + padding-top: 0.5rem; + padding-left: 0.75rem; + padding-right: 0.75rem; + padding-bottom: 0.125rem; + } + + .conversation-chat:before { + content: ""; + position: absolute; + height: 15px; + width: 15px; + box-sizing: border-box; + transform: rotate(135deg) translate(0.85rem, 50%); + z-index: -1; + background-color: inherit; + } + + .conversation-chat ul { + padding: 0; + margin: 0; + margin-top: 0.5ch; + } + + .conversation-chat li ~ li { + margin-top: 0.5ch; + } + \ No newline at end of file diff --git a/cmd/setupdb/main.go b/cmd/setupdb/main.go index 6535c21..3800c68 100644 --- a/cmd/setupdb/main.go +++ b/cmd/setupdb/main.go @@ -32,6 +32,9 @@ func main() { } pages, err := utils.PagesToStructs() + if err != nil { + log.Panic("Cannot convert pages") + } for p := pages.Front(); p != nil; p = p.Next() { vl := p.Value.(models.Page) db.Create(&vl) @@ -42,5 +45,5 @@ func main() { testActor.Key = utils.TestKey testActor.User = "Test" db.Create(&testActor) - return + } diff --git a/config.go b/config.go index 61c1623..9311d0e 100644 --- a/config.go +++ b/config.go @@ -3,3 +3,4 @@ package main var dsn = "web:changethis@unix(/var/lib/mysql/mysql.sock)/website?charset=utf8mb4&parseTime=True&loc=Local" var timelayout = "2006-01-02" var RSS_NUMPOST int = 15 +var creature_tag = "pb-interject" diff --git a/go.mod b/go.mod index 1fddc50..d9670a1 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/metal3d/go-slugify v0.0.0-20160607203414-7ac2014b2f23 github.com/yuin/goldmark v1.5.3 github.com/yuin/goldmark-meta v1.1.0 - golang.org/x/crypto v0.5.0 + golang.org/x/crypto v0.12.0 gorm.io/driver/mysql v1.4.5 gorm.io/gorm v1.24.3 ) @@ -18,5 +18,6 @@ require ( github.com/go-sql-driver/mysql v1.7.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect + golang.org/x/net v0.14.0 gopkg.in/yaml.v2 v2.3.0 // indirect ) diff --git a/go.sum b/go.sum index c2a9078..c27ed25 100644 --- a/go.sum +++ b/go.sum @@ -21,6 +21,10 @@ github.com/yuin/goldmark-meta v1.1.0 h1:pWw+JLHGZe8Rk0EGsMVssiNb/AaPMHfSRszZeUei github.com/yuin/goldmark-meta v1.1.0/go.mod h1:U4spWENafuA7Zyg+Lj5RqK/MF+ovMYtBvXi1lBb2VP0= golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE= golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU= +golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk= +golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw= +golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14= +golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= diff --git a/main.go b/main.go index 17fba3b..69181cc 100644 --- a/main.go +++ b/main.go @@ -199,7 +199,7 @@ func blogLanding(w http.ResponseWriter, req *http.Request) { http.Error(w, err.Error()+" Failed to open database", 550) } var allPost []models.Post - result := db.Order("created_at DESC").Find(&allPost) + result := db.Where("public = ?", true).Order("created_at DESC").Find(&allPost) if result.Error != nil { http.Error(w, result.Error.Error(), http.StatusBadRequest) return @@ -238,7 +238,7 @@ func getBlogPost(w http.ResponseWriter, req *http.Request) { if ok.Error != nil { http.Error(w, ok.Error.Error(), http.StatusNotFound) } - markdown := goldmark.New( + /* markdown := goldmark.New( goldmark.WithRendererOptions( html.WithUnsafe(), ), @@ -247,14 +247,15 @@ func getBlogPost(w http.ResponseWriter, req *http.Request) { ), ) var buf bytes.Buffer - context := parser.NewContext() - if err := markdown.Convert(result.Text, &buf, parser.WithContext(context)); err != nil { + context := parser.NewContext() */ + var buf []byte + if buf, err = parseRewriteMarkdown(result.Text); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } var retPage utils.Page retPage.Title = result.Title - retPage.Html = buf.String() + retPage.Html = string(buf[:]) tplBlog.ExecuteWriter(pongo2.Context{"title": retPage.Title, "post": retPage, "metadata": result}, w) } @@ -369,6 +370,7 @@ func UpdatePost(w http.ResponseWriter, r *http.Request) { newPost.Slug = slugify.Marshal(upload.Title) newPost.CreatedAt, _ = time.Parse(timelayout, upload.Date) newPost.UpdatedAt = time.Now() + newPost.Public = true newPost.Text = []byte(upload.Body) result := db.Model(&newPost).Where("title = ?", newPost.Title).First(&oldPost) if errors.Is(result.Error, gorm.ErrRecordNotFound) { diff --git a/mdparser_custom.go b/mdparser_custom.go new file mode 100644 index 0000000..0d0fef4 --- /dev/null +++ b/mdparser_custom.go @@ -0,0 +1,119 @@ +package main + +// Note i made chatgpt do the boring parts +import ( + "bytes" + "errors" + "fmt" + "strings" + + "github.com/flosch/pongo2/v6" + "github.com/yuin/goldmark" + meta "github.com/yuin/goldmark-meta" + "github.com/yuin/goldmark/parser" + "github.com/yuin/goldmark/renderer/html" + xhtml "golang.org/x/net/html" +) + +func parseRewriteMarkdown(text []byte) ([]byte, error) { + var tplCreature = pongo2.Must(pongo2.FromFile("templates/creature.html")) + var output bytes.Buffer + markdown := goldmark.New( + goldmark.WithRendererOptions( + html.WithUnsafe(), + ), + goldmark.WithExtensions( + meta.Meta, + ), + ) + var buf bytes.Buffer + context := parser.NewContext() + if err := markdown.Convert(text, &buf, parser.WithContext(context)); err != nil { + return nil, errors.New("could not parse markdown") + } + r := bytes.NewReader(buf.Bytes()) + origTree, err := xhtml.Parse(r) + if err != nil { + return nil, errors.New("could not parse markdown") + } + for c := origTree.FirstChild; c != nil; c = c.NextSibling { + customNode := findElement(c, creature_tag) + if customNode != nil { + msg := fmt.Sprint(customNode.FirstChild.Data) + ctx, err := gatherAttribs(customNode, creature_tag) + if err != nil { + return nil, errors.New("level 2 parser error") + + } + ctx["message"] = msg + var ourCTX pongo2.Context = pongo2.Context{"creature": ctx["creature"], "mood": ctx["mood"], "message": ctx["message"]} + s, _ := tplCreature.Execute(ourCTX) + repNode, err := xhtml.Parse(strings.NewReader(s)) + if err != nil { + return nil, errors.New("level 3 parsing error") + } + replaceNode(customNode, repNode) + + } + } + xhtml.Render(&output, origTree) + return output.Bytes(), nil + +} + +func gatherAttribs(node *xhtml.Node, target string) (map[string]string, error) { + retmap := make(map[string]string) + if node.Type == xhtml.ElementNode && node.Data == target { + // Get the value of the href attribute + for _, attr := range node.Attr { + retmap[attr.Key] = attr.Val + } + } else { + return retmap, errors.New("couldn't parse custom element") + } + return retmap, nil + +} + +// Boring part + +func findElement(node *xhtml.Node, tagName string) *xhtml.Node { + if node.Type == xhtml.ElementNode && node.Data == tagName { + return node + } + for child := node.FirstChild; child != nil; child = child.NextSibling { + if found := findElement(child, tagName); found != nil { + return found + } + } + return nil + +} + +func replaceNode(targetNode *xhtml.Node, newNode *xhtml.Node) { + parent := targetNode.Parent + if parent == nil { + return + } + + // Find the position of the target node within its parent's children + var prevSibling *xhtml.Node + for child := parent.FirstChild; child != nil; child = child.NextSibling { + if child == targetNode { + break + } + prevSibling = child + } + + if prevSibling != nil { + // Insert the new node after the previous sibling + parent.InsertBefore(newNode, targetNode) + parent.RemoveChild(targetNode) + } else { + // The target node was the first child, so replace it directly + parent.RemoveChild(targetNode) + parent.AppendChild(newNode) + } +} + +// End boring diff --git a/templates/base.html b/templates/base.html index 59ba740..757e07b 100644 --- a/templates/base.html +++ b/templates/base.html @@ -10,7 +10,7 @@ <link rel="manifest" href="/assets/site.webmanifest"> <link rel="alternate" type="application/rss+xml" title="RSS" href="https://treefort.piusbird.space/feeds/rss" /> <link rel="alternate" type="application/atom+xml" title="Atom" href="https://treefort.piusbird.space/feeds/atom" /> -<link rel="alternate" type="application/json" title="JSONFeed" href="https://treefort.piusbird.space/fee +<link rel="alternate" type="application/json" title="JSONFeed" href="https://treefort.piusbird.space/feeds/json"> {% if title %} <title> {{title}} - The Treefornt </title> {% else %} diff --git a/templates/creature.html b/templates/creature.html new file mode 100644 index 0000000..f2d7b7b --- /dev/null +++ b/templates/creature.html @@ -0,0 +1,9 @@ +{% block conversation %} +<div class="conversation"> + <div class="conversation-picture"> + <picture> + <img src="/assets/creatures/{{creature}}.png" alt="{{creature}} be {{feeling}}"> + </picture> + <div class="conversation-chat"><<b>{{creature}}</b>> {{message}}</div> +</div> +{% endblock %} \ No newline at end of file |