diff --git a/CHANGELOG.md b/CHANGELOG.md index 80ba59a..6fa0b25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,11 +4,27 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.3.0] - 2020-11-09 +### Added +- 🎉 new flag `-images-type` to specify a default suffix for all the images + - specifying this flag when including images, you can omit the extension + - example: if flag has `-images-type png`, you can write `[[bulb]]` instead of `[[build.png]]` + +### Changed +- updated the README markdown file +- improve argument parsing + - when invoked without args, an attempt is made to read from standard input + +### Fixed +- 🐛 Line with no leading stars causes nil pointer dereference [#2](/issues/#2) + ## [0.2.0] - 2020-09-09 ### Added - 📝 more test cases - 🎉 new flag `images-path` to specify the base images folder - now when including images, you can specify just the filename + - example: if flag has `-images-path '/Icons/AwesomFonts'`, you can write `[[bulb.png]]` instead of `[[/Icons/AwesomFonts/build.png]]` + - 🎉 new flag `lim` to specify after how many characters to wrap the text ### Changed diff --git a/README.md b/README.md index 19aa072..30cfbfa 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,9 @@ Here the output: You can, eventually, add images too (one for text line) using a special syntax: `[[path/to/image.png]]` -- starting from the release v0.2.0 you can specify the image path from the command line `-image-path` flag -- if you specify the flag `-image-path` you can wriate `[[image.png]]` instead of `[[path/to/image.png]]` +- if you specify the flag `-image-path` you can write `[[image.png]]` instead of `[[path/to/image.png]]` +- if you specify the flag `-image-type` you can write `[[path/to/image]]` instead of `[[path/to/image.png]]` +- therefore if you specify both you can write `[[image]]` instead of `[[path/to/image.png]]` ```text * [[./png/bulb.png]] main idea diff --git a/crumbs/main.go b/crumbs/main.go index f17b278..4c9113b 100644 --- a/crumbs/main.go +++ b/crumbs/main.go @@ -32,6 +32,7 @@ var ( flagVertical bool flagWrapLim uint flagImagesPath string + flagImagesType string ) func main() { @@ -69,13 +70,12 @@ func readEntry() (*crumbs.Entry, error) { } text := string(src) lines := strings.SplitAfter(text, "\n") - return crumbs.ParseLines(lines, flagImagesPath) + return crumbs.ParseLines(lines, flagImagesPath, flagImagesType) } func readFileObject(r io.Reader, limit int64) ([]byte, error) { lr := io.LimitReader(r, limit) - src, err := ioutil.ReadAll(lr) - return src, err + return ioutil.ReadAll(lr) } func readFile(name string, limit int64) ([]byte, error) { @@ -83,9 +83,9 @@ func readFile(name string, limit int64) ([]byte, error) { if err != nil { return nil, err } - src, err := readFileObject(r, limit) - r.Close() - return src, err + defer r.Close() + + return readFileObject(r, limit) } func configureFlags() { @@ -117,8 +117,8 @@ func configureFlags() { "layout entries as vertical directed graph") flag.CommandLine.UintVar(&flagWrapLim, "lim", 28, "wraps each line within this width in characters") - flag.CommandLine.StringVar(&flagImagesPath, "images-path", "./", "folder in which to look for image files") - //flag.CommandLine.StringVar(&flagImagesType, "images-type", "png", "images file extension [png,jpg,svg]") + flag.CommandLine.StringVar(&flagImagesPath, "images-path", "", "folder in which to look for image files") + flag.CommandLine.StringVar(&flagImagesType, "images-type", "", "images file extension [png,jpg,svg]") flag.CommandLine.Parse(os.Args[1:]) } diff --git a/parser.go b/parser.go index 8ac5743..b5fcb37 100644 --- a/parser.go +++ b/parser.go @@ -1,6 +1,7 @@ package crumbs import ( + "fmt" "path/filepath" "regexp" "strings" @@ -9,9 +10,9 @@ import ( ) // ParseLines parses a slice of text lines and builds the tree. -func ParseLines(lines []string, iconspath string) (*Entry, error) { +func ParseLines(lines []string, imagesPath, imagesSuffix string) (*Entry, error) { mkID := idGenerator() - checkIcon := lookForIcon(iconspath) + checkIcon := lookForIcon(imagesPath, imagesSuffix) // generate a short id for the root node rootID, err := mkID() @@ -33,6 +34,11 @@ func ParseLines(lines []string, iconspath string) (*Entry, error) { // count depth childDepth := depth(el) + // case: no leading 'stars' (skip line) + if childDepth == 0 { + continue + } + // trim leading 'stars', then the spaces text := el[childDepth:] text = strings.TrimSpace(text) @@ -118,14 +124,17 @@ func idGenerator() func() (string, error) { } } -func lookForIcon(iconspath string) func(note *Entry) { +func lookForIcon(imagesPath, imagesSuffix string) func(note *Entry) { re := regexp.MustCompile(`^\[{2}(.*?)\]{2}`) return func(note *Entry) { str := note.text res := re.FindStringSubmatch(str) if len(res) > 0 { - note.icon = filepath.Join(iconspath, strings.TrimSpace(res[1])) + note.icon = filepath.Join(imagesPath, strings.TrimSpace(res[1])) + if len(imagesSuffix) > 0 { + note.icon = fmt.Sprintf("%s.%s", note.icon, imagesSuffix) + } note.text = re.ReplaceAllString(str, "") } } diff --git a/testdata/when-to-use-crumbs-icons-no-path-ext.png b/testdata/when-to-use-crumbs-icons-no-path-ext.png new file mode 100644 index 0000000..70391fc Binary files /dev/null and b/testdata/when-to-use-crumbs-icons-no-path-ext.png differ diff --git a/testdata/when-to-use-crumbs-icons-no-path-ext.txt b/testdata/when-to-use-crumbs-icons-no-path-ext.txt new file mode 100644 index 0000000..56be10d --- /dev/null +++ b/testdata/when-to-use-crumbs-icons-no-path-ext.txt @@ -0,0 +1,21 @@ +* When to use "crumbs" + +** [[bulb]] to generate ideas +*** to search for patterns +*** to speculate and explore ideas +*** to merge ideas + +** [[comments-alt]] to communicate +*** to demostrate a structure +*** to show your thoughts to others +*** to record a meeting + +** [[book-reader]] to learn +*** to record a lecture +*** to prepare a paper +*** to study for exams and certifications + +** [[map-signs]] to make decisions +*** to analyze problems +*** to get team's input +*** to record multiple ideas \ No newline at end of file