Skip to content

Commit

Permalink
Update deps and use correct page flag for inkscape version
Browse files Browse the repository at this point in the history
  • Loading branch information
oxplot committed Aug 30, 2023
1 parent c6371e0 commit f5a93cd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/oxplot/pdfrankenstein

go 1.18

require github.com/gotk3/gotk3 v0.6.1
require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/gotk3/gotk3 v0.6.2
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/gotk3/gotk3 v0.6.1 h1:GJ400a0ecEEWrzjBvzBzH+pB/esEMIGdB9zPSmBdoeo=
github.com/gotk3/gotk3 v0.6.1/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q=
github.com/gotk3/gotk3 v0.6.2 h1:sx/PjaKfKULJPTPq8p2kn2ZbcNFxpOJqi4VLzMbEOO8=
github.com/gotk3/gotk3 v0.6.2/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q=
40 changes: 38 additions & 2 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"strings"
"sync"
"text/template"

"github.com/Masterminds/semver/v3"
)

var (
Expand Down Expand Up @@ -153,6 +155,25 @@ func (s *Session) Thumbnail(page int) (string, error) {
return thumbPath, nil
}

func getInkscapeVersion() (*semver.Version, error) {

cmd := exec.Command("inkscape", "--version")
verBytes, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("failed to get inkscape version: %s", cmdErr(err))
}
verMatch := regexp.MustCompile(`(?i)inkscape\s+([0-9.]+)`).FindStringSubmatch(string(verBytes))
if verMatch == nil {
return nil, fmt.Errorf("failed to parse inkscape version: %s", string(verBytes))
}
sv, err := semver.NewVersion(verMatch[1])
if err != nil {
return nil, fmt.Errorf("failed to parse inkscape version: %s", string(verMatch[1]))
}

return sv, nil
}

// Annotate blocks and launches Inkscape to annotate the page.
// It returns true if the page was annotated by the user this time around.
func (s *Session) Annotate(page int) (bool, error) {
Expand All @@ -165,10 +186,25 @@ func (s *Session) Annotate(page int) (bool, error) {

srcPath := s.srcPath(page)
if _, err := os.Stat(srcPath); err != nil {
cmd := exec.Command("inkscape", "--pages="+strconv.Itoa(page+1), "--export-type=svg",

// Page selection flag has changed between Inkscape versions. Check the
// inkscape version first.

sv, err := getInkscapeVersion()
if err != nil {
return false, err
}
var pagesFlag string
if sv.LessThan(semver.MustParse("1.3.0")) {
pagesFlag = "--pdf-page="
} else {
pagesFlag = "--pages="
}

cmd := exec.Command("inkscape", pagesFlag+strconv.Itoa(page+1), "--export-type=svg",
"--pdf-poppler", "--export-filename="+srcPath+".svg", s.path)
if _, err := cmd.Output(); err != nil {
return false, fmt.Errorf("failed to convert page %d of '%s' to svg: %s", page, s.path, cmdErr(err))
return false, fmt.Errorf("failed to convert page %d of '%s' to svg: %s", page+1, s.path, cmdErr(err))
}
_ = os.Rename(srcPath+".svg", srcPath)
}
Expand Down

0 comments on commit f5a93cd

Please sign in to comment.