From 12dbfd43bb85d9f58e817f37c05e7eba77aa1c25 Mon Sep 17 00:00:00 2001 From: Sebastian Frank Date: Tue, 19 Feb 2019 18:18:40 +0100 Subject: [PATCH] fixed merge of ptr --- main.go | 160 +++++++--- website/content/config.yml | 3 +- .../de/01_Navigation/01_mark2web/README.md | 34 +++ .../03_Benutzung/04_Templates/README.md | 288 +++++++++--------- website/templates/base.html | 2 + 5 files changed, 300 insertions(+), 187 deletions(-) diff --git a/main.go b/main.go index ca9cc4e..f9aa457 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "path" + "reflect" "regexp" "strings" @@ -67,37 +68,42 @@ type ThisPathConfig struct { Data interface{} `yaml:"Data"` } +type indexStruct struct { + InputFile *string `yaml:"InputFile"` + OutputFile *string `yaml:"OutputFile"` +} + +type metaStruct struct { + Title *string `yaml:"Title"` + Description *string `yaml:"Description"` + Keywords *string `yaml:"Keywords"` +} + +type pathStruct struct { + Strip *string `yaml:"Strip"` + IgnoreForNav *string `yaml:"IgnoreForNav"` +} + +type filenameStruct struct { + Strip *string `yaml:"Strip"` + Ignore *string `yaml:"Ignore"` + OutputExtension *string `yaml:"OutputExtension"` +} + +type markdownStruct struct { + ChromaRenderer *bool `yaml:"ChromaRenderer"` + ChromaStyle *string `yaml:"ChromaStyle"` +} + // PathConfig of subdir type PathConfig struct { - This ThisPathConfig `yaml:"This"` - - Template *string `yaml:"Template"` - - Index struct { - InputFile *string `yaml:"InputFile"` - OutputFile *string `yaml:"OutputFile"` - } `yaml:"Index"` - - Meta struct { - Title *string `yaml:"Title"` - Description *string `yaml:"Description"` - Keywords *string `yaml:"Keywords"` - } `yaml:"Meta"` - - Path struct { - Strip *string `yaml:"Strip"` - IgnoreForNav *string `yaml:"IgnoreForNav"` - } `yaml:"Path"` - - Filename struct { - Strip *string `yaml:"Strip"` - Ignore *string `yaml:"Ignore"` - OutputExtension *string `yaml:"OutputExtension"` - } `yaml:"Filename"` - - Markdown struct { - SyntaxHighlight *bool `yaml:"SyntaxHighlight"` - } `yaml:"Markdown"` + This ThisPathConfig `yaml:"This"` + Template *string `yaml:"Template"` + Index *indexStruct `yaml:"Index"` + Meta *metaStruct `yaml:"Meta"` + Path *pathStruct `yaml:"Path"` + Filename *filenameStruct `yaml:"Filename"` + Markdown *markdownStruct `yaml:"Markdown"` Data interface{} `yaml:"Data"` } @@ -116,6 +122,26 @@ type PathConfigTree struct { var contentConfig = new(PathConfigTree) +type ptrTransformer struct{} + +func (t ptrTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error { + if typ.Kind() == reflect.Ptr { + return func(dst, src reflect.Value) error { + if dst.CanSet() { + if dst.IsNil() { + dst.Set(src) + } + } + return nil + } + } + return nil +} + +func merge(dst, src interface{}) error { + return mergo.Merge(dst, src, mergo.WithTransformers(ptrTransformer{})) +} + func backToRoot(curNavPath string) string { tmpPath := "" if curNavPath != "" { @@ -147,7 +173,7 @@ func readContentDir(inBase string, outBase string, dir string, conf *PathConfig, configFile := inPath + "/config.yml" if _, err = os.Stat(configFile); os.IsNotExist(err) { log.Debug("no config.yml found in this directory, using upper configs") - mergo.Merge(newConfig, conf) + merge(newConfig, conf) // remove this newConfig.This = ThisPathConfig{} } else { @@ -163,7 +189,7 @@ func readContentDir(inBase string, outBase string, dir string, conf *PathConfig, log.Debug("merging config with upper config") oldThis := newConfig.This - mergo.Merge(newConfig, conf) + merge(newConfig, conf) newConfig.This = oldThis log.Debug(spew.Sdump(newConfig)) @@ -173,7 +199,10 @@ func readContentDir(inBase string, outBase string, dir string, conf *PathConfig, // calc outDir stripedDir := dir - regexStr := newConfig.Path.Strip + var regexStr *string + if newConfig.Path != nil { + regexStr = newConfig.Path.Strip + } if regexStr != nil && *regexStr != "" { if regex, err := regexp.Compile(*regexStr); err != nil { log.Panicf("error compiling path.strip regex '%s' from '%s': %s", *regexStr, inBase+"/"+dir, err) @@ -246,7 +275,10 @@ type navElement struct { func buildNavigation(conf *PathConfigTree, curNavMap *map[string]*navElement, curNavSlice *[]*navElement, navActive *[]*navElement, activeNav string) { for _, el := range conf.Sub { - ignNav := el.Config.Path.IgnoreForNav + var ignNav *string + if p := el.Config.Path; p != nil { + ignNav = p.IgnoreForNav + } if ignNav != nil && *ignNav != "" { regex, err := regexp.Compile(*ignNav) if err != nil { @@ -384,19 +416,26 @@ RewriteRule ^$ %{REQUEST_URI}`+goToFixed+`/ [R,L] log.Debug("merging config with upper config") oldThis := newConfig.This - mergo.Merge(newConfig, conf.Config) + merge(newConfig, conf.Config) newConfig.This = oldThis log.Debug(spew.Sdump(newConfig)) input = regex.ReplaceAll(input, []byte("")) } else { - mergo.Merge(newConfig, conf.Config) + merge(newConfig, conf.Config) } // ignore ??? ignoreFile := false - ignoreRegex := newConfig.Filename.Ignore + var ignoreRegex *string + var stripRegex *string + var outputExt *string + if f := newConfig.Filename; f != nil { + ignoreRegex = f.Ignore + stripRegex = f.Strip + outputExt = f.OutputExtension + } if ignoreRegex != nil && *ignoreRegex != "" { regex, err := regexp.Compile(*ignoreRegex) if err != nil { @@ -412,13 +451,16 @@ RewriteRule ^$ %{REQUEST_URI}`+goToFixed+`/ [R,L] // build output filename outputFilename := file - indexInputFile := conf.Config.Index.InputFile - indexOutputFile := conf.Config.Index.OutputFile + var indexInputFile *string + var indexOutputFile *string + if i := newConfig.Index; i != nil { + indexInputFile = i.InputFile + indexOutputFile = i.OutputFile + } if indexInputFile != nil && *indexInputFile == file && indexOutputFile != nil && *indexOutputFile != "" { outputFilename = *indexOutputFile } else { - stripRegex := newConfig.Filename.Strip if stripRegex != nil && *stripRegex != "" { regex, err := regexp.Compile(*stripRegex) if err != nil { @@ -426,7 +468,6 @@ RewriteRule ^$ %{REQUEST_URI}`+goToFixed+`/ [R,L] } outputFilename = regex.ReplaceAllString(outputFilename, "$1") } - outputExt := newConfig.Filename.OutputExtension if outputExt != nil && *outputExt != "" { outputFilename += "." + *outputExt } @@ -437,10 +478,23 @@ RewriteRule ^$ %{REQUEST_URI}`+goToFixed+`/ [R,L] var options []blackfriday.Option - synH := conf.Config.Markdown.SyntaxHighlight - if synH != nil && *synH { + var chromaRenderer *bool + var chromaStyle *string + if m := newConfig.Markdown; m != nil { + chromaRenderer = m.ChromaRenderer + chromaStyle = m.ChromaStyle + } + if chromaStyle == nil { + style := "monokai" + chromaStyle = &style + } + if chromaRenderer != nil && *chromaRenderer { options = []blackfriday.Option{ - blackfriday.WithRenderer(bfchroma.NewRenderer()), + blackfriday.WithRenderer( + bfchroma.NewRenderer( + bfchroma.Style(*chromaStyle), + ), + ), } } @@ -665,13 +719,19 @@ func main() { defaultPathConfig := new(PathConfig) defaultPathConfig.Template = &defaultTemplate - defaultPathConfig.Index.InputFile = &defaultInputFile - defaultPathConfig.Index.OutputFile = &defaultOutputFile - defaultPathConfig.Path.Strip = &defaultPathStrip - defaultPathConfig.Path.IgnoreForNav = &defaultPathIgnoreForNav - defaultPathConfig.Filename.Strip = &defaultFilenameStrip - defaultPathConfig.Filename.Ignore = &defaultFilenameIgnore - defaultPathConfig.Filename.OutputExtension = &defaultFilenameOutputExtension + defaultPathConfig.Index = &indexStruct{ + InputFile: &defaultInputFile, + OutputFile: &defaultOutputFile, + } + defaultPathConfig.Path = &pathStruct{ + Strip: &defaultPathStrip, + IgnoreForNav: &defaultPathIgnoreForNav, + } + defaultPathConfig.Filename = &filenameStruct{ + Strip: &defaultFilenameStrip, + Ignore: &defaultFilenameIgnore, + OutputExtension: &defaultFilenameOutputExtension, + } readContentDir(*inDir+"/content", *outDir, "", defaultPathConfig, contentConfig) //spew.Dump(contentConfig) diff --git a/website/content/config.yml b/website/content/config.yml index 4571330..5c84d78 100644 --- a/website/content/config.yml +++ b/website/content/config.yml @@ -7,4 +7,5 @@ Meta: Keywords: mark2web, Website, HTML, markdown, Generator, CMS, Content-Management-System Markdown: - SyntaxHighlight: True + ChromaRenderer: True + ChromaStyle: monokai diff --git a/website/content/de/01_Navigation/01_mark2web/README.md b/website/content/de/01_Navigation/01_mark2web/README.md index f41f1dd..6fb09ac 100644 --- a/website/content/de/01_Navigation/01_mark2web/README.md +++ b/website/content/de/01_Navigation/01_mark2web/README.md @@ -1,7 +1,41 @@ +--- +Markdown: + ChromaRenderer: False + +--- + # mark2web mark2web ist ein Generator, der aus Markdown- und Konfig-Dateien in einer Ordnerstruktur eine statische Website unter Zuhilfenahme von Templates generiert. +```mermaid +graph TD + C(Konfiguration) + M(Markdown) + A(Assets) + D(Templates) + 2(mark2web) + W(Website) + + C==>2 + M==>2 + A==>2 + D==>2 + 2==>W + + style 2 fill:#b5c50f,stroke:#000 + classDef in fill:#464645,color:#fff,stroke:#000 + classDef out stroke-width:5px,stroke:#b5c50f,fill:#ccc + class M,C,D,A in + class W out +``` + + + --- Der Generator selbst wurde in [Go](https://golang.org/) geschrieben. Es wurden dabei eine Vielzahl existierender Packages verwendet. Unter Anderem: diff --git a/website/content/de/01_Navigation/03_Benutzung/04_Templates/README.md b/website/content/de/01_Navigation/03_Benutzung/04_Templates/README.md index 57828b8..88c43d5 100644 --- a/website/content/de/01_Navigation/03_Benutzung/04_Templates/README.md +++ b/website/content/de/01_Navigation/03_Benutzung/04_Templates/README.md @@ -11,86 +11,102 @@ Nachfolgend ist ein Beispiel eines Templates: ```django - - + + - {{ Meta.Title }} - - - - + {{ Meta.Title }} + + + + - - {% block header %} -
-
- {% for nav in NavSlice %} - {{ nav.Navname }} + + {% block header %} +
+
+ {% for nav in NavSlice %} + + {{ nav.Navname }} + + {% endfor %} +
+
+ +
+ {% endblock %} - + + {% block breadcrumb %} + + {% endblock %} + + {% block content %} +
+ {{ Body }} +
+ {% endblock %} + + {% block footer %} +
+
service Level 1
+ +
+ {% endblock %} + + ``` @@ -111,7 +127,7 @@ Blockanweisungen dagegen verwenden zum Beispiel folgende Platzhalter: ```django {% if Variable %} - ... + ... {% endif %} ``` @@ -164,13 +180,13 @@ wird für `{{ Body }}` folgendes HTML: ```django - - {% for part in BodyParts %} - - {% endfor %} - + + {% for part in BodyParts %} + + {% endfor %} +
- {{ part }} -
+ {{ part }} +
``` @@ -178,16 +194,16 @@ Aus dem Template wird nach dem Rendern mit obiger Markdown-Datei also folgendes ```html - - - - + + + +
-

Titel 1

-

Text 1

-
-

Titel 2

-

Text 2

-
+

Titel 1

+

Text 1

+
+

Titel 2

+

Text 2

+
``` @@ -199,16 +215,16 @@ Jedes Navigationselement steht intern in folgender go-Struktur zur Verfügung: ```go type navElement struct { - Navname string - GoTo string - Active bool + Navname string + GoTo string + Active bool - Data interface{} + Data interface{} - This ThisPathConfig + This ThisPathConfig - SubMap *map[string]*navElement - SubSlice *[]*navElement + SubMap *map[string]*navElement + SubSlice *[]*navElement } ``` @@ -220,24 +236,24 @@ Wird z.B. folgende Navigation als Zielverzeichnis-Struktur angenommen: ```plain de - main - home - leistungen - referenzen - service - impressum + main + home + leistungen + referenzen + service + impressum en - main - home - ... + main + home + ... ``` Der Teasertext aus folgender `config.yml` im `content`-Verzeichnis `de/main/02_Leistungen` ```yaml This: - Data: - teaser: Teasertext + Data: + teaser: Teasertext ``` welcher zum Navigationspunkt im Zielpfad *de/main/leistungen* gehört, ist über folgende Template-Variablen erreichbar: @@ -258,11 +274,11 @@ Natürlich wird diese Variable in der Form so nie verwendet, sondern soll nur de ```django ``` @@ -291,9 +307,9 @@ Somit lassen sich leicht Pfade anzeigen, bzw. Breadcrumbs in die Website einbind ```django aktiver Pfad: - {% for nav in NavActive %} - {{ nav.Navname }} - {% endfor %} + {% for nav in NavActive %} + {{ nav.Navname }} + {% endfor %} ``` Ebenso lässt sich bei mehrsprachigen Seite immer die richte Hauptnavigation zur aktuelle Sprache laden: @@ -301,24 +317,24 @@ Ebenso lässt sich bei mehrsprachigen Seite immer die richte Hauptnavigation zur ```django

Hauptnavigation

``` diff --git a/website/templates/base.html b/website/templates/base.html index ac0d7ca..c84b3af 100755 --- a/website/templates/base.html +++ b/website/templates/base.html @@ -166,6 +166,8 @@ + +