tex

package
v1.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2025 License: Apache-2.0 Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Copyright 2022 AutoPDF Pedro Branquinho SPDX-License-Identifier: Apache-2.0

Index

Constants

This section is empty.

Variables

View Source
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)
				}
			}
		}

		if cfg.Output != "" {

			outputPath := cfg.Output.String()
			if !strings.HasSuffix(outputPath, ".pdf") {
				outputPath = outputPath + ".pdf"
			}

			fileInfo, err := os.Stat(outputPath)
			if err != nil {
				if os.IsNotExist(err) {
					log.Printf("Warning: Output file %s doesn't exist after compilation", outputPath)

				} else {
					log.Printf("Error checking output file: %s", err)
				}
			} else if fileInfo.Size() == 0 {
				log.Printf("Warning: Output file %s exists but is empty", outputPath)
			}

			outputPDF = cfg.Output.String()
		}
		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
	},
}
View Source
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

View Source
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

func Default

func Default(texFilePath string) error

Types

type Cleaner

type Cleaner struct {
	Directory string
}

Cleaner handles the removal of auxiliary LaTeX files

func NewCleaner

func NewCleaner(directory string) *Cleaner

NewCleaner creates a new cleaner for LaTeX auxiliary files

func (*Cleaner) Clean

func (c *Cleaner) Clean() error

Clean removes all auxiliary files in the specified directory

type Compiler

type Compiler struct {
	Config *config.Config
}

Compiler handles LaTeX compilation

func NewCompiler

func NewCompiler(cfg *config.Config) *Compiler

NewCompiler creates a new LaTeX compiler

func (*Compiler) Compile

func (c *Compiler) Compile(texFile string) (string, error)

Compile processes a LaTeX file into PDF using the specified LaTeX engine

func (*Compiler) CompileWithBibtex

func (c *Compiler) CompileWithBibtex(texFile string) (string, error)

CompileWithBibtex runs LaTeX with BibTeX for bibliography processing

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL