@@ -19,20 +19,37 @@ var (
19
19
)
20
20
21
21
func 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)" )
23
24
flag .Parse ()
24
25
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
+ }
30
43
default :
31
- break
44
+ log . Fatalf ( "unrecognized flag -mode" )
32
45
}
33
46
}
34
47
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 () {
36
53
basePath , _ := os .Getwd ()
37
54
readmePath := filepath .Join (basePath , "README.md" )
38
55
@@ -52,7 +69,7 @@ func preAdjustment() {
52
69
}
53
70
}
54
71
55
- func postAdjustment () {
72
+ func webbookPostAdjustment () {
56
73
basePath , _ := os .Getwd ()
57
74
bookPath := filepath .Join (basePath , "_book" )
58
75
@@ -149,7 +166,12 @@ func postAdjustment() {
149
166
150
167
// ==== inject github stars button
151
168
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
153
175
htmlString = strings .ReplaceAll (htmlString , buttonToFind , buttonReplacement )
154
176
155
177
// ==== inject adjustment css
@@ -180,16 +202,6 @@ func postAdjustment() {
180
202
// 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
181
203
// htmlString = strings.Replace(htmlString, fbPixelToFind, fbPixelReplacement)
182
204
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
-
193
205
// ==== update file
194
206
err = os .WriteFile (path , []byte (strings .TrimSpace (htmlString )), info .Mode ())
195
207
if err != nil {
@@ -211,9 +223,6 @@ func postAdjustment() {
211
223
}
212
224
sitemapContent := string (buf )
213
225
214
- // ===== change crawl frequency
215
- // sitemapContent = strings.Replace(sitemapContent, `<changefreq>weekly</changefreq>`, `<changefreq>daily</changefreq>`)
216
-
217
226
// ===== inject files into sitemap
218
227
sitemapContent = strings .ReplaceAll (sitemapContent , `</urlset>` , strings .TrimSpace (`
219
228
<url>
@@ -241,6 +250,61 @@ func postAdjustment() {
241
250
fmt .Println (" ==>" , siteMapPath )
242
251
}
243
252
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
+ }
246
310
}
0 commit comments