Documentation
¶
Overview ¶
Copyright 2022 AutoPDF Pedro Branquinho SPDX-License-Identifier: Apache-2.0
Index ¶
Constants ¶
This section is empty.
Variables ¶
var BuildCmd = &bonzai.Cmd{ Name: `build`, Alias: `b`, Short: `process template and compile to PDF`, Usage: `TEMPLATE [CONFIG] [CLEAN]`, MinArgs: 1, MaxArgs: 3, Long: ` The build command processes a template file using variables from a configuration, compiles the processed template to LaTeX, and produces a PDF output. If no configuration file is provided, it will look for autopdf.yaml in the current directory. `, Comp: comp.Cmds, Cmds: []*bonzai.Cmd{ help.Cmd, }, Do: func(cmd *bonzai.Cmd, args ...string) error { log.Println("Building PDF...") templateFile := args[0] configFile := configs.DefaultConfigName log.Println("Template file:", templateFile) log.Println("Config file:", configFile) if len(args) > 1 { log.Println("Using provided config file:", args[1]) configFile = args[1] } else { log.Println("No config file provided, creating default config file...") err := Default(templateFile) if err != nil { return configs.BuildError } log.Println("Default config file written to:", configs.DefaultConfigName) configFile = configs.DefaultConfigName } yamlData, err := os.ReadFile(configFile) if err != nil { return configs.ReadError } cfg, err := config.NewConfigFromYAML(yamlData) if err != nil { return configs.ParseError } if cfg.Template == "" { cfg.Template = config.Template(templateFile) } engine := template.NewEngine(cfg) tempDir, err := os.Getwd() if err != nil { return configs.BuildError } processedTexFile := filepath.Join(tempDir, "autopdf_"+filepath.Base(cfg.Template.String())) result, err := engine.Process(cfg.Template.String()) if err != nil { return configs.TemplateError } if err := os.WriteFile(processedTexFile, []byte(result), 0644); err != nil { return configs.WriteError } compiler := NewCompiler(cfg) outputPDF, err := compiler.Compile(processedTexFile) if err != nil { log.Printf("Error compiling: %s", err) return configs.BuildError } if cfg.Output != "" && outputPDF != cfg.Output.String() { outputDir := filepath.Dir(cfg.Output.String()) if err := os.MkdirAll(outputDir, 0755); err != nil { return configs.BuildError } pdfData, err := os.ReadFile(outputPDF) if err != nil { return configs.ReadError } if err := os.WriteFile(cfg.Output.String(), pdfData, 0644); err != nil { return configs.WriteError } outputPDF = cfg.Output.String() } os.Remove(processedTexFile) if cfg.Conversion.Enabled { conv := converter.NewConverter(cfg) imageFiles, err := conv.ConvertPDFToImages(outputPDF) if err != nil { return fmt.Errorf("PDF to image conversion failed: %w", err) } if len(imageFiles) > 0 { fmt.Println("Generated image files:") for _, file := range imageFiles { fmt.Printf(" - %s\n", file) } } } fmt.Printf("Successfully built PDF: %s\n", outputPDF) if len(args) > 2 && args[2] == "clean" { dir := filepath.Dir(outputPDF) baseName := filepath.Base(outputPDF) outputPDF := filepath.Join(dir, replaceExt(baseName, ".pdf")) if cfg.Output.String() != "" { outputPDF = cfg.Output.String() } dirOutput := filepath.Dir(outputPDF) if err := CleanCmd.Do(cmd, dirOutput); err != nil { return configs.CleanError } } return nil }, }
var CleanCmd = &bonzai.Cmd{ Name: `clean`, Short: `remove LaTeX auxiliary files`, Long: ` The clean command removes auxiliary files created during LaTeX compilation. These include .aux, .log, .toc, and other temporary files. By default, it cleans the current directory. You can specify a different directory as an argument. `, Do: func(caller *bonzai.Cmd, args ...string) error { dir := "." if len(args) > 0 { dir = args[0] } cleaner := NewCleaner(dir) if err := cleaner.Clean(); err != nil { return err } fmt.Printf("Successfully cleaned auxiliary files in: %s\n", dir) return nil }, }
CleanCmd is the bonzai command for cleaning auxiliary LaTeX files
var CompileCmd = &bonzai.Cmd{ Name: `compile`, Do: func(caller *bonzai.Cmd, args ...string) error { if len(args) == 0 { return errors.New("no LaTeX file specified") } texFile := args[0] cfg := config.Config{ Engine: "pdflatex", Output: config.Output(strings.TrimSuffix(texFile, ".tex")), } compiler := NewCompiler(&cfg) outputPDF, err := compiler.Compile(texFile) if err != nil { log.Printf("Error compiling: %s", err) return configs.BuildError } fmt.Printf("Successfully compiled: %s\n", outputPDF) if len(args) > 1 && args[1] == "clean" { if err := CleanCmd.Do(caller); err != nil { return configs.CleanError } } return nil }, }
CompileCmd is the bonzai command for LaTeX compilation
Functions ¶
Types ¶
type Cleaner ¶
type Cleaner struct {
Directory string
}
Cleaner handles the removal of auxiliary LaTeX files
func NewCleaner ¶
NewCleaner creates a new cleaner for LaTeX auxiliary files