From 8b6f9b60dad43077af72b759c4ee0bb2e8cd3cb1 Mon Sep 17 00:00:00 2001 From: Alex Suraci Date: Tue, 16 Apr 2019 11:59:55 -0400 Subject: [PATCH] add --section-path, rename --tag to --section-tag this flag is for loading a .lit file in the context of another .lit file, e.g. loading docs and rendering release notes that reference the docs Signed-off-by: Alex Suraci --- booklitcmd/command.go | 18 ++++++++++++------ load/processor.go | 6 +++++- section.go | 6 +++++- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/booklitcmd/command.go b/booklitcmd/command.go index 85ca301..570c617 100644 --- a/booklitcmd/command.go +++ b/booklitcmd/command.go @@ -19,10 +19,11 @@ import ( type Command struct { Version func() `short:"v" long:"version" description:"Print the version of Boooklit and exit."` - In string `long:"in" short:"i" required:"true" description:"Input .lit file to load."` - + In string `long:"in" short:"i" required:"true" description:"Input .lit file to load."` Out string `long:"out" short:"o" description:"Directory into which sections will be rendered."` - Tag string `long:"tag" short:"t" description:"Section tag to render."` + + SectionTag string `long:"section-tag" description:"Section tag to render."` + SectionPath string `long:"section-path" description:"Section path to load and render with --in as its parent."` SaveSearchIndex bool `long:"save-search-index" description:"Save a search index JSON file in the destination."` @@ -116,13 +117,18 @@ func (cmd *Command) Build() error { } sectionToRender := section - if cmd.Tag != "" { - tags := section.FindTag(cmd.Tag) + if cmd.SectionTag != "" { + tags := section.FindTag(cmd.SectionTag) if len(tags) == 0 { - return fmt.Errorf("unknown tag: %s", cmd.Tag) + return fmt.Errorf("unknown tag: %s", cmd.SectionTag) } sectionToRender = tags[0].Section + } else if cmd.SectionPath != "" { + sectionToRender, err = processor.LoadFileIn(section, cmd.SectionPath, basePluginFactories) + if err != nil { + return err + } } if cmd.Out == "" { diff --git a/load/processor.go b/load/processor.go index 2325e8a..e18feb0 100644 --- a/load/processor.go +++ b/load/processor.go @@ -25,7 +25,11 @@ type parsedNode struct { } func (processor *Processor) LoadFile(path string, pluginFactories []booklit.PluginFactory) (*booklit.Section, error) { - section, err := processor.EvaluateFile(nil, path, pluginFactories) + return processor.LoadFileIn(nil, path, pluginFactories) +} + +func (processor *Processor) LoadFileIn(parent *booklit.Section, path string, pluginFactories []booklit.PluginFactory) (*booklit.Section, error) { + section, err := processor.EvaluateFile(parent, path, pluginFactories) if err != nil { return nil, err } diff --git a/section.go b/section.go index 002d008..40a4513 100644 --- a/section.go +++ b/section.go @@ -112,7 +112,11 @@ func (con *Section) Number() string { parentNumber := con.Parent.Number() selfIndex := 1 - for i := 0; con.Parent.Children[i] != con; i++ { + for _, child := range con.Parent.Children { + if child == con { + break + } + selfIndex++ }