~npisanti/npisantidotcomssg

ref: 88cdde99b2328f6ba743af66967b9d063f55c83f npisantidotcomssg/build_fish.sh -rwxr-xr-x 10.8 KiB
88cdde99Nicola Pisanti finished fish script 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/fish

set SCRIPTFOLDER "$HOME/htdocs/npisantidotcomssg"
set OUTFOLDER "$SCRIPTFOLDER/output2"
set MAXPOSTSPERPAGE 6
set CATEGORIES "ink" "installations" "music" "visuals"

#--------------------------------------------------------------------------------
set GENERAL_THUMB "avatars/avatar_og.png"

function og_image_path
    set filesource "$argv[1]"
   	set og_thumbfile "$GENERAL_THUMB"
   	
	if test -n $filesource
		set post_og_thumb ( cat $filesource | ag "og_thumb=" )
		if test -z $post_og_thumb
			set og_thumbfile (echo $post_og_thumb | cut -d '=' -f2)
		end
	end

	echo "<meta property=\"og:image\" content=\"http://npisanti.com/data/$og_thumbfile\" />"
end

function date_and_category 
	set -l post_path $argv[1]
    if test -z  $argv[2]; or test $argv[2] -eq "1"
        set pagelink "index"
    else
        set pagelink "page$argv[2]"
	end
	
	set -l post_split (string split / $argv[1])
	# channel in [1], name in [2]
	set -l post_dir $post_split[1]
	set -l post_name $post_split[2]
	
	set -l date_str (string sub -s 1 -l 10 $post_name | string replace -a _ / )

	echo "<br><br><div style=\"text-align:right\"><a href=\"../posts/$post_name\">$date_str</a> | posted in <a href=\"../$post_dir/$pagelink.html\">$post_dir</a></div>"
end

function find_dir_page 
    # arg 1 is dir/postname.html 
	set -l post_split (string split / $argv[1])
	set post_dir $post_split[1]
	set post_name $post_split[2]
	set postnum "1" 
	set pagenum "1"
   
	for p in (ls -1 "$SCRIPTFOLDER/input/journal/$post_dir" | sort -r)
        if test "$p" = "$post_name"    
            set pagenum (math -s0 "(($postnum-1)/$MAXPOSTSPERPAGE)+1" )
        end
        set postnum (math "$postnum+1")
	end

	echo $pagenum
end

function open_page #-------------------------------------------------------------
	# arg 1 should be page path to get thumb
    # arg 2 should be the title
	set    text '<!DOCTYPE html>'
	set -a text '\n<html>'
	set -a text '\n<head>'
	set -a text '\n<meta charset="utf-8" />'

	if test -n $argv[2] 
	    set -a text "\n<title>[ $argv[2] ]</title>"
	else
    	set -a text "\n<title>[ npisanti.com]</title>"
	end 

    set -a text "\n"(cat input/base/head.html) 

	set -a text (og_image_path "$argv[1]") 
	
	set -a text '\n<meta property="og:image:height" content="230" />'
	set -a text '\n<meta property="og:image:width" content="230" />'

	set -a text "\n</head>"
	set -a text "\n<body>"

	echo $text
end

function close_page
	echo "</body></html>"
end

function navigation_header #-----------------------------------------------------
	# argument 1 is the tab to highlight
	set -l tabs "feed" "more" "contact" "webring"
	set text "<header>"

	for x in $tabs
		if test "$x" = "$argv[1]"
			set pretab "[ "
			set posttab " ]"
		else
			set pretab ""
			set posttab ""
		end
		
		switch $x 
		case feed main
			set -a text "$pretab<a href=\"../main/index.html\">feed</a>$posttab&nbsp;"
		case more
			set -a text "$pretab<a href=\"../pages/more.html\">more</a>$posttab&nbsp;"
		case contact 
			set -a text "$pretab<a href=\"../pages/contact.html\">contact</a>$posttab&nbsp;"
		case webring
			set -a text "$pretab<a href=\"https://webring.xxiivv.com/\">webring</a>$posttab&nbsp;"
		end
	end

	set -a text "</header>"
	echo $text
end 

function navigation_footer #-----------------------------------------------------
    set numpage "$argv[1]"
    set maxpage "$argv[2]"
    set archive_enable "$argv[3]"
	set footer "<footer>"

	if test -n "$archive_enable" 
		set -a footer "<a href=\"archive.html\">all</a>"        
	end

	if test "$numpage" -ne "1"
		set -l prev (math "$numpage-1")
		set -a footer "<a href=\"page$prev.html\">prev</a>"
	end

	for i in (seq $maxpage)
		if test "$i" -eq "$numpage"
			set -a footer "<a href=\"page$i.html\">[$i]</a>"
		else
			set -a footer "<a href=\"page$i.html\">$i</a>"
		end
	end
	
	if test "$numpage" -ne "$maxpage"
		set -l next (math "$numpage+1")
		set -a footer "<a href=\"page$next.html\">next</a>"
	end

	set -a footer "</footer>"

	echo $footer
end

function build_pages #-----------------------------------------------------------
	printf "building pages"
	
	for x in input/pages/*.html
		set name (basename $x .html)
		set outname (basename $x)

		set    text (open_page "$x" "$name")
		set -a text "\n"(navigation_header $name)
		set -a text "\n"(cat "$x")
		set -a text "\n"(close_page)
		set text (echo $text | sed -e "s|SITEROOTPATH|..|g") 		

		echo -e "$text" > "$OUTFOLDER/pages/$outname"
		printf "."
	end
	
	cp "$OUTFOLDER/pages/more.html" "$OUTFOLDER/pages/index.html" 

	printf "\n"
end

function build_feed #------------------------------------------------------------
    set channel_name "$argv[1]"
    printf "building $channel_name"
    set header_tag "$channel_name"
    set title "$channel_name"

    mkdir -p "$OUTFOLDER/$channel_name"
    
    if test "$channel_name" = "main"
        set list (cat input/journal/feed.list)
        set header_tag "feed"
        set title "npisanti.com"
    else
        set list "$argv[1]/"(ls -1 "$SCRIPTFOLDER/input/journal/$argv[1]" | sort -r)    
    end
 
	set posts_len (count $list)
	set max_pages (math "floor($posts_len / $MAXPOSTSPERPAGE)")

    # adds the last page if needed 
    if test (math -s0 "$posts_len % $MAXPOSTSPERPAGE") -ne "0"
        set max_pages (math "$max_pages+1")
    end
    
	for i in ( seq "$max_pages" )
		set start (math "(($i-1)*$MAXPOSTSPERPAGE)+1" ) 
		set stop (math "(($i)*$MAXPOSTSPERPAGE)" ) 
		# often the last page has less posts
		if test "$stop" -gt "$posts_len" 
			set stop $posts_len
		end

		set    text (open_page "" "$title")
		set -a text "\n"(navigation_header "$header_tag" )

		for p in $list[$start..$stop]
			set -a text "\n<section>"
			set -a text "\n"(cat "input/journal/$p")
			if test "$channel_name" = "main"
                # this will search for the correct category page
			    set -l dirpage (find_dir_page "$p")
			    set -a text "\n"(date_and_category $p $dirpage )
			else
			    set -a text "\n"(date_and_category $p $i)
			end
			set -a text "\n</section>"
		end

		set -a text "\n"(navigation_footer $i $max_pages "active") 
		set -a text "\n"(close_page)
		set    text (echo $text | sed -e "s|SITEROOTPATH|..|g") 		

		echo -e "$text" > "$OUTFOLDER/$channel_name/page$i.html"
		printf "."
	end
	cp "$OUTFOLDER/$channel_name/page1.html" "$OUTFOLDER/$channel_name/index.html"
    printf "\n"
end

function build_posts #-----------------------------------------------------------
    printf "building posts"
    mkdir -p "$OUTFOLDER/posts"

    for dir in $CATEGORIES
        set postnum "1"
        set list (ls -1 "$SCRIPTFOLDER/input/journal/$dir" | sort -r)    

		for p in $list
		    if test -n $p
                set -l pagenum (math -s0 "(($postnum-1)/$MAXPOSTSPERPAGE)+1" )
                set -l title ( basename "$p" .html | string sub -s 13 | string replace -a _ " " )
        		set    text (open_page "" "$title")
        		set -a text "\n"(navigation_header "post" )
    			set -a text "\n<section>"
    			set -a text "\n"(cat "input/journal/$dir/$p" )
    			set -a text "\n"(date_and_category "$dir/$p" "$pagenum" )
    			set -a text "\n</section>"
                set -a text "\n"(close_page)
                set    text (echo $text | sed -e "s|SITEROOTPATH|..|g") 		
                echo -e "$text" > "$OUTFOLDER/posts/$p"

                set postnum (math "$postnum + 1")
                printf "."
            end
		end
    end
    
    printf "\n"
end

function build_archives #--------------------------------------------------------
    printf "building archives"
    set last_year "1995"

    for dir in $CATEGORIES
        set list (ls -1 "$SCRIPTFOLDER/input/journal/$dir")    

		set    text (open_page "" "$dir archives")
		set -a text "\n"(navigation_header "$dir archives" )
        set -a text "\n<section>"

		for p in $list
			set -l post_date (string sub -s 1 -l 10 $p | string replace -a _ / )
			set -l post_year (string sub -s 1 -l 4 $p )
			if test "$last_year" != "$post_year"
                set last_year "$post_year"
                set -a text "\n<br/>"
			end

			set -l post_name ( basename "$p" .html | string sub -s 12 | string replace -a _ " " )
		    set -a text "\n$post_date <a href=\"../posts/$p\">$post_name</a><br/>"
		end

        set -a text "\n</section>"
        set -a text "\n"(close_page)
        set    text (echo $text | sed -e "s|SITEROOTPATH|..|g") 
        echo -e "$text" > "$OUTFOLDER/$dir/archive.html"
        printf "."
    end
    
    printf "\n"
end

function build_rss #-------------------------------------------------------------
    printf "building RSS"
    set list (cat input/journal/rss.list)    
    set text "\n"(cat input/base/rssbase.xml)

    for p in $list
        if test -n $p
        	set -l post_split (string split / $p)
        	set post_dir $post_split[1]
        	set post_name $post_split[2]
            set -l year (string sub -s 1 -l 4 $post_name)
            set -l month (string sub -s 6 -l 2 $post_name)
            set -l day (string sub -s 9 -l 2 $post_name)
            set -l title ( basename "$post_name" .html | string sub -s 13 | string replace -a _ " " )
            set -l pagelink "posts/$post_name"
            set -l RFC822_time (date --date=$month/$day/$year -R)

            set -a text "\n\t\t<item>"
            set -a text "\n\t\t\t<title>$title</title>" 
            set -a text "\n\t\t\t<description><![CDATA["
            set -a text "\n\t\t\t"(cat "input/journal/$p")
            set -a text "\n\t\t\t]]></description>"
            set -a text "\n\t\t\t<pubDate>$RFC822_time</pubDate>"
            set -a text "\n\t\t\t<guid>http://npisanti.com/$pagelink</guid>"
            set -a text "\n\t\t</item>"

            printf "."
        end
    end

    set -a text "\t</channel>"
    set -a text "\t</rss>"    
    set text (echo $text | sed -e "s|SITEROOTPATH|http://npisanti.com|g") 
    echo -e "$text" > "$OUTFOLDER/rss.xml"
    printf "\n"
end

function build_index_redirect #--------------------------------------------------
	echo "creating index redirect..."
	set    text '<!DOCTYPE html>'
	set -a text '\n<html>\n<head>\n<meta charset="utf-8" />'
	set -a text "\n<title>[ npisanti.com]</title>"
    set -a text "\n"(cat input/base/head.html) 
	set -a text (og_image_path) 
	set -a text '\n<meta property="og:image:height" content="230" />'
	set -a text '\n<meta property="og:image:width" content="230" />'
	set -a text '\n<meta http-equiv="refresh" content="0;url=main/index.html" />'
	set -a text "\n</head>"
	set -a text "\n<body>"
	set -a text "\nredirecting to main/index.html"
	set -a text "\n</body>"
	set text (echo $text | sed -e "s|SITEROOTPATH|.|g") 
	echo -e $text > "$OUTFOLDER/index.html"
end

#--------------------- EXECUTES ALL THE FUNCTIONS -------------------------------
cd $SCRIPTFOLDER

build_pages
build_feed "main"
for dir in $CATEGORIES; build_feed $dir; end
build_posts
build_archives
build_rss
build_index_redirect

# retrocompatibiliy
mkdir -p "$OUTFOLDER/journal"
cp "$OUTFOLDER/rss.xml" "$OUTFOLDER/journal/rss.xml"

exit