~npisanti/npisantidotcomssg

718bbd7f6db5eb27e797b100d740144b374100cf — Nicola Pisanti a month ago 105aa37
almost finished
1 files changed, 177 insertions(+), 43 deletions(-)

M build_fish.sh
M build_fish.sh => build_fish.sh +177 -43
@@ 1,18 1,11 @@
#!/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 CATEGORIES "ink" "installations" "music" "visuals"

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

function og_image_path


@@ 31,6 24,12 @@ 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]


@@ 38,18 37,42 @@ function date_and_category
	
	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/index.html\">$post_dir</a></div>"
	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<body>'
	set -a text '\n<head>'
	set -a text '\n<meta charset="utf-8" />'
	set -a text '\n<title>npisanti.com</title>' 

    set -a text (cat input/base/head.html) 
	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]") 
	


@@ 96,15 119,15 @@ function navigation_header #----------------------------------------------------
	echo $text
end 

#--------------------------------------------------------------------------------
function navigation_footer
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=\"$prepend_path/archive.html\">all</a>"        
		set -a footer "<a href=\"archive.html\">all</a>"        
	end

	if test "$numpage" -ne "1"


@@ 130,58 153,73 @@ function navigation_footer
	echo $footer
end

#--------------------------------------------------------------------------------
function build_pages 
	echo "processing pages..."
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")
		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
    # the feed_list has to be set before running
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) +1")
	set max_pages (math "floor($posts_len / $MAXPOSTSPERPAGE)")

	mkdir -p "$OUTFOLDER/$channel_name"

	echo "MAX $max_pages"
	
    # 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" )
		echo "processing page $i"
		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 "")
		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")
			set -a text "\n"(date_and_category $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



@@ 190,28 228,124 @@ function build_feed
		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")    

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

# built posts_and_archives
# build_rss
# add link from npisanti.com/index.html to npisanti.com/main/index.html
		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

build_pages
			set -l post_name ( basename "$p" .html | string sub -s 12 | string replace -a _ " " )

build_feed "main"
build_feed "ink"
build_feed "installations"
build_feed "visuals"
build_feed "music"
		    set -a text "\n$post_date <a href=\"../posts/$p\">$post_name</a><br/>"		
		end

#echo (navigation_footer 2 7 "") 
        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

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

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

exit 
exit 
\ No newline at end of file