mirror of
https://github.com/Warky-Devs/go-mdtopdf-helper.git
synced 2026-11-25 13:48:47 +00:00
feat: add DOCX/HTML output, go-tomd reverse converter, and input/output flags
- go-mdtopdf-helper: -format pdf,html,docx; new internal/document writers (standalone HTML, pure Go DOCX) - new go-tomd command converting PDF/HTML/DOCX back to Markdown, skipping existing .md files unless -force - -input/-output on both commands (single file with chosen name, or output directory mirroring the scan layout) - restructure README and build config (Makefile, goreleaser) for both binaries
This commit is contained in:
1 parent
50b7246c21
commit
5bf9c3673d
17 files changed
+1903
-84
No files matched your search
@@ -46,22 +46,97 @@ func ParseFormats(list string) ([]string, error) {
|
||||
|
||||
// Converter converts Markdown files found under InputDir into each of Formats.
|
||||
// Engine renders the PDF format and may be nil when PDF is not requested.
|
||||
//
|
||||
// When InputFile is set only that file is converted and Output, if given, is the output name:
|
||||
// a base name that gets the format's extension (or a full name when it already has the extension
|
||||
// of the single requested format). Otherwise Output, if given, is a directory that receives the
|
||||
// results, mirroring the layout under InputDir. With neither, output goes next to each source.
|
||||
type Converter struct {
|
||||
InputDir string
|
||||
Recursive bool
|
||||
Parallel bool
|
||||
InputFile string
|
||||
Output string
|
||||
Formats []string
|
||||
Engine engine.Engine
|
||||
}
|
||||
|
||||
// outputPath returns the file written for inputFile in the given format.
|
||||
func outputPath(inputFile, format string) string {
|
||||
return strings.TrimSuffix(inputFile, filepath.Ext(inputFile)) + "." + format
|
||||
func (c *Converter) formats() []string {
|
||||
if len(c.Formats) == 0 {
|
||||
return []string{FormatPDF}
|
||||
}
|
||||
return c.Formats
|
||||
}
|
||||
|
||||
// Run converts every Markdown file under InputDir.
|
||||
// Validate checks the input and output options before any work is done.
|
||||
func (c *Converter) Validate() error {
|
||||
if c.InputFile != "" {
|
||||
info, err := os.Stat(c.InputFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("input file: %w", err)
|
||||
}
|
||||
if info.IsDir() {
|
||||
return fmt.Errorf("input %s is a directory; use -dir to scan a directory", c.InputFile)
|
||||
}
|
||||
if !isMarkdown(c.InputFile) {
|
||||
return fmt.Errorf("input %s must be a .md or .markdown file", c.InputFile)
|
||||
}
|
||||
}
|
||||
|
||||
if c.Output == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
if c.InputFile == "" {
|
||||
if info, err := os.Stat(c.Output); err == nil && !info.IsDir() {
|
||||
return fmt.Errorf("output %s exists and is not a directory", c.Output)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if info, err := os.Stat(c.Output); err == nil && info.IsDir() {
|
||||
return fmt.Errorf("output %s is a directory; give a file name when using -input", c.Output)
|
||||
}
|
||||
if ext := strings.TrimPrefix(strings.ToLower(filepath.Ext(c.Output)), "."); slices.Contains(FormatNames, ext) {
|
||||
if formats := c.formats(); len(formats) != 1 || formats[0] != ext {
|
||||
return fmt.Errorf("output %s has a .%s extension but the requested formats are %s",
|
||||
c.Output, ext, strings.Join(formats, ","))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// outputFor returns the file written for input in the given format.
|
||||
func (c *Converter) outputFor(input, format string) string {
|
||||
switch {
|
||||
case c.InputFile != "" && c.Output != "":
|
||||
ext := strings.TrimPrefix(strings.ToLower(filepath.Ext(c.Output)), ".")
|
||||
if ext == format {
|
||||
return c.Output
|
||||
}
|
||||
return c.Output + "." + format
|
||||
case c.InputFile == "" && c.Output != "":
|
||||
rel, err := filepath.Rel(c.InputDir, input)
|
||||
if err != nil {
|
||||
rel = filepath.Base(input)
|
||||
}
|
||||
return filepath.Join(c.Output, replaceExt(rel, format))
|
||||
default:
|
||||
return replaceExt(input, format)
|
||||
}
|
||||
}
|
||||
|
||||
func replaceExt(path, ext string) string {
|
||||
return strings.TrimSuffix(path, filepath.Ext(path)) + "." + ext
|
||||
}
|
||||
|
||||
// Run converts InputFile, or every Markdown file under InputDir.
|
||||
func (c *Converter) Run() error {
|
||||
files, err := c.findMarkdownFiles()
|
||||
if err := c.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
files, err := c.inputFiles()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to find markdown files: %w", err)
|
||||
}
|
||||
@@ -86,6 +161,13 @@ func isMarkdown(path string) bool {
|
||||
return strings.HasSuffix(path, ".md") || strings.HasSuffix(path, ".markdown")
|
||||
}
|
||||
|
||||
func (c *Converter) inputFiles() ([]string, error) {
|
||||
if c.InputFile != "" {
|
||||
return []string{c.InputFile}, nil
|
||||
}
|
||||
return c.findMarkdownFiles()
|
||||
}
|
||||
|
||||
func (c *Converter) findMarkdownFiles() ([]string, error) {
|
||||
var files []string
|
||||
|
||||
@@ -150,13 +232,11 @@ func (c *Converter) convertFile(inputFile string) error {
|
||||
return fmt.Errorf("failed to read file: %w", err)
|
||||
}
|
||||
|
||||
formats := c.Formats
|
||||
if len(formats) == 0 {
|
||||
formats = []string{FormatPDF}
|
||||
}
|
||||
|
||||
for _, format := range formats {
|
||||
outputFile := outputPath(inputFile, format)
|
||||
for _, format := range c.formats() {
|
||||
outputFile := c.outputFor(inputFile, format)
|
||||
if err := os.MkdirAll(filepath.Dir(outputFile), 0o755); err != nil {
|
||||
return fmt.Errorf("failed to create output directory: %w", err)
|
||||
}
|
||||
if err := c.write(mdContent, format, outputFile); err != nil {
|
||||
return fmt.Errorf("%s: %w", format, err)
|
||||
}
|
||||
|
||||
@@ -101,3 +101,103 @@ func TestRunWritesAllFormats(t *testing.T) {
|
||||
t.Error("doc.pdf should not be written")
|
||||
}
|
||||
}
|
||||
|
||||
func writeFile(t *testing.T, path, content string) {
|
||||
t.Helper()
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func exists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func TestInputFileWithOutputName(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeFile(t, filepath.Join(dir, "a.md"), "# A")
|
||||
writeFile(t, filepath.Join(dir, "b.md"), "# B")
|
||||
|
||||
conv := &Converter{
|
||||
InputFile: filepath.Join(dir, "a.md"),
|
||||
Output: filepath.Join(dir, "out", "report"),
|
||||
Formats: []string{FormatHTML, FormatDOCX},
|
||||
}
|
||||
if err := conv.Run(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, want := range []string{"out/report.html", "out/report.docx"} {
|
||||
if !exists(filepath.Join(dir, want)) {
|
||||
t.Errorf("expected %s", want)
|
||||
}
|
||||
}
|
||||
if exists(filepath.Join(dir, "b.html")) || exists(filepath.Join(dir, "a.html")) {
|
||||
t.Error("only the output name should be written, and only the input file converted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInputFileOutputWithMatchingExtension(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeFile(t, filepath.Join(dir, "a.md"), "# A")
|
||||
|
||||
conv := &Converter{InputFile: filepath.Join(dir, "a.md"), Output: filepath.Join(dir, "x.html"), Formats: []string{FormatHTML}}
|
||||
if err := conv.Run(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !exists(filepath.Join(dir, "x.html")) || exists(filepath.Join(dir, "x.html.html")) {
|
||||
t.Error("an output name that already has the format's extension should be used as is")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOutputValidation(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
writeFile(t, filepath.Join(dir, "a.md"), "# A")
|
||||
writeFile(t, filepath.Join(dir, "notes.txt"), "text")
|
||||
md := filepath.Join(dir, "a.md")
|
||||
|
||||
cases := map[string]*Converter{
|
||||
"missing input": {InputFile: filepath.Join(dir, "nope.md")},
|
||||
"input is a dir": {InputFile: dir},
|
||||
"input not markdown": {InputFile: filepath.Join(dir, "notes.txt")},
|
||||
"output is a dir": {InputFile: md, Output: dir},
|
||||
"extension mismatch": {InputFile: md, Output: filepath.Join(dir, "x.docx"), Formats: []string{FormatHTML}},
|
||||
"ext with many fmts": {InputFile: md, Output: filepath.Join(dir, "x.html"), Formats: []string{FormatHTML, FormatDOCX}},
|
||||
"scan output is file": {InputDir: dir, Output: md},
|
||||
}
|
||||
for name, c := range cases {
|
||||
if err := c.Validate(); err == nil {
|
||||
t.Errorf("%s: expected a validation error", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOutputDirMirrorsLayout(t *testing.T) {
|
||||
src, out := t.TempDir(), filepath.Join(t.TempDir(), "dist")
|
||||
writeFile(t, filepath.Join(src, "a.md"), "# A")
|
||||
writeFile(t, filepath.Join(src, "sub", "b.md"), "# B")
|
||||
|
||||
conv := &Converter{InputDir: src, Recursive: true, Output: out, Formats: []string{FormatHTML}}
|
||||
if err := conv.Run(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, want := range []string{"a.html", "sub/b.html"} {
|
||||
if !exists(filepath.Join(out, want)) {
|
||||
t.Errorf("expected %s in output dir", want)
|
||||
}
|
||||
}
|
||||
if exists(filepath.Join(src, "a.html")) {
|
||||
t.Error("nothing should be written next to the sources when -output is a directory")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHookRejectsInputAndOutput(t *testing.T) {
|
||||
if err := (&Converter{InputFile: "a.md"}).RunAsHook(); err == nil {
|
||||
t.Error("expected error combining hook with -input")
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,10 @@ import (
|
||||
|
||||
// RunAsHook converts the staged Markdown files and stages the generated output.
|
||||
func (c *Converter) RunAsHook() error {
|
||||
if c.InputFile != "" || c.Output != "" {
|
||||
return fmt.Errorf("-input and -output cannot be combined with -hook")
|
||||
}
|
||||
|
||||
if !confirmConversion() {
|
||||
fmt.Println("Skipping conversion")
|
||||
return nil
|
||||
@@ -64,14 +68,9 @@ func stagedMarkdownFiles() ([]string, error) {
|
||||
}
|
||||
|
||||
func (c *Converter) stageGeneratedFiles(files []string) error {
|
||||
formats := c.Formats
|
||||
if len(formats) == 0 {
|
||||
formats = []string{FormatPDF}
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
for _, format := range formats {
|
||||
out := filepath.FromSlash(outputPath(file, format))
|
||||
for _, format := range c.formats() {
|
||||
out := filepath.FromSlash(c.outputFor(file, format))
|
||||
if err := exec.Command("git", "add", out).Run(); err != nil {
|
||||
fmt.Printf("Warning: Could not stage %s\n", out)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user