Skip to content

Commit

Permalink
feat: png support with rsvg-convert
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Feb 1, 2024
1 parent 10f98a9 commit 3addfbc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
9 changes: 4 additions & 5 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (
"github.com/charmbracelet/lipgloss"
)

var ErrorPadding = lipgloss.NewStyle().Padding(1, 2)
var ErrorHeader = lipgloss.NewStyle().Foreground(lipgloss.Color("#F1F1F1")).Background(lipgloss.Color("#FF5F87")).Bold(true).Padding(0, 1).SetString("ERROR")
var ErrorDetails = lipgloss.NewStyle().Foreground(lipgloss.Color("#757575"))
var ErrorHeader = lipgloss.NewStyle().Foreground(lipgloss.Color("#F1F1F1")).Background(lipgloss.Color("#FF5F87")).Bold(true).Padding(0, 1).Margin(1).MarginLeft(2).SetString("ERROR")
var ErrorDetails = lipgloss.NewStyle().Foreground(lipgloss.Color("#757575")).Margin(0, 0, 1, 2)

func printError(title string, err error) {
fmt.Printf("%s", ErrorPadding.Render(ErrorHeader.String(), title))
fmt.Printf("%s\n", ErrorPadding.Render(ErrorDetails.Render(err.Error())))
fmt.Printf("%s\n", lipgloss.JoinHorizontal(lipgloss.Center, ErrorHeader.String(), title))
fmt.Printf("%s\n", ErrorDetails.Render(err.Error()))
}

func printErrorFatal(title string, err error) {
Expand Down
24 changes: 24 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"strconv"
"strings"

"github.com/alecthomas/chroma"
Expand All @@ -19,6 +21,8 @@ import (
"github.com/mattn/go-isatty"
)

const pngExportMultiplier = 3

func main() {
var (
input string
Expand Down Expand Up @@ -190,6 +194,26 @@ func main() {

switch {
case strings.HasSuffix(config.Output, ".png"):
_, err := exec.LookPath("rsvg-conver")
if err != nil {
printErrorFatal("PNG convert failure", fmt.Errorf("rsvg-convert required to convert to png, install librsvg."))
}
rsvgConvert := exec.Command("rsvg-convert",
"--width", strconv.Itoa(w*pngExportMultiplier),
"--keep-aspect-ratio",
"-f", "png",
"-o", config.Output,
)
svg, err := doc.WriteToString()
if err != nil {
printErrorFatal("Unable to write output", err)
}
rsvgConvert.Stdin = strings.NewReader(svg)
err = rsvgConvert.Run()
if err != nil {
printErrorFatal("Unable to write output", err)
}

case strings.HasSuffix(config.Output, ".svg"):
if istty {
err = doc.WriteToFile(config.Output)
Expand Down
1 change: 1 addition & 0 deletions svg/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func AddShadow(element *etree.Element, id string, x, y, blur int) {
f.CreateAttr("id", id)
f.CreateAttr("x", "0")
f.CreateAttr("y", "0")
f.CreateAttr("filterUnits", "userSpaceOnUse")

o := etree.NewElement("feOffset")
o.CreateAttr("result", "offOut")
Expand Down

0 comments on commit 3addfbc

Please sign in to comment.