Skip to content
Snippets Groups Projects
pdf.go 11.28 KiB
package document

import (
	"fmt"
	"gitlab.schukai.com/oss/utilities/documentation-manager/environment"
	"gitlab.schukai.com/oss/utilities/documentation-manager/translations"
	"gitlab.schukai.com/oss/utilities/documentation-manager/utils"
	"io/ioutil"
	"os"
	"path"
	"path/filepath"
	"regexp"
	"strings"
	"text/template"
)

type BuildPdfEnvironment struct {
	SourcePath string
	DateFormat string
	OutputPath string
	Verbose    bool
	Templates  struct {
		Latex    string
		Markdown string
	}
}

type PdfDataset struct {
	Documents string
}

func NewPdfDataset(env BuildPdfEnvironment) (*PdfDataset, error) {

	files, err := getFiles(env.SourcePath)
	if err != nil {
		return nil, err
	}

	mapFiles, keys := buildFileMap(files)

	d := &PdfDataset{}

	docs := []string{}
	for _, key := range keys {

		text := mapFiles[key].textMeta.text

		text, s1Map := utils.MaskCodeBlocks(text, mapFiles[key].relSourcePath, 3)
		text, s2Map := utils.MaskCodeBlocks(text, mapFiles[key].relSourcePath, 1)

		text = convertHeadlines(text, mapFiles[key].level, mapFiles[key].textMeta.meta.Level)
		text = convertAwesomeBoxes(text)
		text = convertImages(text, mapFiles[key].baseDir)
		text = convertCircledNumbers(text)
		text = replaceKbd(text)
		text = replaceRelativeLinks(text, mapFiles[key], mapFiles)

		text = utils.InsertCodeBlocks(text, s2Map)
		text = utils.InsertCodeBlocks(text, s1Map)

		docs = append(docs, "\\newpage{}"+text)
	}

	d.Documents = strings.Join(docs, "\n")

	if env.Verbose {
		fmt.Println(d.Documents)
	}

	return d, nil