Skip to content

Commit

Permalink
Improves args handling and fixes np when line has no leading star
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasepe committed Sep 11, 2020
1 parent 1e318e7 commit e9e9a4a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 14 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions crumbs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
flagVertical bool
flagWrapLim uint
flagImagesPath string
flagImagesType string
)

func main() {
Expand Down Expand Up @@ -69,23 +70,22 @@ 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) {
r, err := os.Open(name)
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() {
Expand Down Expand Up @@ -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:])
}
Expand Down
17 changes: 13 additions & 4 deletions parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package crumbs

import (
"fmt"
"path/filepath"
"regexp"
"strings"
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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, "")
}
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions testdata/when-to-use-crumbs-icons-no-path-ext.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e9e9a4a

Please sign in to comment.