~npisanti/npisantidotcomssg

cb3aff475d97ba024bd727aa1e7cb1da9f1965f3 — Nicola Pisanti a month ago f3ac9d7
working on an alternate ssg with fish shell
1 files changed, 195 insertions(+), 0 deletions(-)

A build_fish.sh
A build_fish.sh => build_fish.sh +195 -0
@@ 0,0 1,195 @@
#!/usr/bin/fish

# notes: needs fish and ag 
#     $text is used as global variable, clear it before use
#
# todo:
#   build_feed function (inputs: file list, feed name, bArchive 
#

set SCRIPTFOLDER "$HOME/htdocs/npisantidotcomssg"
set OUTFOLDER "$SCRIPTFOLDER/output2"
cd $SCRIPTFOLDER
set MAXPOSTSPERPAGE 6

 #-------------------------------------------------------------------------------
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 open_page #-------------------------------------------------------------
	# arg 1 should be page path to get thumb
	set    text '<!DOCTYPE html>'
	set -a text '\n<html>'
	set -a text '\n<body>'
	set -a text '\n<meta charset="utf-8" />'
	set -a text '\n<title>npisanti.com</title>' 

	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 build_header #----------------------------------------------------------
	# argument 1 is the tab to highlight
	# argument 2 is a string to prepend to relative links, eg "../"	
	set -l tabs "feed" "more" "contact" "webring"
	set text ""

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

	echo $text
end 

#--------------------------------------------------------------------------------
function navigation_footer
    set numpage "$argv[1]"
    set maxpage "$argv[2]"
    set prepend_path "$argv[3]"
    set archive_enable "$argv[4]"
	set footer ""
	if test -z "$prepend_path"
		set prepend_path "."
	end

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

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

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

	echo $footer
end

function date_and_category 
	set -l post_path $argv[1]
	set prepend_path $argv[2]
	if test -z $argv[2]
		set prepend_path ".."	
	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=\"$prepend_path/posts/$post_name\">$date_str</a> | posted in <a href=\"$prepend_path/$post_dir/index.html\">$post_dir</a></div>"

	
	
end

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

		set    text (open_page "$x")
		set -a text "\n"(build_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/$outname"
	end
end

function build_index
	set    text (open_page "")
	set -a text "\n"(build_header "feed")

	set list (cat input/journal/feed.list)

	for p in $list[1..$MAXPOSTSPERPAGE]
		set -a text "\n<section>"
		set -a text "\n"(cat "input/journal/$p")
		set -a text "\n"(date_and_category $p ".")
		set -a text "\n</section>"
	end

	set -l posts_len (count $list)
	set -l max_pages (math "floor($posts_len / $MAXPOSTSPERPAGE) +1")

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

	echo -e "$text" > "$OUTFOLDER/index.html"
end


# TODO
# build index : takes range from (cat feed.list)
# build_feed 
# build_channel
# built posts_and_archives
# build_rss

build_index

#echo (navigation_footer 2 7 "") 


exit