@@ -19,20 +19,37 @@ var (
1919)
2020
2121func main () {
22- flagAdjustment := flag .String ("type" , "" , "adjustment type (pre/post)" )
22+ flagMode := flag .String ("mode" , "" , "mode (webbook/ebook)" )
23+ flagAdjustment := flag .String ("adjustment" , "" , "adjustment type (pre/post)" )
2324 flag .Parse ()
2425
25- switch * flagAdjustment {
26- case "pre" :
27- preAdjustment ()
28- case "post" :
29- postAdjustment ()
26+ switch * flagMode {
27+ case "webbook" :
28+ switch * flagAdjustment {
29+ case "pre" :
30+ webbookPreAdjustment ()
31+ case "post" :
32+ webbookPostAdjustment ()
33+ default :
34+ log .Fatalf ("unrecognized flag -adjustment" )
35+ }
36+ case "ebook" :
37+ switch * flagAdjustment {
38+ case "pre" :
39+ ebookPreAdjustment ()
40+ default :
41+ log .Fatalf ("unrecognized flag -adjustment" )
42+ }
3043 default :
31- break
44+ log . Fatalf ( "unrecognized flag -mode" )
3245 }
3346}
3447
35- func preAdjustment () {
48+ func getVersion () string {
49+ return fmt .Sprintf ("%d.%s" , baseVersion , now .Format ("2006.01.02.150405" ))
50+ }
51+
52+ func webbookPreAdjustment () {
3653 basePath , _ := os .Getwd ()
3754 readmePath := filepath .Join (basePath , "README.md" )
3855
@@ -52,7 +69,7 @@ func preAdjustment() {
5269 }
5370}
5471
55- func postAdjustment () {
72+ func webbookPostAdjustment () {
5673 basePath , _ := os .Getwd ()
5774 bookPath := filepath .Join (basePath , "_book" )
5875
@@ -149,7 +166,12 @@ func postAdjustment() {
149166
150167 // ==== inject github stars button
151168 buttonToFind := `</body>`
152- 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> <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
169+ buttonReplacement := `<div style="position: fixed; top: 10px; right: 30px; padding: 10px; background-color: rgba(255, 255, 255, 0.7);">
170+ <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>
171+ <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>
172+ <a class="github-button" href="https://github.com/novalagung" data-size="large" aria-label="Follow @novalagung on GitHub">Follow @novalagung</a>
173+ <script async defer src="https://buttons.github.io/buttons.js"></script>
174+ </div>` + buttonToFind
153175 htmlString = strings .ReplaceAll (htmlString , buttonToFind , buttonReplacement )
154176
155177 // ==== inject adjustment css
@@ -180,16 +202,6 @@ func postAdjustment() {
180202 // 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
181203 // htmlString = strings.Replace(htmlString, fbPixelToFind, fbPixelReplacement)
182204
183- // ===== inject banner of new ebook
184- // bannerToFind := `</body>`
185- // 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
186- // htmlString = strings.Replace(htmlString, bannerToFind, bannerReplacement)
187-
188- // ===== inject popup info banner if exists
189- // infoBannerToFind := `</body>`
190- // 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
191- // htmlString = strings.Replace(htmlString, infoBannerToFind, infoBannerReplacement)
192-
193205 // ==== update file
194206 err = os .WriteFile (path , []byte (strings .TrimSpace (htmlString )), info .Mode ())
195207 if err != nil {
@@ -211,9 +223,6 @@ func postAdjustment() {
211223 }
212224 sitemapContent := string (buf )
213225
214- // ===== change crawl frequency
215- // sitemapContent = strings.Replace(sitemapContent, `<changefreq>weekly</changefreq>`, `<changefreq>daily</changefreq>`)
216-
217226 // ===== inject files into sitemap
218227 sitemapContent = strings .ReplaceAll (sitemapContent , `</urlset>` , strings .TrimSpace (`
219228 <url>
@@ -241,6 +250,61 @@ func postAdjustment() {
241250 fmt .Println (" ==>" , siteMapPath )
242251}
243252
244- func getVersion () string {
245- return fmt .Sprintf ("%d.%s" , baseVersion , now .Format ("2006.01.02.150405" ))
253+ func ebookPreAdjustment () {
254+ basePath , _ := os .Getwd ()
255+ readmePath := filepath .Join (basePath , "README.md" )
256+
257+ buf , err := os .ReadFile (readmePath )
258+ if err != nil {
259+ log .Fatal (err .Error ())
260+ }
261+ mdString := string (buf )
262+
263+ // ==== adjust version
264+ versionToFind := `((VERSION))`
265+ mdString = strings .ReplaceAll (mdString , versionToFind , getVersion ())
266+
267+ err = os .WriteFile (readmePath , []byte (mdString ), 0644 )
268+ if err != nil {
269+ log .Fatal (err .Error ())
270+ }
271+
272+ // ==== adjust content
273+ err = filepath .Walk (basePath , func (path string , info os.FileInfo , err error ) error {
274+ if err != nil {
275+ return err
276+ }
277+ if info .IsDir () {
278+ return nil
279+ }
280+ if filepath .Ext (info .Name ()) != ".md" {
281+ return nil
282+ }
283+
284+ buf , err := os .ReadFile (path )
285+ if err != nil {
286+ return err
287+ }
288+ htmlString := string (buf )
289+
290+ // ==== remove substack embed
291+ substackEmbedToRemove := `<iframe src="partial/substack.html" width="100%" height="320px" frameborder="0" scrolling="no"></iframe>`
292+ htmlString = strings .ReplaceAll (htmlString , substackEmbedToRemove , "" )
293+
294+ // ==== remove ebooks embed
295+ ebooksEmbedToRemove := `<iframe src="partial/ebooks.html" width="100%" height="390px" frameborder="0" scrolling="no"></iframe>`
296+ htmlString = strings .ReplaceAll (htmlString , ebooksEmbedToRemove , "" )
297+
298+ // ==== update file
299+ err = os .WriteFile (path , []byte (strings .TrimSpace (htmlString )), info .Mode ())
300+ if err != nil {
301+ return err
302+ }
303+
304+ fmt .Println (" ==>" , path )
305+ return nil
306+ })
307+ if err != nil {
308+ log .Fatal (err .Error ())
309+ }
246310}
0 commit comments