Skip to content

Commit

Permalink
fix: book adjustment scripts (#253)
Browse files Browse the repository at this point in the history
* fix: pdf adjustment

* refactor: ci/cd script
  • Loading branch information
novalagung authored Jan 16, 2024
1 parent 997f26a commit 838f5cd
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 130 deletions.
32 changes: 16 additions & 16 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,30 @@ jobs:
run: |
rm -f README.md
mv content/* .
mv etc/* .
mv cover_small.jpg .
mv cover_square.png .
mv cover.jpg .
mv custom.css .
mv main.go .
mv robots.txt .
npm install -g gitbook-cli
mv ./book.json ./book.json.template
gitbook install
cd ~/.gitbook/versions/3.2.3 && npm i npm@5
cd ${{ github.workspace }}
mv ./book.json.template ./book.json
mv book.json .
gitbook install
go run fix-webbook.go -type=pre
go run main.go -mode=webbook -adjustment=pre
gitbook build
go run fix-webbook.go -type=post
go run main.go -mode=webbook -adjustment=post
cd _book
echo '<html><head><meta http-equiv="refresh" content="0; url=https://dasarpemrogramangolang.novalagung.com/"></head></html>' > 404.html
rm -rf LICENSE
rm -rf book*
rm -rf .git
rm -rf .gitignore
rm -rf .github
rm -rf in-progress
rm -rf *.md
rm -rf *.sh
rm -rf *.psd
rm -rf *.go
rm -rf *.css
rm -rf *.js
rm -rf .gitmodules
rm -rf crowdin.yml
rm -rf LICENSE
rm -rf package-lock.json
rm -rf package.json.js
- name: 'Deploying'
uses: peaceiris/actions-gh-pages@v3
with:
Expand Down Expand Up @@ -72,8 +72,8 @@ jobs:
mv content/* .
mv etc/* .
rm -f book.json
go run fix-ebook.go -type=pre
mkdir _book
go run main.go -mode=ebook -adjustment=pre
gitbook install
- name: 'Generating ebook in pdf'
run: gitbook pdf ./ ./_book/dasarpemrogramangolang.pdf
Expand Down
89 changes: 0 additions & 89 deletions etc/fix-ebook.go

This file was deleted.

114 changes: 89 additions & 25 deletions etc/fix-webbook.go → etc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,37 @@ var (
)

func main() {
flagAdjustment := flag.String("type", "", "adjustment type (pre/post)")
flagMode := flag.String("mode", "", "mode (webbook/ebook)")
flagAdjustment := flag.String("adjustment", "", "adjustment type (pre/post)")
flag.Parse()

switch *flagAdjustment {
case "pre":
preAdjustment()
case "post":
postAdjustment()
switch *flagMode {
case "webbook":
switch *flagAdjustment {
case "pre":
webbookPreAdjustment()
case "post":
webbookPostAdjustment()
default:
log.Fatalf("unrecognized flag -adjustment")
}
case "ebook":
switch *flagAdjustment {
case "pre":
ebookPreAdjustment()
default:
log.Fatalf("unrecognized flag -adjustment")
}
default:
break
log.Fatalf("unrecognized flag -mode")
}
}

func preAdjustment() {
func getVersion() string {
return fmt.Sprintf("%d.%s", baseVersion, now.Format("2006.01.02.150405"))
}

func webbookPreAdjustment() {
basePath, _ := os.Getwd()
readmePath := filepath.Join(basePath, "README.md")

Expand All @@ -52,7 +69,7 @@ func preAdjustment() {
}
}

func postAdjustment() {
func webbookPostAdjustment() {
basePath, _ := os.Getwd()
bookPath := filepath.Join(basePath, "_book")

Expand Down Expand Up @@ -149,7 +166,12 @@ func postAdjustment() {

// ==== inject github stars button
buttonToFind := `</body>`
buttonReplacement := `<div style="position: fixed; top: 10px; right: 30px; padding: 10px; background-color: rgba(255, 255, 255, 0.7);"><a class="github-button" href="https://github.com/novalagung/dasarpemrogramangolang" data-icon="octicon-star" data-size="large" data-show-count="true" aria-label="Star novalagung/dasarpemrogramangolang on GitHub">Star</a>&nbsp;<a class="github-button" href="https://github.com/novalagung" data-size="large" aria-label="Follow @novalagung on GitHub">Follow @novalagung</a><script async defer src="https://buttons.github.io/buttons.js"></script></div>` + buttonToFind
buttonReplacement := `<div style="position: fixed; top: 10px; right: 30px; padding: 10px; background-color: rgba(255, 255, 255, 0.7);">
<a class="github-button" href="https://github.com/sponsors/novalagung" data-color-scheme="no-preference: light; light: light; dark: dark;" data-icon="octicon-heart" data-size="large" aria-label="Sponsor @novalagung on GitHub">Sponsor</a>&nbsp;
<a class="github-button" href="https://github.com/novalagung/dasarpemrogramangolang" data-icon="octicon-star" data-size="large" data-show-count="true" aria-label="Star novalagung/dasarpemrogramangolang on GitHub">Star</a>&nbsp;
<a class="github-button" href="https://github.com/novalagung" data-size="large" aria-label="Follow @novalagung on GitHub">Follow @novalagung</a>
<script async defer src="https://buttons.github.io/buttons.js"></script>
</div>` + buttonToFind
htmlString = strings.ReplaceAll(htmlString, buttonToFind, buttonReplacement)

// ==== inject adjustment css
Expand Down Expand Up @@ -180,16 +202,6 @@ func postAdjustment() {
// fbPixelReplacement := `<script>!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,document,'script','https://connect.facebook.net/en_US/fbevents.js');fbq('init','1247398778924723');fbq('track','PageView');</script><noscript><imgheight="1"width="1"style="display:none"src="https://www.facebook.com/tr?id=1247398778924723&ev=PageView&noscript=1"/></noscript>` + fbPixelToFind
// htmlString = strings.Replace(htmlString, fbPixelToFind, fbPixelReplacement)

// ===== inject banner of new ebook
// bannerToFind := `</body>`
// bannerReplacement := `<a href="https://devops.novalagung.com/" target="_blank" class="book-news">Halo semua, Saya telah merilis ebook baru lo, tentang devops. Di ebook tersebut fokus tentang pembahasan banyak sekali stacks/teknologi devops, jadi tidak hanya membahas satu stack saja. Dan kabar baiknya tersedia dalam dua bahasa, Indonesia dan Inggris. Yuk mampir https://devops.novalagung.com/</a>` + bannerToFind
// htmlString = strings.Replace(htmlString, bannerToFind, bannerReplacement)

// ===== inject popup info banner if exists
// infoBannerToFind := `</body>`
// infoBannerReplacement := `<div class="banner-container" onclick="this.style.display = 'none';"><div><a target="_blank" href="https://www.udemy.com/course/praktis-belajar-docker-dan-kubernetes-untuk-pemula/"><img src="/images/banner.png?v=` + getVersion() + `"></a></div></div><script>var bannerCounter = parseInt(localStorage.getItem("banner-counter")); if (isNaN(bannerCounter)) { bannerCounter = 0; } var bannerEl = document.querySelector('.banner-container'); if (bannerCounter % 5 === 1 && bannerEl) { bannerEl.style.display = 'block'; } localStorage.setItem("banner-counter", String(bannerCounter + 1));</script>` + infoBannerToFind
// htmlString = strings.Replace(htmlString, infoBannerToFind, infoBannerReplacement)

// ==== update file
err = os.WriteFile(path, []byte(strings.TrimSpace(htmlString)), info.Mode())
if err != nil {
Expand All @@ -211,9 +223,6 @@ func postAdjustment() {
}
sitemapContent := string(buf)

// ===== change crawl frequency
// sitemapContent = strings.Replace(sitemapContent, `<changefreq>weekly</changefreq>`, `<changefreq>daily</changefreq>`)

// ===== inject files into sitemap
sitemapContent = strings.ReplaceAll(sitemapContent, `</urlset>`, strings.TrimSpace(`
<url>
Expand Down Expand Up @@ -241,6 +250,61 @@ func postAdjustment() {
fmt.Println(" ==>", siteMapPath)
}

func getVersion() string {
return fmt.Sprintf("%d.%s", baseVersion, now.Format("2006.01.02.150405"))
func ebookPreAdjustment() {
basePath, _ := os.Getwd()
readmePath := filepath.Join(basePath, "README.md")

buf, err := os.ReadFile(readmePath)
if err != nil {
log.Fatal(err.Error())
}
mdString := string(buf)

// ==== adjust version
versionToFind := `((VERSION))`
mdString = strings.ReplaceAll(mdString, versionToFind, getVersion())

err = os.WriteFile(readmePath, []byte(mdString), 0644)
if err != nil {
log.Fatal(err.Error())
}

// ==== adjust content
err = filepath.Walk(basePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if filepath.Ext(info.Name()) != ".md" {
return nil
}

buf, err := os.ReadFile(path)
if err != nil {
return err
}
htmlString := string(buf)

// ==== remove substack embed
substackEmbedToRemove := `<iframe src="partial/substack.html" width="100%" height="320px" frameborder="0" scrolling="no"></iframe>`
htmlString = strings.ReplaceAll(htmlString, substackEmbedToRemove, "")

// ==== remove ebooks embed
ebooksEmbedToRemove := `<iframe src="partial/ebooks.html" width="100%" height="390px" frameborder="0" scrolling="no"></iframe>`
htmlString = strings.ReplaceAll(htmlString, ebooksEmbedToRemove, "")

// ==== update file
err = os.WriteFile(path, []byte(strings.TrimSpace(htmlString)), info.Mode())
if err != nil {
return err
}

fmt.Println(" ==>", path)
return nil
})
if err != nil {
log.Fatal(err.Error())
}
}

0 comments on commit 838f5cd

Please sign in to comment.