-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added transform command. #29
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a8d1312
Added web command.
bwplotka 600e9bf
Changed to transform.
bwplotka 33d871c
Fixed links.
bwplotka 40a27f6
Added `lastmod` functionality.
bwplotka 6fae4fb
Added more tags.
bwplotka 0b421ed
Fixed links.
bwplotka 3636a5b
Fix links.
bwplotka a25cb51
Added trailing slash.
bwplotka 8ba677d
Fixed hugo link mode.
bwplotka 625f80b
Added skip and static dir funciton.
bwplotka 6d392f4
Added extra file functionality.
bwplotka e4066b4
Addressed comments.
bwplotka 53db60f
Update README.md
bwplotka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Changed to transform.
Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
- Loading branch information
commit 600e9bf547239147de17d3433daea692edf71aea
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package transform | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/gobwas/glob" | ||
"github.com/pkg/errors" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type Config struct { | ||
Version int | ||
InputDir string `yaml:"inputDir"` | ||
OutputDir string `yaml:"outputDir"` | ||
|
||
Transformations []*TransformationConfig | ||
|
||
// GitIgnore specifies if output dir should be git ignored or not. | ||
GitIgnore bool `yaml:"GitIgnore"` | ||
} | ||
|
||
type TransformationConfig struct { | ||
_glob glob.Glob | ||
|
||
// Glob matches files using https://github.com/gobwas/glob. | ||
// After first match, file is no longer matching other elements. | ||
Glob string | ||
|
||
// Skip skips moving matched files. | ||
Skip bool | ||
|
||
// Path is an optional different path for the file to be moved. | ||
// NOTE: All relative links will be moved accordingly. | ||
Path string | ||
|
||
// FrontMatter holds front matter transformations. | ||
FrontMatter *FrontMatterConfig `yaml:"frontMatter"` | ||
} | ||
|
||
type FrontMatterConfig struct { | ||
_template *template.Template | ||
|
||
// Template represents Go template that will be rendered as font matter. | ||
// This will override any existing font matter.1 | ||
// TODO(bwplotka): Add add only option? | ||
Template string | ||
} | ||
|
||
func parseConfigFile(configFile string) (Config, error) { | ||
configFile, err := filepath.Abs(configFile) | ||
if err != nil { | ||
return Config{}, errors.Wrap(err, "abs") | ||
} | ||
c, err := ioutil.ReadFile(configFile) | ||
if err != nil { | ||
return Config{}, errors.Wrap(err, "read config file") | ||
} | ||
return ParseConfig(c) | ||
} | ||
|
||
func ParseConfig(c []byte) (Config, error) { | ||
cfg := Config{} | ||
if err := yaml.Unmarshal(c, &cfg); err != nil { | ||
return Config{}, errors.Wrapf(err, "parsing template content %q", string(c)) | ||
} | ||
|
||
if cfg.InputDir == "" { | ||
return Config{}, errors.New("contentDir field is required") | ||
} | ||
|
||
d, err := os.Stat(cfg.InputDir) | ||
if err != nil { | ||
return Config{}, err | ||
} | ||
if !d.IsDir() { | ||
return Config{}, errors.New("contentDir field is not pointing directory") | ||
} | ||
cfg.InputDir = strings.TrimSuffix(cfg.InputDir, "/") | ||
|
||
if cfg.OutputDir == "" { | ||
return Config{}, errors.New("outputDir field is required") | ||
} | ||
cfg.OutputDir = strings.TrimSuffix(cfg.OutputDir, "/") | ||
|
||
for _, f := range cfg.Transformations { | ||
f._glob, err = glob.Compile(f.Glob, '/') | ||
if err != nil { | ||
return Config{}, errors.Wrapf(err, "compiling glob %v", f.Glob) | ||
} | ||
|
||
if f.FrontMatter != nil { | ||
f.FrontMatter._template, err = template.New("").Parse(f.FrontMatter.Template) | ||
if err != nil { | ||
return Config{}, errors.Wrapf(err, "compiling template %v", f.FrontMatter.Template) | ||
} | ||
} | ||
} | ||
|
||
return cfg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tmp/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
version: 1 | ||
|
||
inputDir: "testdata/testproj" | ||
outputDir: "testdata/tmp/1" | ||
|
||
gitIgnored: true | ||
|
||
transformations: | ||
- glob: "/README.md" | ||
path: _index.md | ||
frontMatter: | ||
template: | | ||
title: "{{ .FirstHeader }}" | ||
|
||
cascade: | ||
- type: "docs" | ||
_target: | ||
path: "/**" | ||
|
||
- glob: "**/README.md" | ||
frontMatter: | ||
template: | | ||
title: "{{ .FirstHeader }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
version: 1 | ||
|
||
inputDir: "testdata/testproj" | ||
outputDir: "testdata/tmp/2" | ||
|
||
gitIgnored: true | ||
|
||
transformations: | ||
- glob: "/README.md" | ||
path: _index.md | ||
frontMatter: | ||
template: | | ||
title: "{{ .FirstHeader }}" | ||
|
||
cascade: | ||
- type: "docs" | ||
_target: | ||
path: "/**" | ||
|
||
- glob: "/doc.md" | ||
frontMatter: | ||
template: | | ||
title: "{{ .FirstHeader }}" | ||
|
||
- glob: "/Team/doc.md" | ||
path: inner/doc.md | ||
frontMatter: | ||
template: | | ||
title: "{{ .FirstHeader }}" | ||
yolo: "yolo" | ||
|
||
- glob: "**/README.md" | ||
path: /test1/_index.md | ||
frontMatter: | ||
template: | | ||
title: "{{ .FirstHeader }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Proposals | ||
|
||
[RelLink](../../../tmp/2/test1/_index.md) | ||
|
||
[RelLink](../Team/doc.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Group Handbook | ||
|
||
Yolo | ||
|
||
[RelLink](Proposals/README.md) | ||
|
||
[RelLink](Team/doc.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Some Doc | ||
|
||
[RelLink](#some-doc) | ||
|
||
[RelLink](../README.md#group-handbook) | ||
|
||
[RelLink](../Proposals/README.md) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a section can be added to README about
transform
command, with sample config yaml, and that can be linked instead of struct? 🙂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, we could use mdox-exec with our tool we plan to build and do this at the point? =D Great idea!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure!