mirror of
https://github.com/Warky-Devs/go-mdtopdf-helper.git
synced 2025-05-18 23:27:30 +00:00
Updated os issues and release conf
This commit is contained in:
parent
54e7287ae6
commit
8c14862ae7
6
.github/workflows/ci.yaml
vendored
6
.github/workflows/ci.yaml
vendored
@ -19,6 +19,12 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
go-version: '1.21'
|
go-version: '1.21'
|
||||||
|
|
||||||
|
- name: Install wkhtmltopdf
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install -y wkhtmltopdf
|
||||||
|
wkhtmltopdf --version
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: go mod download
|
run: go mod download
|
||||||
|
|
||||||
|
@ -0,0 +1,59 @@
|
|||||||
|
before:
|
||||||
|
hooks:
|
||||||
|
- go mod tidy
|
||||||
|
|
||||||
|
builds:
|
||||||
|
- env:
|
||||||
|
- CGO_ENABLED=0
|
||||||
|
goos:
|
||||||
|
- linux
|
||||||
|
- windows
|
||||||
|
- darwin
|
||||||
|
goarch:
|
||||||
|
- amd64
|
||||||
|
- arm64
|
||||||
|
ignore:
|
||||||
|
- goos: windows
|
||||||
|
goarch: arm64
|
||||||
|
binary: go-mdtopdf-helper
|
||||||
|
ldflags:
|
||||||
|
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}
|
||||||
|
|
||||||
|
archives:
|
||||||
|
- format: tar.gz
|
||||||
|
name_template: >-
|
||||||
|
{{ .ProjectName }}_
|
||||||
|
{{- title .Os }}_
|
||||||
|
{{- if eq .Arch "amd64" }}x86_64
|
||||||
|
{{- else if eq .Arch "arm64" }}arm64
|
||||||
|
{{- else }}{{ .Arch }}{{ end }}
|
||||||
|
format_overrides:
|
||||||
|
- goos: windows
|
||||||
|
format: zip
|
||||||
|
|
||||||
|
checksum:
|
||||||
|
name_template: 'checksums.txt'
|
||||||
|
|
||||||
|
changelog:
|
||||||
|
sort: asc
|
||||||
|
filters:
|
||||||
|
exclude:
|
||||||
|
- '^docs:'
|
||||||
|
- '^test:'
|
||||||
|
- '^ci:'
|
||||||
|
- Merge pull request
|
||||||
|
- Merge branch
|
||||||
|
|
||||||
|
release:
|
||||||
|
prerelease: auto
|
||||||
|
|
||||||
|
brews:
|
||||||
|
- name: go-mdtopdf-helper
|
||||||
|
homepage: "https://github.com/yourusername/go-mdtopdf-helper"
|
||||||
|
description: "A tool to convert Markdown files to PDF using wkhtmltopdf"
|
||||||
|
tap:
|
||||||
|
owner: yourusername
|
||||||
|
name: homebrew-tools
|
||||||
|
commit_author:
|
||||||
|
name: goreleaserbot
|
||||||
|
email: bot@goreleaser.com
|
BIN
README.pdf
BIN
README.pdf
Binary file not shown.
74
main.go
74
main.go
@ -8,6 +8,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@ -16,6 +17,12 @@ import (
|
|||||||
"github.com/gomarkdown/markdown/parser"
|
"github.com/gomarkdown/markdown/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Windows = "windows"
|
||||||
|
Linux = "linux"
|
||||||
|
MacOS = "darwin"
|
||||||
|
)
|
||||||
|
|
||||||
type Converter struct {
|
type Converter struct {
|
||||||
inputDir string
|
inputDir string
|
||||||
recursive bool
|
recursive bool
|
||||||
@ -29,21 +36,64 @@ func init() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ensureWkhtmltopdfInPath() error {
|
func ensureWkhtmltopdfInPath() error {
|
||||||
wkhtmlPath := `C:\Program Files\wkhtmltopdf\bin`
|
var wkhtmlPath string
|
||||||
|
pathSeparator := string(os.PathListSeparator)
|
||||||
|
|
||||||
|
switch runtime.GOOS {
|
||||||
|
case Windows:
|
||||||
|
wkhtmlPath = `C:\Program Files\wkhtmltopdf\bin`
|
||||||
|
case Linux:
|
||||||
|
// Common Linux installation paths
|
||||||
|
possiblePaths := []string{
|
||||||
|
"/usr/local/bin",
|
||||||
|
"/usr/bin",
|
||||||
|
"/opt/wkhtmltopdf/bin",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, path := range possiblePaths {
|
||||||
|
if _, err := os.Stat(filepath.Join(path, "wkhtmltopdf")); err == nil {
|
||||||
|
wkhtmlPath = path
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case MacOS:
|
||||||
|
// Common MacOS installation paths
|
||||||
|
possiblePaths := []string{
|
||||||
|
"/usr/local/bin",
|
||||||
|
"/opt/homebrew/bin",
|
||||||
|
"/opt/local/bin",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, path := range possiblePaths {
|
||||||
|
if _, err := os.Stat(filepath.Join(path, "wkhtmltopdf")); err == nil {
|
||||||
|
wkhtmlPath = path
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unsupported operating system: %s", runtime.GOOS)
|
||||||
|
}
|
||||||
|
|
||||||
|
if wkhtmlPath == "" {
|
||||||
|
return fmt.Errorf("wkhtmltopdf not found in common installation paths")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the directory exists
|
||||||
if _, err := os.Stat(wkhtmlPath); os.IsNotExist(err) {
|
if _, err := os.Stat(wkhtmlPath); os.IsNotExist(err) {
|
||||||
return fmt.Errorf("wkhtmltopdf directory not found at: %s", wkhtmlPath)
|
return fmt.Errorf("wkhtmltopdf directory not found at: %s", wkhtmlPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get current PATH
|
||||||
currentPath := os.Getenv("PATH")
|
currentPath := os.Getenv("PATH")
|
||||||
|
|
||||||
|
// Check if the path is already in PATH
|
||||||
if strings.Contains(currentPath, wkhtmlPath) {
|
if strings.Contains(currentPath, wkhtmlPath) {
|
||||||
return nil // Already in PATH
|
return nil // Already in PATH
|
||||||
}
|
}
|
||||||
|
|
||||||
newPath := currentPath + ";" + wkhtmlPath
|
// Add to PATH using OS-specific path separator
|
||||||
|
newPath := currentPath + pathSeparator + wkhtmlPath
|
||||||
if err := os.Setenv("PATH", newPath); err != nil {
|
if err := os.Setenv("PATH", newPath); err != nil {
|
||||||
return fmt.Errorf("failed to update PATH: %w", err)
|
return fmt.Errorf("failed to update PATH: %w", err)
|
||||||
}
|
}
|
||||||
@ -52,6 +102,14 @@ func ensureWkhtmltopdfInPath() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add this helper function to get the executable name based on OS
|
||||||
|
func getExecutableName() string {
|
||||||
|
if runtime.GOOS == Windows {
|
||||||
|
return "wkhtmltopdf.exe"
|
||||||
|
}
|
||||||
|
return "wkhtmltopdf"
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
conv := &Converter{}
|
conv := &Converter{}
|
||||||
|
|
||||||
@ -135,6 +193,8 @@ func (c *Converter) getStagedMarkdownFiles() ([]string, error) {
|
|||||||
func (c *Converter) stageGeneratedPDFs(files []string) error {
|
func (c *Converter) stageGeneratedPDFs(files []string) error {
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
pdfFile := strings.TrimSuffix(file, filepath.Ext(file)) + ".pdf"
|
pdfFile := strings.TrimSuffix(file, filepath.Ext(file)) + ".pdf"
|
||||||
|
// Convert to OS-specific path
|
||||||
|
pdfFile = filepath.FromSlash(pdfFile)
|
||||||
cmd := exec.Command("git", "add", pdfFile)
|
cmd := exec.Command("git", "add", pdfFile)
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
fmt.Printf("Warning: Could not stage %s\n", pdfFile)
|
fmt.Printf("Warning: Could not stage %s\n", pdfFile)
|
||||||
@ -239,9 +299,15 @@ func (c *Converter) convertFile(inputFile string) error {
|
|||||||
|
|
||||||
pdfg, err := wkhtmltopdf.NewPDFGenerator()
|
pdfg, err := wkhtmltopdf.NewPDFGenerator()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if runtime.GOOS == Windows {
|
||||||
|
// Check if wkhtmltopdf exists in the expected path
|
||||||
|
expectedPath := filepath.Join(`C:\Program Files\wkhtmltopdf\bin`, "wkhtmltopdf.exe")
|
||||||
|
if _, err := os.Stat(expectedPath); os.IsNotExist(err) {
|
||||||
|
return fmt.Errorf("wkhtmltopdf not found at %s. Please ensure it's installed correctly", expectedPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
return fmt.Errorf("failed to create PDF generator: %w", err)
|
return fmt.Errorf("failed to create PDF generator: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
page := wkhtmltopdf.NewPageReader(strings.NewReader(string(html)))
|
page := wkhtmltopdf.NewPageReader(strings.NewReader(string(html)))
|
||||||
page.EnableLocalFileAccess.Set(true)
|
page.EnableLocalFileAccess.Set(true)
|
||||||
pdfg.AddPage(page)
|
pdfg.AddPage(page)
|
||||||
|
Loading…
Reference in New Issue
Block a user