From 824879afee49d37a5b5292216b950710aaeb68b4 Mon Sep 17 00:00:00 2001 From: xnacly <47723417+xNaCly@users.noreply.github.com> Date: Wed, 19 Apr 2023 07:35:18 +0200 Subject: [PATCH] refactor(core): split functions from main to core --- cli/cli.go | 13 +++++- core/core.go | 106 +++++++++++++++++++++++++++++++++++++++++++++ doc/Builds.md | 1 + doc/Features.md | 1 + doc/Structure.md | 11 ++++- fleck.go | 110 ++++------------------------------------------- test.md | 2 + 7 files changed, 139 insertions(+), 105 deletions(-) create mode 100644 core/core.go diff --git a/cli/cli.go b/cli/cli.go index 074c2a8..f49c7a8 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -26,16 +26,25 @@ func ParseCli() Arguments { } } -// prints the help with all options available +// prints a help page with only the usage func PrintShortHelp() { fmt.Println(`Usage: fleck [Options] file + Run 'fleck --help' for an in depth help page + `) +} + +// prints the help with all options available +func PrintLongHelp() { + fmt.Println(`Usage: + fleck [Options] file + Options:`) for _, v := range OPTIONS { fmt.Printf("\t--%-20s\t\t%s\n", v.Name, v.Description) } - fmt.Println("") + fmt.Println("\nOnline documentation: https://github.com/xnacly/fleck") } func PrintVersion(version, buildAt, buildBy string) { diff --git a/core/core.go b/core/core.go new file mode 100644 index 0000000..9f17840 --- /dev/null +++ b/core/core.go @@ -0,0 +1,106 @@ +package core + +import ( + "fmt" + "os" + "time" + + "github.com/xnacly/fleck/cli" + "github.com/xnacly/fleck/generator" + "github.com/xnacly/fleck/logger" + "github.com/xnacly/fleck/parser" + "github.com/xnacly/fleck/preprocessor" + "github.com/xnacly/fleck/scanner" +) + +// alerts the user if a flag depends on a different flag to have an effect +func FlagCombinationSensible() { + for _, f := range cli.OPTIONS { + if len(f.Requires) == 0 { + continue + } + if cli.GetFlag(cli.ARGUMENTS, f.Name) && !cli.GetFlag(cli.ARGUMENTS, f.Requires) { + logger.LWarn(fmt.Sprintf("flag '--%s' requires flag '--%s' to be set, otherwise it has no effect.", f.Name, f.Requires)) + } + } +} + +// watches for changes in a file, recompiles the file if a change occurs, can be exited via +func WatchForChanges(fileName string, executor func(string)) { + executor(fileName) + logger.LInfo("watching for changes...") + + initialStat, err := os.Stat(fileName) + if err != nil { + logger.LError("failed to watch for changes: " + err.Error()) + } + + for { + stat, err := os.Stat(fileName) + if err != nil { + logger.L("test") + logger.LError("failed to watch for changes: " + err.Error()) + } + + if stat.Size() != initialStat.Size() || stat.ModTime() != stat.ModTime() { + initialStat = stat + logger.LInfo("detected change, recompiling...") + executor(fileName) + } + + time.Sleep(1 * time.Second) + } +} + +func Run(fileName string) { + start := time.Now() + + if cli.GetFlag(cli.ARGUMENTS, "preprocessor-enabled") { + logger.LInfo("preprocessor enabled, starting...") + preprocessor.Process(cli.ARGUMENTS, fileName) + fileName = fileName + ".fleck" + } + + logger.LDebug("starting scanner") + lexerStart := time.Now() + s := scanner.New(fileName) + tokens := s.Lex() + logger.LDebug("lexed " + fmt.Sprint(len(tokens)) + " token, took " + time.Since(lexerStart).String()) + if logger.DEBUG { + s.PrintTokens() + } + + logger.LDebug("starting parser") + parserStart := time.Now() + p := parser.New(tokens) + result := p.Parse() + logger.LDebug("parsed " + fmt.Sprint(len(result)) + " items, took " + time.Since(parserStart).String()) + logger.LDebug("parsed tags:", result) + + var toc string + if cli.GetFlag(cli.ARGUMENTS, "toc") { + logger.LDebug("generating toc...") + toc = p.GenerateToc() + } + + if cli.GetFlag(cli.ARGUMENTS, "no-template") { + generator.WritePlain(fileName, result, toc) + } else { + generator.WriteTemplate(fileName, result, toc) + } + + logger.LInfo("compiled '" + fileName + "', took: " + time.Since(start).String()) + + defer func() { + if cli.GetFlag(cli.ARGUMENTS, "preprocessor-enabled") { + if cli.GetFlag(cli.ARGUMENTS, "keep-temp") { + return + } + logger.LDebug("cleanup, removing: '" + fileName + "'") + err := os.Remove(fileName) + if err != nil { + logger.LWarn(err.Error()) + } + } + }() +} diff --git a/doc/Builds.md b/doc/Builds.md index 7191b76..1430c9f 100644 --- a/doc/Builds.md +++ b/doc/Builds.md @@ -13,6 +13,7 @@ go build -tags=bare . The `bare` build does not support: - cli options and flags +- watch mode and live preview - colored output - extensive logs and time stamps for compilation steps - templates diff --git a/doc/Features.md b/doc/Features.md index 75bf447..8ed9ce5 100644 --- a/doc/Features.md +++ b/doc/Features.md @@ -22,3 +22,4 @@ Fleck implements the basic markdown features everyone needs, while extending mar - Fleck escapes html characters and does therefore not support inline html. - Fleck minifies the output html by default - Fleck provides a default template for the compiled html to be written to +- Fleck allows the user to view their markdown via a live preview, which recompiles after changes and reloads the page diff --git a/doc/Structure.md b/doc/Structure.md index 89eb72b..848cff4 100644 --- a/doc/Structure.md +++ b/doc/Structure.md @@ -12,12 +12,21 @@ The `cli` modules main objective is to handle parsing of command line arguments It contains the `Arguments` structure, which in itself contains all the flags and arguments fleck can receive and the `ParseCli` function which returns the structure. -`cli` also contains the function which is used to print the help message (`PrintShortHelp`). +`cli` also contains the functions used to print the help message (`PrintShortHelp` & `PrintLongHelp`). + +### core + +The `core` module contains functions necessary for flecks execution, such as the check if the provided flags are sensible and no flag is set which requires an other flag to have an effect on the output. +It also contains the `Run` function and the `Watch` function, which are both used to wrap the calls to the preprocessor, lexer, parser and generator. ### doc The doc directory contains all written documentation for fleck, such as [architecture](./Architecture.md) and [structure](#) +### generator + +The generator module includes the default template fleck will write its output to and is responsible for setting the outputs document title, styling and content. + ### logger The `logger` module contains logging helpers with colors. diff --git a/fleck.go b/fleck.go index 1eb4bb8..8912e68 100644 --- a/fleck.go +++ b/fleck.go @@ -3,16 +3,11 @@ package main import ( - "fmt" "os" - "time" "github.com/xnacly/fleck/cli" - "github.com/xnacly/fleck/generator" + "github.com/xnacly/fleck/core" "github.com/xnacly/fleck/logger" - "github.com/xnacly/fleck/parser" - "github.com/xnacly/fleck/preprocessor" - "github.com/xnacly/fleck/scanner" ) // supplied by the build process @@ -20,114 +15,25 @@ var VERSION = "" var BUILD_AT = "" var BUILD_BY = "" -// alerts the user if a flag depends on a different flag to have an effect -func flagCombinationSensible() { - for _, f := range cli.OPTIONS { - if len(f.Requires) == 0 { - continue - } - if cli.GetFlag(cli.ARGUMENTS, f.Name) && !cli.GetFlag(cli.ARGUMENTS, f.Requires) { - logger.LWarn(fmt.Sprintf("flag '--%s' requires flag '--%s' to be set, otherwise it has no effect.", f.Name, f.Requires)) - } - } -} - -// watches for changes in a file, recompiles the file if a change occurs, can be exited via -func watchForChanges(fileName string) { - run(fileName) - logger.LInfo("watching for changes...") - - initialStat, err := os.Stat(fileName) - if err != nil { - logger.LError("failed to watch for changes: " + err.Error()) - } - - for { - stat, err := os.Stat(fileName) - if err != nil { - logger.L("test") - logger.LError("failed to watch for changes: " + err.Error()) - } - - if stat.Size() != initialStat.Size() || stat.ModTime() != stat.ModTime() { - initialStat = stat - logger.LInfo("detected change, recompiling...") - run(fileName) - } - - time.Sleep(1 * time.Second) - } -} - -func run(fileName string) { - start := time.Now() - - if cli.GetFlag(cli.ARGUMENTS, "preprocessor-enabled") { - logger.LInfo("preprocessor enabled, starting...") - preprocessor.Process(cli.ARGUMENTS, fileName) - fileName = fileName + ".fleck" - } - - logger.LDebug("starting scanner") - lexerStart := time.Now() - s := scanner.New(fileName) - tokens := s.Lex() - logger.LDebug("lexed " + fmt.Sprint(len(tokens)) + " token, took " + time.Since(lexerStart).String()) - if logger.DEBUG { - s.PrintTokens() - } - - logger.LDebug("starting parser") - parserStart := time.Now() - p := parser.New(tokens) - result := p.Parse() - logger.LDebug("parsed " + fmt.Sprint(len(result)) + " items, took " + time.Since(parserStart).String()) - logger.LDebug("parsed tags:", result) - - var toc string - if cli.GetFlag(cli.ARGUMENTS, "toc") { - logger.LDebug("generating toc...") - toc = p.GenerateToc() - } - - if cli.GetFlag(cli.ARGUMENTS, "no-template") { - generator.WritePlain(fileName, result, toc) - } else { - generator.WriteTemplate(fileName, result, toc) - } - - logger.LInfo("compiled '" + fileName + "', took: " + time.Since(start).String()) - - defer func() { - if cli.GetFlag(cli.ARGUMENTS, "preprocessor-enabled") { - if cli.GetFlag(cli.ARGUMENTS, "keep-temp") { - return - } - logger.LDebug("cleanup, removing: '" + fileName + "'") - err := os.Remove(fileName) - if err != nil { - logger.LWarn(err.Error()) - } - } - }() -} - // TODO: only rebuild if the file changed, md5 hash? func main() { cli.ARGUMENTS = cli.ParseCli() + if cli.GetFlag(cli.ARGUMENTS, "version") { cli.PrintVersion(VERSION, BUILD_AT, BUILD_BY) } + if cli.GetFlag(cli.ARGUMENTS, "help") { - cli.PrintShortHelp() + cli.PrintLongHelp() os.Exit(0) } + if len(cli.ARGUMENTS.InputFile) == 0 { cli.PrintShortHelp() logger.LError("not enough arguments, specify an input file") } - flagCombinationSensible() + core.FlagCombinationSensible() logger.DEBUG = cli.GetFlag(cli.ARGUMENTS, "debug") logger.SILENT = cli.GetFlag(cli.ARGUMENTS, "silent") @@ -141,8 +47,8 @@ func main() { } if cli.GetFlag(cli.ARGUMENTS, "watch") { - watchForChanges(fileName) + core.WatchForChanges(fileName, core.Run) } else { - run(fileName) + core.Run(fileName) } } diff --git a/test.md b/test.md index 80ff0eb..f60d6ba 100644 --- a/test.md +++ b/test.md @@ -5,6 +5,8 @@ ```js // `npm run this-shit` console.log("🤬"); + +console.log(`${name} is dumb`); ```