diff --git a/.idea/runConfigurations/document_build_pdf.xml b/.idea/runConfigurations/document_build_pdf.xml index 697e35db5edb5c5cba371186df47c5e16e30e36f..2ee53ceada9065b62bc5aa95374e7e1e3062810e 100644 --- a/.idea/runConfigurations/document_build_pdf.xml +++ b/.idea/runConfigurations/document_build_pdf.xml @@ -2,7 +2,7 @@ <configuration default="false" name="document build pdf" type="GoApplicationRunConfiguration" factoryName="Go Application"> <module name="documentation-manager" /> <working_directory value="$PROJECT_DIR$/development/examples/example1" /> - <parameters value="document pdf " /> + <parameters value="document pdf --verbose" /> <kind value="DIRECTORY" /> <directory value="$PROJECT_DIR$/application/source" /> <filePath value="$PROJECT_DIR$" /> diff --git a/README.md b/README.md index b014f6dc39c580cee6cab32152d5eac1ef96f189..8a6434322ec26aa05efe4d2b377ed4cb00bda7b6 100644 --- a/README.md +++ b/README.md @@ -25,13 +25,17 @@ call go generate translations/translations.go ``` - show font directories ```bash kpsepath mf | sed -e 's/:/\n/g' ``` +### Awsome Boxes + + + +* [Awesomebox](https://ftp.gwdg.de/pub/ctan/graphics/awesomebox/awesomebox.pdf) ### Circled numbers diff --git a/application/source/commands/08_document_pdf.go b/application/source/commands/08_document_pdf.go index 14419cbb0d4753146464af0fbf99c9b924bbc082..c660831910be1c3329c56ced33dedff57d201e6c 100644 --- a/application/source/commands/08_document_pdf.go +++ b/application/source/commands/08_document_pdf.go @@ -24,6 +24,7 @@ type DocumentPDFDefinition struct { LatexTemplate string `long:"latex-template" short:"l"` MarkdownTemplate string `long:"markdown-template" short:"m"` OutputPath string `long:"output" short:"o"` + Verbose bool `long:"verbose" short:"v"` } func initDocumentPDF(command *flags.Command) { @@ -39,6 +40,8 @@ func initDocumentPDF(command *flags.Command) { opt.Description = translations.T.Sprintf("directory in which the finished documentation is to be written") case "format": opt.Description = translations.T.Sprintf("format of the finished documentation") + case "verbose": + opt.Description = translations.T.Sprintf("verbose output") } } @@ -54,6 +57,7 @@ func runDocumentPDF(command *flags.Command) { env.Templates.Latex = environment.State.GetPDFLatexTemplatePath(definition.Document.PDF.LatexTemplate) env.Templates.Markdown = environment.State.GetPDFMarkdownTemplatePath(definition.Document.PDF.MarkdownTemplate) + env.Verbose = definition.Document.PDF.Verbose document.BuildPDF(env) diff --git a/application/source/document/files.go b/application/source/document/files.go index c6b89726fd6afe00444d85b239b37eb297e7eb99..2e3b6f265bafff4739e81fc27f0586da178bab86 100644 --- a/application/source/document/files.go +++ b/application/source/document/files.go @@ -1,6 +1,7 @@ package document import ( + "errors" "io/ioutil" "os" "path" @@ -68,7 +69,7 @@ func getFiles(sourcePath string) ([]*SourceFile, error) { err, p := evaluateDocumentContent(c) if err != nil { - return err + return errors.New("Error while processing files: " + current + " (" + err.Error() + ")") } def = append(def, NewDefinition(sourcePath, current, info, p)) diff --git a/application/source/document/mapping.go b/application/source/document/mapping.go index 0b1b2e079be6e4d01a4d60d9f7b9f8807c127e0b..52a5c5359748e25d615b629a8d6a3fc131b7f275 100644 --- a/application/source/document/mapping.go +++ b/application/source/document/mapping.go @@ -36,113 +36,37 @@ var kbdMapping = map[string]string{ "pgdown": "\\LKeyPgDown", } -/** - -\DeclareRobustCommand*\LKeyStrg{\BiolinumKeyGlyph{Strg}} -\DeclareRobustCommand*\LKeyAlt{\BiolinumKeyGlyph{Alt}} -\DeclareRobustCommand*\LKeyAltApple{\biolinumKeyGlyph{"2325}} -\DeclareRobustCommand*\LKeyAltGr{\BiolinumKeyGlyph{AltGr}} -\DeclareRobustCommand*\LKeyShift{\BiolinumKeyGlyph{Shift}} -\DeclareRobustCommand*\LKeyTab{\BiolinumKeyGlyph{Tab}} -\DeclareRobustCommand*\LKeyEnter{\BiolinumKeyGlyph{Enter}} -\DeclareRobustCommand*\LKeyCapslock{\BiolinumKeyGlyph{Capslock}} -\DeclareRobustCommand*\LKeyPos{\BiolinumKeyGlyph{Pos1}} -\DeclareRobustCommand*\LKeyEntf{\BiolinumKeyGlyph{Entf}} -\DeclareRobustCommand*\LKeyEinf{\BiolinumKeyGlyph{Einf}} -\DeclareRobustCommand*\LKeyLeer{\BiolinumKeyGlyph{Leer}} -\let\LKeySpace\LKeyLeer -\DeclareRobustCommand*\LKeyEsc{\BiolinumKeyGlyph{Esc}} -\DeclareRobustCommand*\LKeyEnde{\BiolinumKeyGlyph{Ende}} -%\DeclareRobustCommand*\LKeyTux{\BiolinumKeyGlyph{Tux}} -\DeclareRobustCommand*\LKeyWin{\BiolinumKeyGlyph{Windows}} -\DeclareRobustCommand*\LKeyMenu{\biolinumKeyGlyph{"E104}} -\DeclareRobustCommand*\LKeyCtrl{\BiolinumKeyGlyph{Ctrl}} -\DeclareRobustCommand*\LKeyOptionKey{\BiolinumKeyGlyph{Fn}} -\DeclareRobustCommand*\LKeyBack{\BiolinumKeyGlyph{Back}} -\DeclareRobustCommand*\LKeyUp{\biolinumKeyGlyph{"2191}} -\DeclareRobustCommand*\LKeyDown{\biolinumKeyGlyph{"2193}} -\DeclareRobustCommand*\LKeyLeft{\biolinumKeyGlyph{"2190}} -\DeclareRobustCommand*\LKeyRight{\biolinumKeyGlyph{"2192}} -\DeclareRobustCommand*\LKeyBildUp{\BiolinumKeyGlyph{Buildup}} -\DeclareRobustCommand*\LKeyBildDown{\BiolinumKeyGlyph{Builddown}} -\DeclareRobustCommand*\LKeyAt{\biolinumKeyGlyph{"0040}} -\DeclareRobustCommand*\LKeyFn{\BiolinumKeyGlyph{Fn}} -\DeclareRobustCommand*\LKeyHome{\BiolinumKeyGlyph{Home}} -\DeclareRobustCommand*\LKeyDel{\BiolinumKeyGlyph{Del}} -\DeclareRobustCommand*\LKeyIns{\BiolinumKeyGlyph{Ins}} -\DeclareRobustCommand*\LKeyEnd{\BiolinumKeyGlyph{End}} -\DeclareRobustCommand*\LKeyGNU{\BiolinumKeyGlyph{GNU}} -\DeclareRobustCommand*\LKeyPageUp{\BiolinumKeyGlyph{Pageup}} -\DeclareRobustCommand*\LKeyPageDown{\BiolinumKeyGlyph{Pagedown}} -\DeclareRobustCommand*\LKeyWindows{\BiolinumKeyGlyph{Windows}} +func convertCircledNumbers(content string) string { + + circleMap := map[string]string{ + "➊": "\\large\\libertineGlyph{uni2776}\\normalsize", + "➋": "\\large\\libertineGlyph{uni2777}\\normalsize", + "➌": "\\large\\libertineGlyph{uni2778}\\normalsize", + "➍": "\\large\\libertineGlyph{uni2779}\\normalsize", + "➎": "\\large\\libertineGlyph{uni277A}\\normalsize", + "➏": "\\large\\libertineGlyph{uni277B}\\normalsize", + "➐": "\\large\\libertineGlyph{uni277C}\\normalsize", + "➑": "\\large\\libertineGlyph{uni277D}\\normalsize", + "➒": "\\large\\libertineGlyph{uni277E}\\normalsize", + "➓": "\\large\\libertineGlyph{uni277F}\\normalsize", + "⓫": "\\large\\libertineGlyph{uni24EB}\\normalsize", + "⓬": "\\large\\libertineGlyph{uni24EC}\\normalsize", + "⓭": "\\large\\libertineGlyph{uni24ED}\\normalsize", + "⓮": "\\large\\libertineGlyph{uni24EE}\\normalsize", + "⓯": "\\large\\libertineGlyph{uni24EF}\\normalsize", + "⓰": "\\large\\libertineGlyph{uni24F0}\\normalsize", + "⓱": "\\large\\libertineGlyph{uni24F1}\\normalsize", + "⓲": "\\large\\libertineGlyph{uni24F2}\\normalsize", + "⓳": "\\large\\libertineGlyph{uni24F3}\\normalsize", + "⓴": "\\large\\libertineGlyph{uni24F4}\\normalsize", + } -\@namedef{libertine@key@F@1}{\BiolinumKeyGlyph{F1}} -\@namedef{libertine@key@F@2}{\BiolinumKeyGlyph{F2}} -\@namedef{libertine@key@F@3}{\BiolinumKeyGlyph{F3}} -\@namedef{libertine@key@F@4}{\BiolinumKeyGlyph{F4}} -\@namedef{libertine@key@F@5}{\BiolinumKeyGlyph{F5}} -\@namedef{libertine@key@F@6}{\BiolinumKeyGlyph{F6}} -\@namedef{libertine@key@F@7}{\BiolinumKeyGlyph{F7}} -\@namedef{libertine@key@F@8}{\BiolinumKeyGlyph{F8}} -\@namedef{libertine@key@F@9}{\BiolinumKeyGlyph{F9}} -\@namedef{libertine@key@F@10}{\BiolinumKeyGlyph{F10}} -\@namedef{libertine@key@F@11}{\BiolinumKeyGlyph{F11}} -\@namedef{libertine@key@F@12}{\BiolinumKeyGlyph{F12}} -\@namedef{libertine@key@F@13}{\BiolinumKeyGlyph{F13}} -\@namedef{libertine@key@F@14}{\BiolinumKeyGlyph{F14}} -\@namedef{libertine@key@F@15}{\BiolinumKeyGlyph{F15}} -\@namedef{libertine@key@F@16}{\BiolinumKeyGlyph{F16}} -\DeclareRobustCommand*\LKeyF[1]{\@nameuse{libertine@key@F@#1}} -% -\DeclareRobustCommand*\LKeyAltF[1]{\LKeyAlt+\@nameuse{libertine@key@F@#1}} -\DeclareRobustCommand*\LKeyStrgAltF[1]{\LKeyStrg+\LKeyAlt+\@nameuse{libertine@key@F@#1}} -\DeclareRobustCommand*\LKeyStrgX[1]{\LKeyStrg+\LKey{#1}} -\DeclareRobustCommand*\LKeyShiftX[1]{\LKeyShift+\LKey{#1}} -\DeclareRobustCommand*\LKeyAltX[1]{\LKeyAlt+\LKey{#1}} -\DeclareRobustCommand*\LKeyAltAppleX[1]{\LKeyAltApple+\LKey{#1}} -\DeclareRobustCommand*\LKeyAltGrX[1]{\LKeyAltGr+\LKey{#1}} -\DeclareRobustCommand*\LKeyShiftStrgX[1]{\LKeyShift+\LKeyStrg+\LKey{#1}} -\DeclareRobustCommand*\LKeyShiftAltX[1]{\LKeyShift+\LKeyAlt+\LKey{#1}} -\DeclareRobustCommand*\LKeyShiftAltGrX[1]{\LKeyShift+\LKeyAltGr+\LKey{#1}} -\DeclareRobustCommand*\LKeyStrgAltX[1]{\LKeyStrg+\LKeyAlt+\LKey{#1}} -\DeclareRobustCommand*\LKeyStrgAltEntf{\LKeyStrg+\LKeyAlt+\LKeyEntf} -\let\LKeyReset\LKeyStrgAltEntf -% -\@namedef{libertine@key@Pad@0}{\BiolinumKeyGlyph{Pad0}} -\@namedef{libertine@key@Pad@1}{\BiolinumKeyGlyph{Pad1}} -\@namedef{libertine@key@Pad@2}{\BiolinumKeyGlyph{Pad2}} -\@namedef{libertine@key@Pad@3}{\BiolinumKeyGlyph{Pad3}} -\@namedef{libertine@key@Pad@4}{\BiolinumKeyGlyph{Pad4}} -\@namedef{libertine@key@Pad@5}{\BiolinumKeyGlyph{Pad5}} -\@namedef{libertine@key@Pad@6}{\BiolinumKeyGlyph{Pad6}} -\@namedef{libertine@key@Pad@7}{\BiolinumKeyGlyph{Pad7}} -\@namedef{libertine@key@Pad@8}{\BiolinumKeyGlyph{Pad8}} -\@namedef{libertine@key@Pad@9}{\BiolinumKeyGlyph{Pad9}} -%\@namedef{libertine@key@Pad@10}{\BiolinumKeyGlyph{"E1AA}} -%\@namedef{libertine@key@Pad@11}{\BiolinumKeyGlyph{"E1AB}} -%\@namedef{libertine@key@Pad@12}{\BiolinumKeyGlyph{"E1AC}} -%\@namedef{libertine@key@Pad@13}{\BiolinumKeyGlyph{"E1AD}} -%\@namedef{libertine@key@Pad@14}{\BiolinumKeyGlyph{"E1AE}} -\DeclareRobustCommand*\LKeyPad[1]{\@nameuse{libertine@key@Pad@#1}} -% -% Maus -% -\iffalse -\DeclareRobustCommand*\LMouseEmpty{\biolinumKeyGlyph{"E130}} -\DeclareRobustCommand*\LMouseN{\biolinumKeyGlyph{"E131}} -\DeclareRobustCommand*\LMouseL{\biolinumKeyGlyph{"E132}} -\DeclareRobustCommand*\LMouseM{\biolinumKeyGlyph{"E133}} -\DeclareRobustCommand*\LMouseR{\biolinumKeyGlyph{"E134}} -\DeclareRobustCommand*\LMouseLR{\biolinumKeyGlyph{"E135}} -\DeclareRobustCommand*\LMouseIIEmpty{\biolinumKeyGlyph{"E138}} -\DeclareRobustCommand*\LMouseIIN{\biolinumKeyGlyph{"E139}} -\DeclareRobustCommand*\LMouseIIL{\biolinumKeyGlyph{"E13A}} -\DeclareRobustCommand*\LMouseIIR{\biolinumKeyGlyph{"E13C}} -\DeclareRobustCommand*\LMouseIILR{\biolinumKeyGlyph{"E13D}} -\fi -% + for k, v := range circleMap { + content = strings.Replace(content, k, v, -1) + } -*/ + return content +} func replaceKbd(content string) string { @@ -165,9 +89,7 @@ func replaceKbd(content string) string { if !ok { runes := []rune(result["key"]) for _, k := range runes { - h := strings.ToUpper(fmt.Sprintf("%x", k)) - h = "uni00" + h - + h := strings.ToUpper(fmt.Sprintf("uni%04x", k)) r += fmt.Sprintf("\\biolinumKeyGlyph{%s}", h) } } @@ -177,24 +99,5 @@ func replaceKbd(content string) string { } return content - // result := make(map[string]string) - // for i, name := range todoRegEx.SubexpNames() { - // if i != 0 && name != "" { - // result[name] = match[i] - // } - // } - // - // if filepath.IsAbs(result["path"]) { - // continue - // } - // - // - // - // - - // - // - //map[string]string{ - - //} + } diff --git a/application/source/document/pandoc.go b/application/source/document/pandoc.go index ad421c2929acfd11c76216902b8d9c9ccdc28348..3f933d488d124546c6e2ea3e2edf5a929c4f4102 100644 --- a/application/source/document/pandoc.go +++ b/application/source/document/pandoc.go @@ -3,10 +3,61 @@ package document import ( "fmt" "gitlab.schukai.com/oss/utilities/documentation-manager/environment" + "io/ioutil" + "os" "os/exec" ) -func runPandoc(source string, outputName string, latexPath string, luaFilter string) error { +func runInlinePandoc(source string) string { + + tmpSource, err := ioutil.TempFile(os.TempDir(), "inline-source") + if err != nil { + environment.ExitWithError(2, "A temporary file cannot be created", err.Error()) + } + + defer os.Remove(tmpSource.Name()) + + tmpResult, err := ioutil.TempFile(os.TempDir(), "inline-result") + if err != nil { + environment.ExitWithError(2, "A temporary file cannot be created", err.Error()) + } + + defer os.Remove(tmpResult.Name()) + + tmpSource.WriteString(source) + + arguments := "/bin/env pandoc " + + arguments += "--variable fontsize=12pt " + arguments += "--variable version=2.0 " + arguments += tmpSource.Name() + " " + arguments += "--from markdown " + arguments += "--to latex " + arguments += "--output=" + tmpResult.Name() + " " + + cmd := exec.Command("bash", "-c", arguments) + _, err = cmd.Output() + + if err != nil { + + exitErr, ok := err.(*exec.ExitError) + if ok { + environment.ExitWithError(2, string(exitErr.Stderr)) + + } else { + environment.ExitWithError(2, err.Error()) + } + + } + + content, err := os.ReadFile(tmpResult.Name()) + checkError(err) + + return string(content) + +} + +func runPandoc(source string, outputName string, latexPath string, luaFilter string, verbose bool) error { arguments := "/bin/env pandoc " @@ -18,6 +69,9 @@ func runPandoc(source string, outputName string, latexPath string, luaFilter str arguments += "--pdf-engine=xelatex " arguments += "--from markdown " arguments += "--to pdf " + if verbose { + arguments += "--verbose " + } arguments += "--listings " if latexPath != "" { @@ -33,12 +87,15 @@ func runPandoc(source string, outputName string, latexPath string, luaFilter str arguments += "--toc " arguments += "--output=" + outputName + " " - fmt.Println(arguments) + cmd := exec.Command("bash", "-c", arguments) + log, err := cmd.Output() - cmd, err := exec.Command("bash", "-c", arguments).Output() - fmt.Println(string(cmd)) if err != nil { + if verbose { + fmt.Println(string(log)) + } + exitErr, ok := err.(*exec.ExitError) if ok { environment.ExitWithError(2, string(exitErr.Stderr)) diff --git a/application/source/document/pdf.go b/application/source/document/pdf.go index 6b59e149ec92c4af77598905832ae543807448f0..c38368045bb35262ec025a8fa0929a4bd6a82f1b 100644 --- a/application/source/document/pdf.go +++ b/application/source/document/pdf.go @@ -3,7 +3,9 @@ package document import ( "fmt" "gitlab.schukai.com/oss/utilities/documentation-manager/environment" + "gitlab.schukai.com/oss/utilities/documentation-manager/utils" "io/ioutil" + "net/url" "os" "path" "path/filepath" @@ -16,6 +18,7 @@ type BuildPdfEnvironment struct { SourcePath string DateFormat string OutputPath string + Verbose bool Templates struct { Latex string Markdown string @@ -41,7 +44,9 @@ func NewPdfDataset(env BuildPdfEnvironment) (*PdfDataset, error) { for _, key := range keys { text := mapFiles[key].textMeta.text - text = convertImages(text, env.SourcePath) + text = convertHeadlines(text, mapFiles[key].level) + text = convertAwesomeBoxes(text) + text = convertImages(text, mapFiles[key].baseDir) text = convertCircledNumbers(text) text = replaceKbd(text) text = replaceRelativeLinks(text, mapFiles[key].hash, mapFiles) @@ -51,7 +56,9 @@ func NewPdfDataset(env BuildPdfEnvironment) (*PdfDataset, error) { d.Documents = strings.Join(docs, "\n") - fmt.Println(d.Documents) + if env.Verbose { + fmt.Println(d.Documents) + } return d, nil } @@ -99,7 +106,11 @@ func BuildPDF(env BuildPdfEnvironment) error { } }() - runPandoc(file.Name(), output, env.Templates.Latex, luaFilter.Name()) + c, _ := os.ReadFile(file.Name()) + + os.WriteFile("/tmp/debug.txt", c, 0644) + + runPandoc(file.Name(), output, env.Templates.Latex, luaFilter.Name(), env.Verbose) return nil } @@ -128,6 +139,10 @@ func replaceRelativeLinks(content, hash string, fileMap SourceFileMap) string { } s := fileMap.findByRelativePath(result["path"]) + if s == nil { + continue + } + replace := "\\hyperlink{link_" + s.hash + "}{" + result["label"] + "}" content = strings.Replace(content, result["match"], replace, -1) @@ -138,15 +153,15 @@ func replaceRelativeLinks(content, hash string, fileMap SourceFileMap) string { func createLuaFile() *os.File { - tmp, err := ioutil.TempFile(os.TempDir(), "lua-raw-block") + tmp, err := ioutil.TempFile(os.TempDir(), "lua-filter") if err != nil { environment.ExitWithError(2, "A temporary file cannot be created", err.Error()) } tmp.WriteString(` function RawBlock (raw) - return raw.format:match 'html' - and pandoc.read(raw.text, 'html').blocks + return raw.format:match "html" + and pandoc.read(raw.text, "html").blocks or raw end `) @@ -155,125 +170,261 @@ end } -func convertCircledNumbers(content string) string { - - circleMap := map[string]string{ - "➊": "\\large\\libertineGlyph{uni2776}\\normalsize", - "➋": "\\large\\libertineGlyph{uni2777}\\normalsize", - "➌": "\\large\\libertineGlyph{uni2778}\\normalsize", - "➍": "\\large\\libertineGlyph{uni2779}\\normalsize", - "➎": "\\large\\libertineGlyph{uni277A}\\normalsize", - "➏": "\\large\\libertineGlyph{uni277B}\\normalsize", - "➐": "\\large\\libertineGlyph{uni277C}\\normalsize", - "➑": "\\large\\libertineGlyph{uni277D}\\normalsize", - "➒": "\\large\\libertineGlyph{uni277E}\\normalsize", - "➓": "\\large\\libertineGlyph{uni277F}\\normalsize", - "⓫": "\\large\\libertineGlyph{uni24EB}\\normalsize", - "⓬": "\\large\\libertineGlyph{uni24EC}\\normalsize", - "⓭": "\\large\\libertineGlyph{uni24ED}\\normalsize", - "⓮": "\\large\\libertineGlyph{uni24EE}\\normalsize", - "⓯": "\\large\\libertineGlyph{uni24EF}\\normalsize", - "⓰": "\\large\\libertineGlyph{uni24F0}\\normalsize", - "⓱": "\\large\\libertineGlyph{uni24F1}\\normalsize", - "⓲": "\\large\\libertineGlyph{uni24F2}\\normalsize", - "⓳": "\\large\\libertineGlyph{uni24F3}\\normalsize", - "⓴": "\\large\\libertineGlyph{uni24F4}\\normalsize", - } - - for k, v := range circleMap { - content = strings.Replace(content, k, v, -1) - } +func isUrl(str string) bool { + u, err := url.Parse(str) + return err == nil && u.Scheme != "" && u.Host != "" +} - return content +type foundedTitleInfoStruct struct { + level int + match string + title string } -func convertImages(content string, absolute string) string { +type foundedTitleInfo []foundedTitleInfoStruct - todoRegEx := regexp.MustCompile(`(?P<match>\!\[(?P<label>[^]]*)\]\((?P<path>[^)]*)\))`) +func convertHeadlines(content string, level int) string { - matches := todoRegEx.FindAllStringSubmatch(content, -1) + regEx := regexp.MustCompile(`(?m)^(?P<match>(?P<level>#+)\s+(?P<title>[^\n]*))`) + matches := regEx.FindAllStringSubmatch(content, -1) if matches == nil { return content } + info := foundedTitleInfo{} + smallestLevelInTheDoc := 999 + for _, match := range matches { result := make(map[string]string) - for i, name := range todoRegEx.SubexpNames() { + for i, name := range regEx.SubexpNames() { if i != 0 && name != "" { result[name] = match[i] } } - if filepath.IsAbs(result["path"]) { - continue + info = append(info, foundedTitleInfoStruct{ + level: len(result["level"]), + match: result["match"], + title: result["title"], + }) + + if len(result["level"]) < smallestLevelInTheDoc { + smallestLevelInTheDoc = len(result["level"]) } - path := path.Clean(absolute + "/" + result["path"]) - content = strings.Replace(content, result["match"], "!["+result["label"]+"]("+path+")", -1) + } + + d := level - smallestLevelInTheDoc + if d < 0 { + d = 0 + } + for _, i := range info { + fill := strings.Repeat("#", d+i.level) + nw := fill + " " + i.title + "\n" + content = strings.Replace(content, i.match, nw, 1) } return content } -func convertTemplateLogo(content string, absolute string) string { - todoRegEx := regexp.MustCompile(`(?m)^(?P<match>logo:\s*"?(?P<path>[^"\n]*)"?\s*)$`) +// https://ftp.gwdg.de/pub/ctan/graphics/awesomebox/awesomebox.pdf +func convertAwesomeBoxes(content string) string { + + regEx := regexp.MustCompile(`(?m)(?P<matches>!!!\s*(?P<type>[^\s]+)\s+?(?P<title>[^\n]*)\n(?P<lines>(?P<lastline>[[:blank:]]+[^\n]+\n)+))`) - matches := todoRegEx.FindAllStringSubmatch(content, -1) + matches := regEx.FindAllStringSubmatch(content, -1) if matches == nil { return content } for _, match := range matches { result := make(map[string]string) - for i, name := range todoRegEx.SubexpNames() { + for i, name := range regEx.SubexpNames() { if i != 0 && name != "" { result[name] = match[i] } } - if filepath.IsAbs(result["path"]) { - continue + boxtype := "note" + + switch { + case utils.Contains([]string{"notebox", "note", "info"}, result["type"]): + boxtype = "note" + case utils.Contains([]string{"tipbox", "tip", "hint"}, result["type"]): + boxtype = "tip" + + case utils.Contains([]string{"warningbox", "warning", "warn"}, result["type"]): + boxtype = "warning" + + case utils.Contains([]string{"cautionbox", "caution", "danger"}, result["type"]): + boxtype = "caution" + + case utils.Contains([]string{"importantbox", "important"}, result["type"]): + boxtype = "important" } - path := path.Clean(absolute + "/" + result["path"]) - content = strings.Replace(content, result["match"], "logo: \""+path+"\"", -1) + c := "" + + t := escapeLatexSpecialChars(result["title"]) + if t != "" { + c += "\\textbf{" + utils.TrimQuotes(t) + "}\n" + } + + lines := result["lines"] + lines = convertAwesomeBoxesMarkdown(lines) + + c += "\n" + lines + "\n" //+escapeLatexSpecialChars(result["lastline"])) + "\n" + awesomebox := `\begin{` + boxtype + `block}` + c + "\n" + `\end{` + boxtype + `block}` + + content = strings.Replace(content, result["matches"], "\n"+awesomebox+"\n", 1) } return content + } -func convertTemplateLatexLogo(content string, absolute string) string { - todoRegEx := regexp.MustCompile(`(?m)(?P<match>\\includegraphics[^{]*\{(?P<path>[^}]*)\})`) +func convertAwesomeBoxesMarkdown(content string) string { + output := runInlinePandoc(escapeLatexSpecialChars(utils.TrimLines(content))) + return output +} - matches := todoRegEx.FindAllStringSubmatch(content, -1) +// The following characters play a special role in LaTeX and are called special printing characters, or simply special characters. +// +// # $ % & ~ _ ^ \ { } +// +// Whenever you put one of these special characters into your file, you are doing something special. If you simply want the character +// to be printed just as any other letter, include a \ in front of the character. For example, \$ will produce $ in your output. +// +// The exception to the rule is the \ itself because \\ has its own special meaning. A \ is produced by typing $\backslash$ in your file. +// +// The meaning of these characters are: +// +// ~ (tilde) unbreakable space, use it whenever you want to leave a space which is unbreakable, and cannot expand or shrink, as e.q. in names: A.~U.~Thor. +// $ (dollar sign) to start an finish math mode. +// _ (underscore) for subscripts in math mode. +// ^ (hat) for superscripts in math mode. +// \ (backslash) starting commands, which extend until the first non-alphanumerical character. The space following the command is swallowed. The following line results in what expected: +// The \TeX nician is an expert in \TeX{} language. +// {} (curly brackets) to group and separate commands from its surroundings. Must appear in pairs. +func escapeLatexSpecialChars(content string) string { + + result := "" + + runes := []rune(content) + for i, k := range runes { + + if k == '\\' { + result += "\\textbackslash{}" + continue + } else if utils.Contains([]string{"#", "$", "%", "&", "~", "_", "^", "\\", "{", "}"}, string(k)) { + if i == 0 || runes[i-1] != '\\' { + result += "\\" + } + + } + + result += string(k) + + } + + return result + +} + +func convertImages(content string, absolute string) string { + + regEx := regexp.MustCompile(`(?P<match>\!\[(?P<label>[^]]*)\]\((?P<path>[^)]*)\))`) + + matches := regEx.FindAllStringSubmatch(content, -1) if matches == nil { return content } for _, match := range matches { result := make(map[string]string) - for i, name := range todoRegEx.SubexpNames() { + for i, name := range regEx.SubexpNames() { if i != 0 && name != "" { result[name] = match[i] } } - if filepath.IsAbs(result["path"]) { + if isUrl(result["path"]) { continue } - path := path.Clean(absolute + "/" + result["path"]) - a := strings.Replace(result["match"], result["path"], path, -1) + if filepath.IsAbs(result["path"]) { + continue + } - content = strings.Replace(content, result["match"], a, -1) + p := filepath.Join(absolute, result["path"]) + path := path.Clean(p) + content = strings.Replace(content, result["match"], "!["+result["label"]+"]("+path+")", -1) } return content } +//func convertTemplateLogo(content string, absolute string) string { +// todoRegEx := regexp.MustCompile(`(?m)^(?P<match>logo:\s*"?(?P<path>[^"\n]*)"?\s*)$`) +// +// matches := todoRegEx.FindAllStringSubmatch(content, -1) +// if matches == nil { +// return content +// } +// +// for _, match := range matches { +// result := make(map[string]string) +// for i, name := range todoRegEx.SubexpNames() { +// if i != 0 && name != "" { +// result[name] = match[i] +// } +// } +// +// if filepath.IsAbs(result["path"]) { +// continue +// } +// +// path := path.Clean(absolute + "/" + result["path"]) +// content = strings.Replace(content, result["match"], "logo: \""+path+"\"", -1) +// +// } +// +// return content +//} + +//func convertTemplateLatexLogo(content string, absolute string) string { +// todoRegEx := regexp.MustCompile(`(?m)(?P<match>\\includegraphics[^{]*\{(?P<path>[^}]*)\})`) +// +// matches := todoRegEx.FindAllStringSubmatch(content, -1) +// if matches == nil { +// return content +// } +// +// for _, match := range matches { +// result := make(map[string]string) +// for i, name := range todoRegEx.SubexpNames() { +// if i != 0 && name != "" { +// result[name] = match[i] +// } +// } +// +// if filepath.IsAbs(result["path"]) { +// continue +// } +// +// path := path.Clean(absolute + "/" + result["path"]) +// a := strings.Replace(result["match"], result["path"], path, -1) +// +// content = strings.Replace(content, result["match"], a, -1) +// +// } +// +// return content +//} + // //func convertTemplateImages(content string, absolute string) string { // content = convertTemplateLogo(content, absolute) diff --git a/application/source/document/source.go b/application/source/document/source.go index eda6734680da3594c164b745efb5d26396d58f4b..a5d96a709f8eba6fdd60c8f5cee3bc3d9630c8dc 100644 --- a/application/source/document/source.go +++ b/application/source/document/source.go @@ -5,16 +5,19 @@ import ( "fmt" "os" "path/filepath" + "strings" ) type SourceFile struct { //dateFormat string + baseDir string absSourcePath string relSourcePath string //targetPath string fileInfo os.FileInfo textMeta textMetaStruct hash string + level int } func NewDefinition(root string, sourcePath string, info os.FileInfo, textMeta textMetaStruct) *SourceFile { @@ -29,6 +32,14 @@ func NewDefinition(root string, sourcePath string, info os.FileInfo, textMeta te } s.hash = fmt.Sprintf("%x", md5.Sum([]byte(r))) + s.baseDir = filepath.Dir(sourcePath) + + d, _ := filepath.Split(s.relSourcePath) + if d != "" { + s.level = len(strings.Split(d, "/")) + 1 + } else { + s.level = 1 + } return s } diff --git a/application/source/document/yaml.go b/application/source/document/yaml.go index 743333434f6617875d8af2a408c8ad22e70902ec..2c29067274fecbf55ef7c631ca3df7fb3b88eb97 100644 --- a/application/source/document/yaml.go +++ b/application/source/document/yaml.go @@ -47,41 +47,28 @@ func evaluateDocumentContent(content []byte) (error, textMetaStruct) { meta := "" text := "" - before, remaining, found := strings.Cut(origin, "---") + before, remaining, found := strings.Cut(origin, "\n---\n") if !found { req := textMetaStruct{} req.text = origin - - return nil, req - //t := strings.TrimSpace(origin) - //if len(t) == 0 { - // return errors.New("the file is empty"), textMetaStruct{ - // text: origin, - // meta: document{}, - // } - //} - // - //return errors.New("the file does not contain a definition block"), textMetaStruct{ - // text: origin, - // meta: document{}, - //} + return nil, req } text += before - a, b, found := strings.Cut(remaining, "\n...") + a, b, found := strings.Cut(remaining, "\n...\n") if !found { - a, b, found = strings.Cut(remaining, "\n---") + a, b, found = strings.Cut(remaining, "\n---\n") if !found { - a, b, found = strings.Cut(remaining, "...") + a, b, found = strings.Cut(remaining, "\n...\n") if !found { - a, b, found = strings.Cut(remaining, "---") + a, b, found = strings.Cut(remaining, "\n---\n") if !found { return errors.New("the file does not contain a definition block"), textMetaStruct{} diff --git a/application/source/utils/array.go b/application/source/utils/array.go new file mode 100644 index 0000000000000000000000000000000000000000..67dc274d10e98e591fb5d7712ea9d73ab337698c --- /dev/null +++ b/application/source/utils/array.go @@ -0,0 +1,11 @@ +package utils + +func Contains(s []string, str string) bool { + for _, v := range s { + if v == str { + return true + } + } + + return false +} diff --git a/application/source/utils/strings.go b/application/source/utils/strings.go new file mode 100644 index 0000000000000000000000000000000000000000..8edf317b973efdf2a9cd6908addfde078e1a74c8 --- /dev/null +++ b/application/source/utils/strings.go @@ -0,0 +1,36 @@ +package utils + +import "strings" + +func TrimLines(s string) string { + + flag := false + l := strings.Split(s, "\n") + r := []string{} + for _, v := range l { + m := strings.TrimSpace(v) + if m == "" { + flag = true + continue + } + + if m != "" && flag { + m += "\n" + } + + flag = false + r = append(r, m) + } + rs := strings.Join(r, "\n") + + return rs +} + +func TrimQuotes(s string) string { + if len(s) >= 2 { + if s[0] == '"' && s[len(s)-1] == '"' { + return s[1 : len(s)-1] + } + } + return s +} diff --git a/development/examples/example1/out/my.pdf b/development/examples/example1/out/my.pdf index 68296146c964b9293e9a9e8d2642e976bbb06591..1cae3aa7c5f7bf7c12a43d7de2b2071a0ffd7af9 100644 Binary files a/development/examples/example1/out/my.pdf and b/development/examples/example1/out/my.pdf differ diff --git a/development/examples/example1/templates/my.latex b/development/examples/example1/templates/my.latex index e79c80882ce8a18d0f5ff9ad7a341b03faadd845..2b5e2715e0e643ff22f16867658c2c682fafdb1f 100644 --- a/development/examples/example1/templates/my.latex +++ b/development/examples/example1/templates/my.latex @@ -887,6 +887,8 @@ $endif$ \node[shape=circle,draw,text=white,fill=black,inner sep=2pt] (char) {#1};}} \usepackage{libertine} +\usepackage{awesomebox} +\usepackage{dirtree} diff --git a/development/examples/example2/newdoc1.md b/development/examples/example2/newdoc1.md index 6e99f3fd6d3917747792afb0483f57e3248c3de3..79876534d196107dccab8ec280be321ff48eac36 100644 --- a/development/examples/example2/newdoc1.md +++ b/development/examples/example2/newdoc1.md @@ -24,6 +24,21 @@ Last Update: 06.07.2022 Lorem ipsum dolor sit amet, consectetur [read sub](sub1/doc1.md) adipiscing elit. + + + +!!! note "This is a note" + A-Lorem ipsum dolor sit amet, consectetur adipiscing elit. + B-Lorem ipsum dolor sit amet, consectetur adipiscing elit. + Die Escape-Sequenz gilt für: \, `, *, _, {, }, [, ], (, ), #, +, -, ., ! + D-Lorem ipsum dolor sit amet, consectetur adipiscing elit. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit. + +`text in html`{=html} +`text in pdf`{=latex} + + -- HEUTE: <kbd>Ctrl</kbd>+<kbd>ShifT</kbd><kbd>AUTO123$_öäüP</kbd> -- diff --git a/development/temp/changelog.go b/development/temp/changelog.go deleted file mode 100644 index fd4fa6f3c21c7ed941c80e85ff4e893bf49ad6ff..0000000000000000000000000000000000000000 --- a/development/temp/changelog.go +++ /dev/null @@ -1,75 +0,0 @@ -package main - -import ( - "fmt" - "sort" - "strings" -) - -type logs []string - -func buildChangelog(pageMap map[string]*documentContent) (error, string, bool) { - - changelog := make(map[string]logs) - datemap := make(map[string]string) - - for _, q := range pageMap { - s := *q - p := s.meta - - title := strings.Trim(p.Title, " ") - if title == "" { - title = printer.Sprintf("Untitled") - } - - e := title - - d := p.Created.String() - datemap[d] = p.Created.Format(config.Locale.DateFormat) - if p.Created.Before(p.LastUpdate) { - d = p.LastUpdate.String() - datemap[d] = p.LastUpdate.Format(config.Locale.DateFormat) - } - - changelog[d] = append(changelog[d], e) - - } - - hasChangelog := false - result := "" - - var sortedKeys sort.StringSlice - for k := range changelog { - sortedKeys = append(sortedKeys, k) - } - - sort.Sort(sort.Reverse(sortedKeys)) - - for _, k := range sortedKeys { - - result += "### " + datemap[k] + "\n\n" - - for _, m := range changelog[k] { - result += "- " + m + "\n" - } - - result += "\n" - hasChangelog = true - - } - - return nil, result, hasChangelog - -} - -func printChangelog() error { - - err, pageData := collectStructureFromFiles(config.Path) - if err != nil { - return err - } - - err, table, _ := buildChangelog(pageData) - fmt.Println(table) - return err -} diff --git a/development/temp/commandline.go b/development/temp/commandline.go deleted file mode 100644 index e5cb29ab2e98b297a8b4e35b1665f93d5ddee239..0000000000000000000000000000000000000000 --- a/development/temp/commandline.go +++ /dev/null @@ -1,143 +0,0 @@ -package main - -import ( - "os" - "path" - "path/filepath" - - "github.com/jessevdk/go-flags" -) - -var ( - config *Configuration - arguments *commandLineOptions -) - -func executeCommand() { - - arguments = new(commandLineOptions) - p := flags.NewParser(arguments, flags.Default) - _, err := p.Parse() - if err != nil { - os.Exit(-1) - } - - config = NewConfiguration(arguments.Config) - - if arguments.Path != "" { - config.Path = arguments.Path - } - - arguments.Path, err = filepath.Abs(arguments.Path) - if err != nil { - printErrorAndExit(2, "Unknown Error", err.Error()) - } - - if arguments.DateFormat != "" { - config.Locale.DateFormat = arguments.DateFormat - } - - activeCommand := p.Command.Active - - if activeCommand == nil { - return - } - - switch activeCommand.Name { - - case "version": - printInfo("Version " + version + " (" + build + ")") - case "changelog": - subcommand := activeCommand.Active - switch subcommand.Name { - case "print": - err := printChangelog() - if err != nil { - printErrorAndExit(2, err.Error()) - } - } - - case "tasks": - subcommand := activeCommand.Active - switch subcommand.Name { - case "print": - err := printTaskTable() - if err != nil { - printErrorAndExit(2, err.Error()) - } - } - - case "add": - - if config.Document.Template.Add == "" { - config.Document.Template.Add = defaultNewDocumentTemplate - } - - err := addDocument() - if err != nil { - printErrorAndExit(2, err.Error()) - } - - case "build": - - subcommand := activeCommand.Active - switch subcommand.Name { - case "html": - - if arguments.Build.HTML.TemplatePath != "" { - config.HTML.Template.Internal.HTMLContent = readTemplate(arguments.Build.HTML.TemplatePath) - } else if config.HTML.Template.HTML != "" { - config.HTML.Template.Internal.HTMLContent = readTemplate(config.HTML.Template.HTML) - } else { - config.HTML.Template.Internal.HTMLContent = defaultHTMLTemplate - } - - err := createHTML() - if err != nil { - printErrorAndExit(2, err.Error()) - } - - case "pdf": - - if arguments.Build.PDF.TemplatePath != "" { - config.PDF.Template.Internal.MarkdownContent = readTemplate(arguments.Build.PDF.TemplatePath) - } else if config.PDF.Template.Markdown != "" { - config.PDF.Template.Internal.MarkdownContent = readTemplate(config.PDF.Template.Markdown) - } else { - config.PDF.Template.Internal.MarkdownContent = defaultPDFTemplate - } - - if arguments.Build.PDF.LatexTemplatePath != "" { - config.PDF.Template.Latex = arguments.Build.PDF.LatexTemplatePath - } - - err := CreatePDF() - if err != nil { - printErrorAndExit(2, err.Error()) - } - } - - } -} - -func readTemplate(argPath string) string { - - current, err := os.Getwd() - if err != nil { - printErrorAndExit(2, "Unknown Error", err.Error()) - } - - p := argPath - - if !filepath.IsAbs(p) { - p = path.Clean(current + "/" + p) - } - - template, err := os.ReadFile(p) - if err != nil { - printErrorAndExit(exitCodeCatchAll, - "the specified template", argPath) - } - - return convertTemplateImages(string(template), path.Dir(p)) -} diff --git a/development/temp/commandlineoptions.go b/development/temp/commandlineoptions.go deleted file mode 100644 index 4baf8620c5266b89e107d2e95b6cfc36284e5a30..0000000000000000000000000000000000000000 --- a/development/temp/commandlineoptions.go +++ /dev/null @@ -1,31 +0,0 @@ -package main - -type commandLineOptions struct { - Config string `short:"c" long:"config" description:"file with configuration values"` - Path string `short:"p" long:"path" description:"define the path where the specifications are located" default:"."` - DateFormat string `long:"date-format" description:"date format" default:"2006-01-02"` - Add struct { - Name string `short:"n" long:"name" description:"name of the document"` - } `command:"add"` - Tasks struct { - Print struct { - } `command:"print"` - } `command:"tasks"` - Changelog struct { - Print struct { - } `command:"print"` - } `command:"changelog"` - Build struct { - PDF struct { - TemplatePath string `short:"t" long:"template" description:"file name of the template of the pages"` - Output string `short:"o" long:"output" description:"output file name"` - LatexTemplatePath string `short:"l" long:"latex-template-path" description:"latex template"` - } `command:"pdf"` - HTML struct { - TemplatePath string `short:"t" long:"template" description:"file name of the template of a html page"` - Output string `short:"o" long:"output" description:"output directory"` - } `command:"html"` - } `command:"build"` - Version struct { - } `command:"version"` -} diff --git a/development/temp/config.go b/development/temp/config.go deleted file mode 100644 index 253813191496570ff54df8aedbe8bce6e6666b30..0000000000000000000000000000000000000000 --- a/development/temp/config.go +++ /dev/null @@ -1,113 +0,0 @@ -package main - -import ( - "os" - "os/user" - "path" -) - -type configPathsStruct struct { - argument string - current string - user string - main string -} - -var ( - configPaths = configPathsStruct{ - main: "/etc/documentations-manager/config.yaml", - } -) - -// Configuration -type Configuration struct { - Path string `yaml:"SourcePath"` - - Locale struct { - DateFormat string `yaml:"DateFormat"` - } `yaml:"Locale"` - - PDF struct { - Template struct { - Markdown string `yaml:"Markdown"` - Latex string `yaml:"Latex"` - Internal struct { - MarkdownContent string - } `yaml:"-"` - } `yaml:"Template"` - } `yaml:"PDF"` - - HTML struct { - Template struct { - HTML string `yaml:"HTML"` - Internal struct { - HTMLContent string - } `yaml:"-"` - } `yaml:"Template"` - } - - Document struct { - Template struct { - Add string `yaml:"Add"` - } `yaml:"Template"` - } `yaml:"Document"` -} - -func fileExists(name string) (found bool) { - if f, err := os.Stat(name); err == nil { - return f.Mode().IsRegular() - } - return false -} - -// NewConfiguration -func NewConfiguration(argConfigPath string) *Configuration { - - current, err := os.Getwd() - if err != nil { - panic(err) - } - - filename := path.Clean(current + "/config.yaml") - - cfg := Configuration{} - - usr, err := user.Current() - if err == nil { - configPaths.user = usr.HomeDir + "/.config/documentations-manager/config.yaml" - } - - if argConfigPath != "" { - configPaths.argument = argConfigPath - } - - switch { - case fileExists(configPaths.argument): - readFile(&cfg, configPaths.argument) - case fileExists(filename): - readFile(&cfg, filename) - case fileExists(current): - readFile(&cfg, current) - case fileExists(configPaths.user): - readFile(&cfg, configPaths.user) - case fileExists(configPaths.main): - readFile(&cfg, configPaths.main) - } - - return &cfg -} - -func readFile(cfg *Configuration, filename string) { - f, err := os.Open(filename) - if err != nil { - printErrorAndExit(2, err.Error()) - } - - defer f.Close() - - decoder := yaml.NewDecoder(f) - err = decoder.Decode(cfg) - if err != nil { - printErrorAndExit(2, err.Error()) - } -} diff --git a/development/temp/dataset.go b/development/temp/dataset.go deleted file mode 100644 index b1dbc32db497416d464bbead8692a2c9e1656cf2..0000000000000000000000000000000000000000 --- a/development/temp/dataset.go +++ /dev/null @@ -1,80 +0,0 @@ -package main - -import ( - "sort" - "time" -) - -type Dataset struct { - Config *Configuration - Meta string - HasMeta bool - HasChangelog bool - Changelog string - Tasks string - HasTasks bool - Documents string - HasDocuments bool - Created time.Time - CreatedFormat string - Description string - HasDescription bool - Title string - HasTitle bool - keys []string -} - -const flagInitChangelog = 1 -const flagInitTasks = 2 -const flagInitDocuments = 4 - -func getDataset(pageData map[string]*documentContent, flags int) (*Dataset, error) { - - keys := make([]string, 0, len(pageData)) - for k := range pageData { - keys = append(keys, k) - } - - sort.Strings(keys) - - d := &Dataset{} - - d.keys = keys - - if flags&flagInitChangelog != 0 { - err, changelog, hasChangelog := buildChangelog(pageData) - if err != nil { - return nil, err - } - d.Changelog = changelog - d.HasChangelog = hasChangelog - } - - if flags&flagInitTasks != 0 { - err, tasks, hasTasks := buildTasksTable(pageData, false) - if err != nil { - return nil, err - } - - d.Tasks = tasks - d.HasTasks = hasTasks - } - - if flags&flagInitDocuments != 0 { - documents := "" - - for _, k := range d.keys { - d.HasDocuments = true - p := pageData[k].meta - documents = documents + convertToHtml(getAdjustedContent(p.Absolute)) - } - - d.Documents = documents - } - - d.Config = config - d.Created = time.Now() - d.CreatedFormat = time.Now().Format(config.Locale.DateFormat) - - return d, nil -} diff --git a/development/temp/defaults.go b/development/temp/defaults.go deleted file mode 100644 index 6888cf4cb82635ad2c3e605ede74fbcab6334093..0000000000000000000000000000000000000000 --- a/development/temp/defaults.go +++ /dev/null @@ -1,69 +0,0 @@ -package main - -const exitOK = 0 - -const exitCodeCatchAll = 1 - -// language: html -const defaultHTMLTemplate = `<!DOCTYPE html> -{{ if .HasLanguage }} -<html lang="{{.Language}}"> -{{ else }} -<html> -{{ end }} -<head> - <meta charset="UTF-8"/> - <meta http-equiv="X-UA-Compatible" content="IE=edge"> - <meta name='viewport' - content='width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no'> - <title>{{.Title}}</title> - {{if .HasCanonical }} - <link rel="canonical" href="{{.Canonical}}"> - {{end}} - {{if .HasDescription }} - <meta name="description" content="{{.Description}}"> - {{end}} - - <style> - body { - padding: 0px; - margin: 0px; - } - </style> -</head> -<body> -{{ if .HasBody }}{{.Body}}{{ end }} -</body> -</html>` - -const defaultPDFTemplate = ` -<!DOCTYPE html> -<html lang="de"> -<head> - -<body> ---- -block-headings: true -... - -{{ if .HasChangelog }} -## Changelog - -{{.Changelog}} - -{{ end}}{{ if .HasTasks }} -## Tasks - -{{.Tasks}} - -{{ end}}{{ if .HasDocuments }} -## Documents - -{{.Documents}} - -{{ end}} - -</body> -</html> - -` diff --git a/development/temp/documents.go b/development/temp/documents.go deleted file mode 100644 index 0684d558536023aea3b53780100fc2536ad02195..0000000000000000000000000000000000000000 --- a/development/temp/documents.go +++ /dev/null @@ -1,54 +0,0 @@ -package main - -import ( - "os" - "path" - "strings" - "time" - - "gopkg.in/yaml.v3" -) - -type document struct { - ToDos []task `yaml:"-"` - Absolute string `yaml:"-"` - File string `yaml:"-"` - - // remember the node structure of yaml - OriginNode *yaml.Node `yaml:"-"` - OriginText string `yaml:"-"` - - Title string `yaml:"Title"` - Abbreviation string `yaml:"Abbreviation"` - References []string `yaml:"References"` - Keywords []string `yaml:"Keywords"` - Authors []string `yaml:"Authors"` - Version string `yaml:"Version"` - Created time.Time `yaml:"Created"` - LastUpdate time.Time `yaml:"Last Update"` - Language string `yaml:"Language"` -} - -func addDocument() error { - - if arguments.Add.Name == "" { - printErrorAndExit(2, "the name must not be empty") - } - - p := path.Join(arguments.Path, arguments.Add.Name+".md") - if fileExists(p) { - printErrorAndExit(2, "the document with name already exists", arguments.Add.Name, p) - } - - t := config.Document.Template.Add - t = strings.Replace(t, "%%ID%%", arguments.Add.Name, -1) - t = strings.Replace(t, "%%CREATED%%", time.Now().Format("2006-01-02"), -1) - - d1 := []byte(t) - err := os.WriteFile(p, d1, 0644) - if err != nil { - return err - } - - return nil -} diff --git a/development/temp/errors.go b/development/temp/errors.go deleted file mode 100644 index bef18c31dd09faca39acab6c9f0709fa7637c723..0000000000000000000000000000000000000000 --- a/development/temp/errors.go +++ /dev/null @@ -1,9 +0,0 @@ -package main - -import "errors" - -var ( - noGitRepositoryError = errors.New("no git repository found") - notFoundError = errors.New("not found") - notPermittedError = errors.New("not permitted") -) diff --git a/development/temp/files.go b/development/temp/files.go deleted file mode 100644 index afb290e3a3d68b9b0762637f6301d720b9fd349b..0000000000000000000000000000000000000000 --- a/development/temp/files.go +++ /dev/null @@ -1,97 +0,0 @@ -package main - -import ( - "bytes" - "io/ioutil" - "os" - "path" - "path/filepath" - "strings" - - "gopkg.in/yaml.v3" -) - -func collectStructureFromFiles(directory string) (error, map[string]*documentContent) { - - cleanedDirectory, err := filepath.Abs(path.Clean(directory)) - - pageMap := make(map[string]*documentContent) - - err = filepath.Walk(cleanedDirectory, - func(current string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - cleanedPath := path.Clean(current) - - if info.IsDir() { - return nil - } - - ext := filepath.Ext(cleanedPath) - if ext != ".md" { - return nil - } - - content, err := ioutil.ReadFile(cleanedPath) - - if err != nil { - return err - } - - err, p := splitYamlParts(content) - - if err != nil { - printWarning("the file contains structural errors. check the YAML and if necessary the template.", cleanedPath, err) - } - - bd := []byte(cleanedDirectory) - bp := []byte(cleanedPath) - bs := string(bp[len(bd):len(bp)]) - - p.meta.File = "." + bs - p.meta.Absolute = cleanedPath - p.meta.ToDos = findTask(content) - - k := cleanedPath - - pageMap[k] = &p - - return nil - }) - - return err, pageMap -} - -func rewriteFiles(pageData map[string]*document) error { - - for filename, pd := range pageData { - - buf := new(bytes.Buffer) - - var encoder = yaml.NewEncoder(buf) - err := encoder.Encode(pd.OriginNode) - if err != nil { - return err - } - encoder.Close() - - text := "---\n" + buf.String() + "\n...\n" - text += strings.Trim(pd.OriginText, "\n") - - info, err := os.Stat(filename) - if err != nil { - return err - } - mode := info.Mode() - - err = os.WriteFile(filename, []byte(text), mode) - if err != nil { - return err - } - - } - - return nil -} diff --git a/development/temp/html.go b/development/temp/html.go deleted file mode 100644 index 48686c6fa59a4d5df3cabe9f17e181ccef6aff7c..0000000000000000000000000000000000000000 --- a/development/temp/html.go +++ /dev/null @@ -1,149 +0,0 @@ -package main - -import ( - "fmt" - "github.com/gomarkdown/markdown" - "github.com/gomarkdown/markdown/html" - "github.com/gomarkdown/markdown/parser" - "html/template" - "os" - "path" - "time" -) - -type PageDataset struct { - Body string - HasBody bool - Created time.Time - CreatedFormat string - Updated time.Time - UpdatedFormat string - Language string - HasLanguage bool - - //Config *Configuration - //Meta string - //HasMeta bool - //HasChangelog bool - //Changelog string - //Tasks string - //HasTasks bool - //Documents string - //HasDocuments bool - //Language string - //HasLanguage bool - //Canonical string - //HasCanonical bool - //Description string - //HasDescription bool - //Title string - //HasTitle bool - //Body string - //HasBody bool - //keys []string -} - -func createHTML() error { - - err, pageData := collectStructureFromFiles(config.Path) - if err != nil { - return err - } - - dataset, err := getDataset(pageData, 0) - fmt.Println(dataset) - if err != nil { - return err - } - - for _, p := range pageData { - fmt.Println(p.text) - d := &PageDataset{ - //Created: p.created, - //CreatedFormat: p.createdFormat, - //Updated: p.updated, - //UpdatedFormat: p.updatedFormat, - //Language: p.language, - //HasLanguage: p.hasLanguage, - } - - fmt.Printf(d.CreatedFormat) - //initBody(d, pageData) - //err = createHTMLFile(p, d) - //if err != nil { - // return err - //} - } - - return nil -} - -//func initBody(d *Dataset, pageData map[string]*documentContent) { -// -// d.Body = "" -// d.HasBody = false -// -// for _, k := range d.keys { -// d.HasBody = true -// p := pageData[k].meta -// d.Body = d.Body + getAdjustedContent(p.Absolute) -// } -// -//} - -func convertToHtml(text string) string { - extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.Footnotes | parser.SuperSubscript | parser.Includes | parser.Attributes - parser := parser.NewWithExtensions(extensions) - - htmlFlags := html.CommonFlags | html.HrefTargetBlank - opts := html.RendererOptions{Flags: htmlFlags} - renderer := html.NewRenderer(opts) - - md := []byte(text) - return string(markdown.ToHTML(md, parser, renderer)) - -} - -func createHTMLFile(p *documentContent, d *PageDataset) error { - - if d.Body == "" { - d.HasBody = false - } else { - d.HasBody = true - } - - t, err := template.New("pdf").Parse(config.PDF.Template.Internal.MarkdownContent) - if err != nil { - return err - } - - outputDir := arguments.Build.HTML.Output - if outputDir == "" { - printErrorAndExit(2, "if the type is html, the output option must be specified") - } - - if !path.IsAbs(outputDir) { - outputDir = path.Join(config.Path, outputDir) - } - - fileinfo, err := os.Stat(outputDir) - if os.IsNotExist(err) { - printErrorAndExit(2, "The file %s does not exist", outputDir) - } - if !fileinfo.IsDir() { - printErrorAndExit(2, "The file %s is not a directory", outputDir) - } - - fileName := path.Join(outputDir, ".html") - file, err := os.Create(fileName) - if err != nil { - return err - } - - err = t.Execute(file, d) - if err != nil { - return err - } - - return nil -} diff --git a/development/temp/l10n.go b/development/temp/l10n.go deleted file mode 100644 index b42be045ed64e585b20313ef3ac473e403809f84..0000000000000000000000000000000000000000 --- a/development/temp/l10n.go +++ /dev/null @@ -1,461 +0,0 @@ -package main - -import ( - "golang.org/x/text/language" - "golang.org/x/text/message" - "golang.org/x/text/message/catalog" -) - -type l10nLocaleTranslation struct { - tag string - msg interface{} -} -type l10nKeyTranslation struct { - key string - tm []l10nLocaleTranslation -} - -var l10nMap = []l10nKeyTranslation{ - { - "does not contain a valid yaml definition", - []l10nLocaleTranslation{ - {"de", "\"%s\" enthält keine gültige yaml Struktur."}, - {"en", "\"%s\" does not contain a valid yaml definition."}, - }, - }, - { - "the specified template", - []l10nLocaleTranslation{ - {"de", "Das angegebene AddTemplate \"%s\" wurde nicht gefunden."}, - {"en", "the specified template \"%s\" was not found."}, - }, - }, - { - "the due of the todo cannot be parsed", - []l10nLocaleTranslation{ - {"de", "Das Datum %s der Aufgabe \"%s\" kann nicht verarbeitet werden."}, - {"en", "the due %s of the todo \"%s\" cannot be parsed."}, - }, - }, - { - "ToDo", - []l10nLocaleTranslation{ - {"de", "Aufgabe"}, - {"en", "task"}, - }, - }, - { - "Due", - []l10nLocaleTranslation{ - {"de", "Bis"}, - {"en", "due"}, - }, - }, { - "File", - []l10nLocaleTranslation{ - {"de", "Datei"}, - {"en", "file"}, - }, - }, { - "group", - []l10nLocaleTranslation{ - {"de", "Gruppe"}, - {"en", "group"}, - }, - }, - { - "Description", - []l10nLocaleTranslation{ - {"de", "Beschreibung"}, - {"en", "Description"}, - }, - }, - { - "Delivery until", - []l10nLocaleTranslation{ - {"de", "Bis"}, - {"en", "until"}, - }, - }, - { - "Provided on", - []l10nLocaleTranslation{ - {"de", "Am"}, - {"en", "on"}, - }, - }, - { - "Provided by", - []l10nLocaleTranslation{ - {"de", "Von"}, - {"en", "by"}, - }, - }, - { - "Type", - []l10nLocaleTranslation{ - {"de", "Typ"}, - {"en", "Type"}, - }, - }, - { - "Title", - []l10nLocaleTranslation{ - {"de", "Titel"}, - {"en", "Title"}, - }, - }, - { - "Alias", - []l10nLocaleTranslation{ - {"de", "Alias"}, - {"en", "Alias"}, - }, - }, - { - "Type", - []l10nLocaleTranslation{ - {"de", "Typ"}, - {"en", "Type"}, - }, - }, - { - "Keywords", - []l10nLocaleTranslation{ - {"de", "Schlüsselwörter"}, - {"en", "Keywords"}, - }, - }, { - "Estimation", - []l10nLocaleTranslation{ - {"de", "Schätzung"}, - {"en", "Estimation"}, - }, - }, { - "Source", - []l10nLocaleTranslation{ - {"de", "Quelle"}, - {"en", "Source"}, - }, - }, - { - "Author", - []l10nLocaleTranslation{ - {"de", "Autor"}, - {"en", "Author"}, - }, - }, - { - "Status", - []l10nLocaleTranslation{ - {"de", "Status"}, - {"en", "Status"}, - }, - }, - { - "Complexity", - []l10nLocaleTranslation{ - {"de", "Komplexität"}, - {"en", "Complexity"}, - }, - }, - { - "Difficulty", - []l10nLocaleTranslation{ - {"de", "Schwierigkeit"}, - {"en", "Difficulty"}, - }, - }, - { - "Priority", - []l10nLocaleTranslation{ - {"de", "Priorität"}, - {"en", "Priority"}, - }, - }, - { - "Version", - []l10nLocaleTranslation{ - {"de", "Version"}, - {"en", "Version"}, - }, - }, - { - "Milestone", - []l10nLocaleTranslation{ - {"de", "Meilenstein"}, - {"en", "Milestone"}, - }, - }, - { - "Created", - []l10nLocaleTranslation{ - {"de", "Erstellt am"}, - {"en", "Created"}, - }, - }, - { - "LastUpdate", - []l10nLocaleTranslation{ - {"de", "Letzte Änderung"}, - {"en", "LastUpdate"}, - }, - }, - { - "The file cannot be read", - []l10nLocaleTranslation{ - {"de", "Die Datei \"%s\" kann nicht eingelesen werden."}, - {"en", "The file \"%s\" cannot be read."}, - }, - }, - { - "The output file cannot be written", - []l10nLocaleTranslation{ - {"de", "Die Ausgabedatei kann nicht geschrieben werden (%s)."}, - {"en", "The output file cannot be written (%s)."}, - }, - }, { - "A temporary file cannot be created", - []l10nLocaleTranslation{ - {"de", "Es kann keine temporäre Datei angelegt werden (%s)."}, - {"en", "A temporary file cannot be created (%s)."}, - }, - }, { - "Checked", - []l10nLocaleTranslation{ - {"de", "Erledigt"}, - {"en", "done"}, - }, - }, - { - "Title", - []l10nLocaleTranslation{ - {"de", "Title"}, - {"en", "Title"}, - }, - }, - { - "Type", - []l10nLocaleTranslation{ - {"de", "Typ"}, - {"en", "Type"}, - }, - }, { - "RequirementID", - []l10nLocaleTranslation{ - {"de", "Documents"}, - {"en", "Anforderungen"}, - }, - }, { - "the id must not be empty", - []l10nLocaleTranslation{ - {"de", "Die ID muss angegeben werden"}, - {"en", "the id must not be empty"}, - }, - }, - { - "the document with name already exists", - []l10nLocaleTranslation{ - {"de", "Das Dokument mit dem Namen %s existiert bereits (%s)."}, - {"en", "The document named %s already exists (%s)."}, - }, - }, - { - "YAML parse error", - []l10nLocaleTranslation{ - {"de", "YAML Fehler: %s "}, - {"en", "YAML parse error: %s ."}, - }, - }, - { - "if the type is pdf, the output option must be specified", - []l10nLocaleTranslation{ - {"de", "Der Tpy PDF benötigt die Option --output"}, - {"en", "if the type is pdf, the output option must be specified"}, - }, - }, - { - "if the type is html, the output option must be specified", - []l10nLocaleTranslation{ - {"de", "Der Tpy HTML benötigt die Option --output"}, - {"en", "if the type is html, the output option must be specified"}, - }, - }, - { - "Unknown Error", - []l10nLocaleTranslation{ - {"de", "Unbekannter Fehler: %s"}, - {"en", "Unknown Error %s"}, - }, - }, - { - "the file contains structural errors. check the YAML and if necessary the template.", - []l10nLocaleTranslation{ - {"de", "Die Datei %s enthält strukturelle Fehler (%s). Überprüfen Sie die Datei."}, - {"en", "the file %s contains structural errors (%s). Check the if necessary the file."}, - }, - }, - { - "the file is empty", - []l10nLocaleTranslation{ - {"de", "Die Datei enthält keinen Inhalt."}, - {"en", "the file is empty."}, - }, - }, - { - "the grouping key was not found", - []l10nLocaleTranslation{ - {"de", "Der Schlüssel %s existiert nicht"}, - {"en", "the grouping key %s was not found"}, - }, - }, - - { - "Ratio", - []l10nLocaleTranslation{ - {"de", "Verhältnis"}, - {"en", "Ratio"}, - }, - }, - { - "Standard Deviation", - []l10nLocaleTranslation{ - {"de", "Standardabweichung"}, - {"en", "Standard Deviation"}, - }, - }, - { - "Variance", - []l10nLocaleTranslation{ - {"de", "Varianz"}, - {"en", "Variance"}, - }, - }, - { - "Average", - []l10nLocaleTranslation{ - {"de", "Mittelwert"}, - {"en", "Average"}, - }, - }, - { - "Time Spent", - []l10nLocaleTranslation{ - {"de", "Aufwand"}, - {"en", "Time Spent"}, - }, - }, - { - "Estimated", - []l10nLocaleTranslation{ - {"de", "Planwert"}, - {"en", "Estimated"}, - }, - }, - { - "unsuported type", - []l10nLocaleTranslation{ - {"de", "nicht unterstützter Typ %s"}, - {"en", "unsuported type %s"}, - }, - }, - { - "no value", - []l10nLocaleTranslation{ - {"de", "kein Wert"}, - {"en", "no value"}, - }, - }, - { - "opened", - []l10nLocaleTranslation{ - {"de", "offen"}, - {"en", "opened"}, - }, - }, - - { - "closed", - []l10nLocaleTranslation{ - {"de", "geschlossen"}, - {"en", "closed"}, - }, - }, - - { - "State", - []l10nLocaleTranslation{ - {"de", "Status"}, - {"en", "State"}, - }, - }, - { - "Due date", - []l10nLocaleTranslation{ - {"de", "Fälligkeitsdatum"}, - {"en", "Due Date"}, - }, - }, - - { - "Labels", - []l10nLocaleTranslation{ - {"de", "Labels"}, - {"en", "Labels"}, - }, - }, - - { - "Estimate", - []l10nLocaleTranslation{ - {"de", "Planwert"}, - {"en", "Estimate"}, - }, - }, - - { - "Spent", - []l10nLocaleTranslation{ - {"de", "Aufwand"}, - {"en", "Spent"}, - }, - }, - - { - "Assignees", - []l10nLocaleTranslation{ - {"de", "Zuweisungen"}, - {"en", "Assignees"}, - }, - }, { - "Statistics", - []l10nLocaleTranslation{ - {"de", "Statistiken"}, - {"en", "Statistics"}, - }, - }, - { - "Untitled", - []l10nLocaleTranslation{ - {"de", "Unbenannt"}, - {"en", "Untitled"}, - }, - }, -} - -func initL10n() { - for _, e := range l10nMap { - key := e.key - for _, f := range e.tm { - tag := language.MustParse(f.tag) - switch msg := f.msg.(type) { - case string: - message.SetString(tag, key, msg) - case catalog.Message: - message.Set(tag, key, msg) - case []catalog.Message: - message.Set(tag, key, msg...) - } - } - } -} diff --git a/development/temp/main.go b/development/temp/main.go deleted file mode 100644 index 98a67334419c517571019fcb0db21b9509baa78e..0000000000000000000000000000000000000000 --- a/development/temp/main.go +++ /dev/null @@ -1,50 +0,0 @@ -package main - -import ( - "os" - - "golang.org/x/text/language" - "golang.org/x/text/message" -) - -// Wird beim Bauen per -// -ldflags "-X main.version=$app_version -X main.build=$(due --iso-8601 | tr -d "-" )" -// gesetzt -var ( - version string = "dev" - build string = "dev" - printer *message.Printer - //sugar *zap.SugaredLogger -) - -var serverLangs = []language.Tag{ - language.English, // en fallback - language.German, // de -} - -var matcher = language.NewMatcher(serverLangs) - -var userPrefs = []language.Tag{ - language.Make(os.Getenv("LANG")), - language.Make("en"), -} - -func init() { - - initEnvironment() - - tag, _, _ := matcher.Match(userPrefs...) - printer = message.NewPrinter(tag) - - initL10n() - -} - -func initEnvironment() { -} - -/** - */ -func main() { - executeCommand() -} diff --git a/development/temp/message.go b/development/temp/message.go deleted file mode 100644 index 60f014a07443f6550e3590644597896a2e4ada80..0000000000000000000000000000000000000000 --- a/development/temp/message.go +++ /dev/null @@ -1,57 +0,0 @@ -package main - -import ( - "os" - - "github.com/gookit/color" -) - -var lastBlock string - -// print error -func printErrorAndExit(code int, message string, a ...interface{}) { - printError(message, a...) - if code == 0 { - code = exitCodeCatchAll - } - os.Exit(code) -} - -func printError(message string, a ...interface{}) { - - message = printer.Sprintf(message, a...) - - if lastBlock == "error" { - color.Error.Println(" " + message) - return - } - lastBlock = "error" - - color.Error.Block(message) -} - -func printWarning(message string, a ...interface{}) { - - message = printer.Sprintf(message, a...) - - if lastBlock == "warning" { - color.Warn.Println(" " + message) - return - } - lastBlock = "warning" - - color.Warn.Block(message) -} - -func printInfo(message string, a ...interface{}) { - - message = printer.Sprintf(message, a...) - - if lastBlock == "info" { - color.Info.Println(" " + message) - return - } - lastBlock = "info" - - color.Info.Block(message) -} diff --git a/development/temp/pandoc.go b/development/temp/pandoc.go deleted file mode 100644 index 83e501124cc97bf4eadde018d21d98981267d9a9..0000000000000000000000000000000000000000 --- a/development/temp/pandoc.go +++ /dev/null @@ -1,50 +0,0 @@ -package main - -import ( - "fmt" - "os/exec" -) - -func runPandoc(source string, outputName string, latexPath string) string { - - arguments := "/bin/env pandoc " - - arguments += "--number-sections " - arguments += "--variable \"geometry:a4paper,margin=2cm\" " - arguments += "--variable fontsize=12pt " - arguments += "--variable version=2.0 " - arguments += source + " " - arguments += "--pdf-engine=xelatex " - arguments += "--listings " - - if latexPath != "" { - arguments += "--template=" + latexPath + " " - } - - if luaRawBlockFile != nil { - arguments += "--lua-filter=" + luaRawBlockFile.Name() + " " - } - - arguments += "--columns=5 " - arguments += "--highlight-style espresso " - arguments += "--toc " - arguments += "--output=" + outputName + " " - - cmd, err := exec.Command("bash", "-c", arguments).Output() - if err != nil { - - exitErr, ok := err.(*exec.ExitError) - if ok { - printErrorAndExit(2, string(exitErr.Stderr)) - - } else { - printErrorAndExit(2, err.Error()) - } - - } - - fmt.Println(string(cmd)) - - return string(cmd) - -} diff --git a/development/temp/pdf.go b/development/temp/pdf.go deleted file mode 100644 index 0f3e3d66a9c6d0ee526912300635f5f0e0d01da9..0000000000000000000000000000000000000000 --- a/development/temp/pdf.go +++ /dev/null @@ -1,190 +0,0 @@ -package main - -import ( - "io/ioutil" - "os" - "path" - "path/filepath" - "regexp" - "strings" - "text/template" -) - -func CreatePDF() error { - - err, pageData := collectStructureFromFiles(config.Path) - if err != nil { - return err - } - - d, err := getDataset(pageData, flagInitChangelog|flagInitTasks|flagInitDocuments) - if err != nil { - return err - } - - outputName := arguments.Build.PDF.Output - - if outputName == "" { - printErrorAndExit(2, "if the type is pdf, the output option must be specified") - } - - file, err := ioutil.TempFile(os.TempDir(), "docman") - if err != nil { - printErrorAndExit(2, "A temporary file cannot be created", err.Error()) - } - defer os.Remove(file.Name()) - - t, err := template.New("pdf").Parse(config.PDF.Template.Internal.MarkdownContent) - if err != nil { - return err - } - - err = t.Execute(file, d) - if err != nil { - return err - } - - createLuaFile() - runPandoc(file.Name(), outputName, config.PDF.Template.Latex) - - if luaRawBlockFile != nil { - luaRawBlockFile.Close() - } - - return nil - -} - -var luaRawBlockFile *os.File - -func createLuaFile() { - var err error - - luaRawBlockFile, err = ioutil.TempFile(os.TempDir(), "lua-raw-block") - if err != nil { - printErrorAndExit(2, "A temporary file cannot be created", err.Error()) - } - - luaRawBlockFile.WriteString(` -function RawBlock (raw) - return raw.format:match 'html' - and pandoc.read(raw.text, 'html').blocks - or raw -end -`) - -} - -func getAdjustedContent(absolute string) string { - - content, err := os.ReadFile(absolute) - if err != nil { - printError("The file cannot be read", absolute) - return "" - } - - err, def := splitYamlParts(content) - if err != nil { - printError(err.Error()) - return "" - } - - s := convertImages(def.text, path.Dir(absolute)) - - return s -} - -func convertImages(content string, absolute string) string { - - todoRegEx := regexp.MustCompile(`(?P<match>\!\[(?P<label>[^]]*)\]\((?P<path>[^)]*)\))`) - - matches := todoRegEx.FindAllStringSubmatch(content, -1) - if matches == nil { - return content - } - - for _, match := range matches { - result := make(map[string]string) - for i, name := range todoRegEx.SubexpNames() { - if i != 0 && name != "" { - result[name] = match[i] - } - } - - if filepath.IsAbs(result["path"]) { - continue - } - - path := path.Clean(absolute + "/" + result["path"]) - content = strings.Replace(content, result["match"], "!["+result["label"]+"]("+path+")", -1) - - } - - return content -} - -func convertTemplateLogo(content string, absolute string) string { - todoRegEx := regexp.MustCompile(`(?m)^(?P<match>logo:\s*"?(?P<path>[^"\n]*)"?\s*)$`) - - matches := todoRegEx.FindAllStringSubmatch(content, -1) - if matches == nil { - return content - } - - for _, match := range matches { - result := make(map[string]string) - for i, name := range todoRegEx.SubexpNames() { - if i != 0 && name != "" { - result[name] = match[i] - } - } - - if filepath.IsAbs(result["path"]) { - continue - } - - path := path.Clean(absolute + "/" + result["path"]) - content = strings.Replace(content, result["match"], "logo: \""+path+"\"", -1) - - } - - return content -} - -func convertTemplateLatexLogo(content string, absolute string) string { - todoRegEx := regexp.MustCompile(`(?m)(?P<match>\\includegraphics[^{]*\{(?P<path>[^}]*)\})`) - - matches := todoRegEx.FindAllStringSubmatch(content, -1) - if matches == nil { - return content - } - - for _, match := range matches { - result := make(map[string]string) - for i, name := range todoRegEx.SubexpNames() { - if i != 0 && name != "" { - result[name] = match[i] - } - } - - if filepath.IsAbs(result["path"]) { - continue - } - - path := path.Clean(absolute + "/" + result["path"]) - a := strings.Replace(result["match"], result["path"], path, -1) - - content = strings.Replace(content, result["match"], a, -1) - - } - - return content -} - -func convertTemplateImages(content string, absolute string) string { - content = convertTemplateLogo(content, absolute) - content = convertTemplateLatexLogo(content, absolute) - - return content - -} diff --git a/development/temp/run.go b/development/temp/run.go deleted file mode 100644 index 8e9f1e0a845b4df5ccb23be020ddedc420b22b61..0000000000000000000000000000000000000000 --- a/development/temp/run.go +++ /dev/null @@ -1,40 +0,0 @@ -package main - -import ( - "bytes" - "log" - "os/exec" - "syscall" -) - -func runCommand(name string, args ...string) (stdout string, stderr string, exitCode int) { - var outbuf, errbuf bytes.Buffer - - cmd := exec.Command(name, args...) - cmd.Stdout = &outbuf - cmd.Stderr = &errbuf - - err := cmd.Run() - stdout = outbuf.String() - stderr = errbuf.String() - - if err != nil { - // try to get the exit code - if exitError, ok := err.(*exec.ExitError); ok { - ws := exitError.Sys().(syscall.WaitStatus) - exitCode = ws.ExitStatus() - } else { - log.Printf("Could not get exit code for failed program: %v, %v", name, args) - exitCode = exitCodeCatchAll - if stderr == "" { - stderr = err.Error() - } - } - } else { - // success, exitCode should be 0 if go is ok - ws := cmd.ProcessState.Sys().(syscall.WaitStatus) - exitCode = ws.ExitStatus() - } - - return -} diff --git a/development/temp/tasks.go b/development/temp/tasks.go deleted file mode 100644 index 52eeca3b99e9d82532a50d65175278c8c79f12bc..0000000000000000000000000000000000000000 --- a/development/temp/tasks.go +++ /dev/null @@ -1,146 +0,0 @@ -package main - -import ( - "bytes" - "fmt" - "regexp" - "time" - - "github.com/olekukonko/tablewriter" -) - -type task struct { - due time.Time - text string - checked bool -} - -const dateLayout = "2006-01-02 15:04:05 GMT" - -func newTask(date string, text string, checked bool) task { - - var d time.Time - var err error - - if date != "" { - d, err = time.Parse(dateLayout, date+" 00:00:00 GMT") - if err != nil { - printError("the due of the todo cannot be parsed", date, text) - } - } - - t := task{ - due: d, - text: text, - checked: checked, - } - - return t -} - -func findTask(content []byte) []task { - - toDos := []task{} - - todoRegEx := regexp.MustCompile(`(?m)^\- \[(?P<checked>(x|\s?))\]\s*(?P<due>[2][0-9]{3}-[0-9]{2}-[0-9]{2}){0,1}\s*(?P<text>[^\n]*)`) - - s := string(content) - matches := todoRegEx.FindAllStringSubmatch(s, -1) - if matches == nil { - return []task{} - } - - for _, match := range matches { - result := make(map[string]string) - for i, name := range todoRegEx.SubexpNames() { - if i != 0 && name != "" { - result[name] = match[i] - } - } - - checked := true - - if result["checked"] != "x" { - checked = false - } - - t := newTask(result["due"], result["text"], checked) - toDos = append(toDos, t) - - } - - return toDos -} - -func buildTasksTable(pageMap map[string]*documentContent, extended bool) (error, string, bool) { - - buf := new(bytes.Buffer) - - table := tablewriter.NewWriter(buf) - has := false - - header := []string{ - printer.Sprintf("ToDo"), - printer.Sprintf("Due"), - printer.Sprintf("Checked")} - - if extended == true { - header = append(header, printer.Sprintf("File")) - } - - table.SetHeader(header) - table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false}) - table.SetCenterSeparator("|") - - var tableData [][]string - - for _, pageDoc := range pageMap { - - pageData := pageDoc.meta - - for _, info := range pageData.ToDos { - - has = true - - var d string - - if info.due.IsZero() { - d = "—" - } else { - d = info.due.Format(config.Locale.DateFormat) - } - - checked := "—" - if info.checked == true { - checked = "✓" - } - - cols := []string{info.text, d, checked} - - if extended == true { - cols = append(cols, pageData.File) - } - - tableData = append(tableData, cols) - } - - } - - table.AppendBulk(tableData) // Add Bulk Data - table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) - table.Render() - - return nil, buf.String(), has -} - -func printTaskTable() error { - err, pageData := collectStructureFromFiles(config.Path) - if err != nil { - return err - } - - err, table, _ := buildTasksTable(pageData, true) - fmt.Println(table) - - return nil -} diff --git a/development/temp/util.go b/development/temp/util.go deleted file mode 100644 index 46abce5061c481f733b48774642630aef5261509..0000000000000000000000000000000000000000 --- a/development/temp/util.go +++ /dev/null @@ -1,170 +0,0 @@ -package main - -import ( - "bytes" - "errors" - "os" - "path/filepath" - "strings" - "text/template" - - "gopkg.in/yaml.v3" -) - -type documentContent struct { - text string - meta document -} - -func splitYamlParts(content []byte) (error, documentContent) { - - origin := string(content) - - meta := "" - text := "" - - before, remaining, found := strings.Cut(origin, "---") - if !found { - - t := strings.TrimSpace(origin) - if len(t) == 0 { - return errors.New("the file is empty"), documentContent{ - text: origin, - meta: document{}, - } - } - - return errors.New("the file does not contain a definition block"), documentContent{ - text: origin, - meta: document{}, - } - } - - text += before - - a, b, found := strings.Cut(remaining, "\n...") - if !found { - a, b, found = strings.Cut(remaining, "\n---") - - if !found { - - a, b, found = strings.Cut(remaining, "...") - - if !found { - - a, b, found = strings.Cut(remaining, "---") - - if !found { - return errors.New("the file does not contain a definition block"), documentContent{} - } - - } - } - - } - - meta = a - text += b - - t, err := template.New("overview").Parse(text) - if err != nil { - return err, documentContent{} - } - - var node yaml.Node - err = yaml.Unmarshal([]byte(meta), &node) - if err != nil { - return err, documentContent{} - } - - data := document{} - err = node.Decode(&data) - if err != nil { - return err, documentContent{} - } - - req := documentContent{} - req.meta = data - - req.meta.OriginNode = &node - req.meta.OriginText = text - - var buffer bytes.Buffer - err = t.Execute(&buffer, data) - if err != nil { - printError(err.Error()) - return err, documentContent{} - } - - req.text = buffer.String() - - return nil, req - -} - -func translateHeaders(columns []string) []string { - - result := []string{} - - for _, column := range columns { - result = append(result, printer.Sprintf(column)) - } - - return result - -} - -func getGitDirectory() (string, error) { - - path := config.Path - - fileInfo, err := os.Stat(path) - if err != nil { - return "", err - } - - if !fileInfo.IsDir() { - path = filepath.Dir(path) - } - - return findGitDirectory(path) -} - -func findGitDirectory(path string) (string, error) { - - if path == "" { - return "", noGitRepositoryError - } - - if volume := filepath.VolumeName(path); volume != "" { - if volume == path { - return "", noGitRepositoryError - } - } else { - if path == "/" { - return "", noGitRepositoryError - } - } - - if !exists(filepath.Join(path, ".git")) { - return findGitDirectory(filepath.Dir(path)) - } - - return path, nil - -} - -func exists(path string) bool { - _, err := os.Stat(path) - if err == nil { - return true - } - - if os.IsNotExist(err) { - return false - } - - printErrorAndExit(0, err.Error()) - - return false -}