From 2ba61f10ff2023217b586b4117aeb755b074416d Mon Sep 17 00:00:00 2001 From: cyber-dream Date: Wed, 7 Jun 2023 03:42:50 +0300 Subject: [PATCH] Markdown with supress

--- apps/aboutme/personalprops.go | 259 ++++++++++++++++--------- apps/blogviewer/blogviewer.go | 4 +- libs/markdown.go | 9 +- routes/private.go | 10 +- templates/personal-properties/app.tmpl | 45 ++--- 5 files changed, 192 insertions(+), 135 deletions(-) diff --git a/apps/aboutme/personalprops.go b/apps/aboutme/personalprops.go index 0f7aaed..b08e85e 100644 --- a/apps/aboutme/personalprops.go +++ b/apps/aboutme/personalprops.go @@ -2,10 +2,12 @@ package aboutme import ( "errors" + "html/template" "net/http" "path" "personalwebsite/apps/appCtx" "personalwebsite/errormessage" + "personalwebsite/libs" "personalwebsite/webfilesystem" "github.com/gin-gonic/gin" @@ -15,6 +17,7 @@ import ( type AboutMeApp struct { fs *webfilesystem.WebFileSystem appID string + mLib libs.MarkdownLib } func NewAboutMeApp(webFs *webfilesystem.WebFileSystem) *AboutMeApp { @@ -54,12 +57,35 @@ func (p *AboutMeApp) PublicRoutes(route *gin.RouterGroup) { func (p *AboutMeApp) PrivateRoutes(router *gin.RouterGroup) { p.PublicRoutes(router) - router.GET("writeMock", func(ctx *gin.Context) { - err := p.WriteMock() - if err != nil { - ctx.Status(http.StatusInternalServerError) - } - ctx.Status(http.StatusOK) + // router.GET("writeMock", func(ctx *gin.Context) { + // err := p.WriteMock() + // if err != nil { + // ctx.Status(http.StatusInternalServerError) + // } + // ctx.Status(http.StatusOK) + // }) + + router.GET("getMock", func(ctx *gin.Context) { + ctx.JSON(http.StatusOK, PropertiesFileData{ + Header: HeaderIsland{}, + Islands: []Island{ + { + Header: "qq", + Properties: []IslandProperty{ + { + Key: "22", + KeyComments: []string{ + "45", + "12", + }, + Values: []string{ + "aaaa", + }, + }, + }, + }, + }, + }) }) router.GET("read", func(ctx *gin.Context) { @@ -93,7 +119,9 @@ func (p *AboutMeApp) PrivateRoutes(router *gin.RouterGroup) { propsData := PropertiesFileData{} err := ctx.BindJSON(&propsData) if err != nil { - ctx.Status(http.StatusBadRequest) + ctx.JSON(http.StatusBadRequest, errormessage.ErrorMessage{ + Message: err.Error(), + }) return } @@ -113,72 +141,72 @@ func (p *AboutMeApp) GetAppID() string { return p.appID } -func (p *AboutMeApp) WriteMock() error { - fileHeader := webfilesystem.FileHeader{ - MongoId: primitive.NewObjectID(), - Name: "aboutme.props", - Type: "personal-properties", - Icon: "", - } - fileData := PropertiesFileData{ - Header: HeaderIsland{ - Name: "Test Name", - IconPath: "test icon path", - Info1: "Info1", - Info2: "Info2", - }, - Props: []PropIsland{ - { - Header: "Test Prop Header", - Props: []PropElement{ - { - Key: "Test key", - KeyComments: []string{"Test key comment 1", "Test key comment 1"}, - Values: []Value{ - { - Blocks: []Block{ - { - Type: "string", - Data: []string{"aappo"}, - }, - { - Type: "link", - Data: []string{"https://ya.ru", "Azazaza"}, - }, - }, - }, - }, - }, - }, - }, - { - Header: "Test Prop Header 2", - Props: []PropElement{ - { - Key: "Test key", - KeyComments: []string{"Test key comment 1", "Test key comment 1"}, - Values: []Value{ - { - Blocks: []Block{ - { - Type: "string", - Data: []string{"aappo"}, - }, - { - Type: "link", - Data: []string{"https://ya.ru", "Azazaza"}, - }, - }, - }, - }, - }, - }, - }, - }, - } - _, _, err := p.fs.Write("/Applications/AboutMe.app/aboutme.props", &fileHeader, fileData) - return err -} +// func (p *AboutMeApp) WriteMock() error { +// fileHeader := webfilesystem.FileHeader{ +// MongoId: primitive.NewObjectID(), +// Name: "aboutme.props", +// Type: "personal-properties", +// Icon: "", +// } +// fileData := PropertiesFileData{ +// Header: HeaderIsland{ +// Name: "Test Name", +// IconPath: "test icon path", +// Info1: "Info1", +// Info2: "Info2", +// }, +// Props: []PropIsland{ +// { +// Header: "Test Prop Header", +// Props: []PropElement{ +// { +// Key: "Test key", +// KeyComments: []string{"Test key comment 1", "Test key comment 1"}, +// Values: []Value{ +// { +// Blocks: []ValueBlock{ +// { +// Type: "string", +// Data: []string{"aappo"}, +// }, +// { +// Type: "link", +// Data: []string{"https://ya.ru", "Azazaza"}, +// }, +// }, +// }, +// }, +// }, +// }, +// }, +// { +// Header: "Test Prop Header 2", +// Props: []PropElement{ +// { +// Key: "Test key", +// KeyComments: []string{"Test key comment 1", "Test key comment 1"}, +// Values: []Value{ +// { +// Blocks: []ValueBlock{ +// { +// Type: "string", +// Data: []string{"aappo"}, +// }, +// { +// Type: "link", +// Data: []string{"https://ya.ru", "Azazaza"}, +// }, +// }, +// }, +// }, +// }, +// }, +// }, +// }, +// } +// _, _, err := p.fs.Write("/Applications/AboutMe.app/aboutme.props", &fileHeader, fileData) +// return err +// } func (p *AboutMeApp) Read(filePath string) (*PropertiesFileData, error) { propData := PropertiesFileData{} @@ -215,47 +243,84 @@ func (p *AboutMeApp) Edit(filePath string, blogData PropertiesFileData) error { } func (p *AboutMeApp) Render(appCtx appCtx.AppContext, filePath string) (gin.H, error) { - propsData := PropertiesFileData{} + //Read file from WebFS + propsData := &PropertiesFileData{} filePath = p.fs.RelativeToAbsolute(appCtx, filePath) _, err := p.fs.Read(filePath, &propsData) if err != nil { return nil, err } + + //Render Markdown strings + renderedIslands := []RenderedIsland{} + for _, island := range propsData.Islands { + newRenderedIsland := RenderedIsland{ + Header: island.Header, + Properties: []RenderedIslandProperty{}, + } + for _, property := range island.Properties { + newRenderedIslandProperty := RenderedIslandProperty{ + Key: property.Key, + KeyComments: property.KeyComments, + Values: []template.HTML{}, + } + for _, value := range property.Values { + renderedValue := p.mLib.Render([]byte(value), true) + newRenderedIslandProperty.Values = append(newRenderedIslandProperty.Values, renderedValue) + } + newRenderedIsland.Properties = append(newRenderedIsland.Properties, newRenderedIslandProperty) + } + renderedIslands = append(renderedIslands, newRenderedIsland) + } + + //Make icon path absolute propsData.Header.IconPath = p.fs.RelativeToAbsolute(appCtx, propsData.Header.IconPath) + return gin.H{ - "headerProps": propsData.Header, - "allprops": propsData.Props, + "HeaderProps": propsData.Header, + "Islands": renderedIslands, }, nil } +type PropertiesFileData struct { + Header HeaderIsland `bson:"header"` + Islands []Island `bson:"islands"` //TODO rename +} + type HeaderIsland struct { Name string `bson:"name"` IconPath string `bson:"iconpath"` - Info1 string `bson:"info1"` - Info2 string `bson:"info2"` + Value1 string `bson:"value1"` //TODO Rename to value + Value2 string `bson:"value2"` } -type PropElement struct { +type Island struct { + Header string `bson:"header"` + Properties []IslandProperty `bson:"properties"` +} + +type IslandProperty struct { + Key string `bson:"key"` + KeyComments []string `bson:"keycomment"` + Values []string `bson:"values"` +} + +type RenderedIsland struct { + Header string + Properties []RenderedIslandProperty +} + +type RenderedIslandProperty struct { Key string KeyComments []string - Values []Value + Values []template.HTML } -type Value struct { - Blocks []Block -} +// type Value struct { +// Blocks []ValueBlock +// } -type Block struct { - Type string - Data []string -} - -type PropIsland struct { - Header string - Props []PropElement -} - -type PropertiesFileData struct { - Header HeaderIsland `bson:"header"` - Props []PropIsland `bson:"props"` -} +// type ValueBlock struct { +// Type string +// Data string +// } diff --git a/apps/blogviewer/blogviewer.go b/apps/blogviewer/blogviewer.go index 2c7ab1f..3f661c8 100644 --- a/apps/blogviewer/blogviewer.go +++ b/apps/blogviewer/blogviewer.go @@ -45,7 +45,7 @@ func (b *BlogViewerApplication) PrivateRoutes(route *gin.RouterGroup) { for _, block := range blogData.Blocks { for _, str := range block.Data { test := []byte(str) - mLib.Render(test) + mLib.Render(test, true) } } }) @@ -239,7 +239,7 @@ func (b *BlogViewerApplication) Render(filePath string, appCtx appCtx.AppContext // } case "markdown": for _, data := range block.Data { - renderedMD := b.mLib.Render([]byte(data)) + renderedMD := b.mLib.Render([]byte(data), true) newBlock.Data = append(newBlock.Data, renderedMD) } newData = append(newData, newBlock) diff --git a/libs/markdown.go b/libs/markdown.go index 8054dc5..2f390a4 100644 --- a/libs/markdown.go +++ b/libs/markdown.go @@ -2,6 +2,7 @@ package libs import ( "html/template" + "strings" "github.com/microcosm-cc/bluemonday" "github.com/russross/blackfriday/v2" @@ -10,8 +11,12 @@ import ( type MarkdownLib struct { } -func (ml *MarkdownLib) Render(s []byte) template.HTML { - output := blackfriday.Run(s) +func (ml *MarkdownLib) Render(md []byte, supressParagraph bool) template.HTML { + output := blackfriday.Run(md) + if supressParagraph { + output = []byte(strings.ReplaceAll(string(output), "

", "")) + output = []byte(strings.ReplaceAll(string(output), "

", "")) + } _ = output html := bluemonday.UGCPolicy().SanitizeBytes(output) // println(string(html)) diff --git a/routes/private.go b/routes/private.go index 58eb34d..44d5ef7 100644 --- a/routes/private.go +++ b/routes/private.go @@ -42,17 +42,17 @@ func PrivateRoutes(port string, webfs *webfilesystem.WebFileSystem, webde *wde.W Args: []string{"/home/user/Blogs/blog1.blog"}, } - // aboutMe := AppString{ - // AppPath: "/Applications/AboutMe.app", - // Args: []string{}, - // } + aboutMe := AppString{ + AppPath: "/Applications/AboutMe.app", + Args: []string{}, + } // appString2 := AppString{ // AppPath: "/Applications/Finder.app", // Args: []string{"/home/user/", "--desktop", "desktop-layer"}, // } - autostart := []AppString{appString} + autostart := []AppString{appString, aboutMe} ctx.HTML(http.StatusOK, "index.tmpl", gin.H{ "autostart": autostart, }) diff --git a/templates/personal-properties/app.tmpl b/templates/personal-properties/app.tmpl index ef93df8..78edd59 100644 --- a/templates/personal-properties/app.tmpl +++ b/templates/personal-properties/app.tmpl @@ -12,44 +12,31 @@
- My Photo + My Photo
-
{{ .headerProps.Name }}
-
{{ .headerProps.Info1 }}
-
{{ .headerProps.Info2 }}
+
{{ .HeaderProps.Name }}
+
{{ .HeaderProps.Value1 }}
+
{{ .HeaderProps.Value2 }}
- {{ range $propIsland := .allprops }} - -
-
- {{$propIsland.Header}}: -
+ {{ range $island := .Islands }} + + +
+
{{$island.Header}}:
- {{range $prop := $propIsland.Props}} + + {{range $property := $island.Properties}}
- {{$prop.Key}}: - {{ range $keyComment := $prop.KeyComments }} -
- {{ $keyComment }} -
+ {{$property.Key}}: + {{ range $keyComment := $property.KeyComments }} +
{{ $keyComment }}
{{ end }}
- {{ range $value := $prop.Values }} -
- {{ range $block := $value.Blocks }} - {{ if eq $block.Type "link"}} - {{ index $block.Data 1}} - {{ end }} - {{ if eq $block.Type "string"}} - {{ range $string := $block.Data}} - {{ $string }} - {{ end }} - {{ end }} - {{ end }} -
+ {{ range $value := $property.Values }} +
{{ $value }}
{{ end }}