Skip to content

Commit

Permalink
feat: began work on preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
xNaCly committed Apr 10, 2023
1 parent b2aab92 commit 50afa2c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
11 changes: 10 additions & 1 deletion fleck.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/xnacly/fleck/cli"
"github.com/xnacly/fleck/preprocessor"
"github.com/xnacly/fleck/scanner"
)

Expand All @@ -18,6 +19,14 @@ func main() {
os.Exit(1)
}

s := scanner.New(ARGUMENTS.InputFile)
if ARGUMENTS.ShellMacroEnabled {
log.Println("warning: 'shell-macro-enabled' flag specified, this can harm your operating system and make it vulnerable for attack, proceed at your own digression")
}

fileName := ARGUMENTS.InputFile

preprocessor.Process(fileName)

s := scanner.New(fileName + ".fleck")
s.Lex()
}
37 changes: 33 additions & 4 deletions preprocessor/preprocessor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
package preprocessor

// this module contains logic for replacing snippets / macros in the future, such as:
// - @include{file} => gets replaced with the markdown content of the file
// - @today{format} => gets replaced by the current date
// - @shell{command} => gets replaced by the output of the given command
// possible errors:
// - empty @include => warning
// - @include cycle => error and abort
// - empty @today => warning
// - empty @shell => warning

import (
"bufio"
"log"
"os"
"time"
)

func Process(filename string) {
f, err := os.Open(filename)
if err != nil {
log.Fatalln("couldn't open file: '" + err.Error() + "'")
}
s := bufio.NewScanner(f)

ok := s.Scan()
for ok {
s.Text()
// process the line here, replace found macros
ok = s.Scan()
}

// write to filename+".fleck" here
}

func todayMacro(format string) string {
return time.Now().Format(format)
}
2 changes: 1 addition & 1 deletion scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Scanner struct {
func New(fileName string) Scanner {
file, err := os.Open(fileName)
if err != nil {
log.Fatalln("couldn't open file", err)
log.Fatalln("couldn't open file: '" + err.Error() + "'")
}
scan := bufio.NewScanner(file)
scan.Scan()
Expand Down

0 comments on commit 50afa2c

Please sign in to comment.