Skip to content

Commit

Permalink
feat: add ability to customize the output font
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishb committed Mar 18, 2024
1 parent 6d99d5e commit 154695c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Written in Go.
1. [x] Migrate "Show more..." of WordPress -> `Summary` in Hugo
1. [x] Support for parallax blur (similar to [WordPress Advanced Backgrounds](https://wordpress.org/plugins/advanced-backgrounds/))
1. [x] Migrate WordPress table of content -> Hugo
1. [x] Custom font - defaults to Lexend
1. [ ] Migrate code blocks correctly - syntax highlighting is not working right now
1. [ ] Featured images - I tried this [WordPress plugin](https://wordpress.org/plugins/export-media-with-selected-content/) but featured images are simply not exported

Expand Down
8 changes: 5 additions & 3 deletions src/wp2hugo/cmd/wp2hugo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ var (
// This is useful for repeated executions of the tool to avoid downloading the media files again
// Mostly for development and not for the production use
mediaCacheDir = flag.String("media-cache-dir", path.Join("/tmp/wp2hugo-cache"), "dir path to cache the downloaded media files")
// Custom font for Hugo's papermod theme
font = flag.String("font", "Lexend", "custom font for the output website")
)

func main() {
Expand All @@ -43,7 +45,7 @@ func handle(filePath string) error {
if err != nil {
return err
}
return generate(*websiteInfo, *outputDir, *downloadMedia)
return generate(*websiteInfo, *outputDir)
}

func getWebsiteInfo(filePath string) (*wpparser.WebsiteInfo, error) {
Expand All @@ -55,8 +57,8 @@ func getWebsiteInfo(filePath string) (*wpparser.WebsiteInfo, error) {
return parser.Parse(file)
}

func generate(info wpparser.WebsiteInfo, outputDirPath string, downloadMedia bool) error {
func generate(info wpparser.WebsiteInfo, outputDirPath string) error {
log.Debug().Msgf("Output: %s", outputDirPath)
generator := hugogenerator.NewGenerator(outputDirPath, downloadMedia, mediacache.New(*mediaCacheDir), info)
generator := hugogenerator.NewGenerator(outputDirPath, *downloadMedia, *font, mediacache.New(*mediaCacheDir), info)
return generator.Generate()
}
52 changes: 52 additions & 0 deletions src/wp2hugo/internal/hugogenerator/hugo_custom_font.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package hugogenerator

import (
"errors"
"fmt"
"github.com/rs/zerolog/log"
"os"
"path/filepath"
)

const _extendedHeaderData = `
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=%s&display=swap" rel="stylesheet">
`

const _outputHeadFile = "themes/PaperMod/layouts/partials/extend_head.html"

const _customFontCSS = `
body {
font-family: '%s', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
font-size: 18px;
line-height: 1.6;
word-break: break-word;
background: var(--theme);
}
`

const _outputCssFile = "themes/PaperMod/assets/css/extended/blank.css"

// Custom font for Hugo's papermod theme
// Ref: https://forum.wildserver.ru/viewtopic.php?t=18
func setupFont(siteDir string, fontName string) error {
err1 := appendFile(filepath.Join(siteDir, _outputHeadFile), fmt.Sprintf(_extendedHeaderData, fontName))
err2 := appendFile(filepath.Join(siteDir, _outputCssFile), fmt.Sprintf(_customFontCSS, fontName))
return errors.Join(err1, err2)
}

func appendFile(outputFilePath string, data string) error {
log.Info().
Str("location", outputFilePath).
Msgf("Writing custom font to %s", outputFilePath)
f, err := os.OpenFile(outputFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return err
}
defer f.Close()
if _, err := f.WriteString(data); err != nil {
return err
}
return nil
}
10 changes: 8 additions & 2 deletions src/wp2hugo/internal/hugogenerator/hugo_gen_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ placeholder: "placeholder text in search input box"
`

type Generator struct {
fontName string
imageURLProvider hugopage.ImageURLProvider
outputDirPath string
downloadMedia bool
Expand All @@ -46,8 +47,10 @@ type MediaProvider interface {
GetReader(url string) (io.Reader, error)
}

func NewGenerator(outputDirPath string, downloadMedia bool, mediaProvider MediaProvider, info wpparser.WebsiteInfo) *Generator {
func NewGenerator(outputDirPath string, downloadMedia bool, fontName string,
mediaProvider MediaProvider, info wpparser.WebsiteInfo) *Generator {
return &Generator{
fontName: fontName,
imageURLProvider: newImageURLProvider(info),
outputDirPath: outputDirPath,
mediaProvider: mediaProvider,
Expand Down Expand Up @@ -77,6 +80,9 @@ func (g Generator) Generate() error {
if err = setupSearchPage(*siteDir); err != nil {
return err
}
if err = setupFont(*siteDir, g.fontName); err != nil {
return err
}
if err = WriteCustomShortCodes(*siteDir); err != nil {
return err
}
Expand All @@ -91,7 +97,7 @@ func (g Generator) Generate() error {
if err != nil {
return fmt.Errorf("error fetching media file %s: %s", url1, err)
}
if err = writeFavicon(*siteDir, media); err != nil {
if err = writeFavicon(path.Join(*siteDir, "static"), media); err != nil {
return err
}
}
Expand Down

0 comments on commit 154695c

Please sign in to comment.