From 67b549e996488940567a850fa43e90ca578809c7 Mon Sep 17 00:00:00 2001 From: merry_envs Date: Tue, 1 Dec 2020 11:37:19 +0000 Subject: [PATCH] populate --- .afterbody | 2 + .beforebody | 2 + .config | 17 + .header.template | 13 + Markdown.pl | 1450 ++++++++++++++++++++++++++++++++++++++++++++++ bb.sh | 1174 +++++++++++++++++++++++++++++++++++++ gophermap | 7 + 7 files changed, 2665 insertions(+) create mode 100644 .afterbody create mode 100644 .beforebody create mode 100644 .config create mode 100644 .header.template create mode 100755 Markdown.pl create mode 100755 bb.sh create mode 100755 gophermap diff --git a/.afterbody b/.afterbody new file mode 100644 index 0000000..f20fb6e --- /dev/null +++ b/.afterbody @@ -0,0 +1,2 @@ + +
diff --git a/.beforebody b/.beforebody new file mode 100644 index 0000000..d47041f --- /dev/null +++ b/.beforebody @@ -0,0 +1,2 @@ +
+ diff --git a/.config b/.config new file mode 100644 index 0000000..701b5fe --- /dev/null +++ b/.config @@ -0,0 +1,17 @@ +# Blog title +global_title="Blog | merry" +# The typical subtitle for each blog +global_description="Blog de merry" +# The public base URL for this blog +global_url="http://fr.tild3.org/~merry/blog" + +# Your name +global_author="merry" +# You can use twitter or facebook or anything for global_author_url +global_author_url="https://fr.tild3.org/~merry/" +# Your email +global_email="merry@fr.tild3.org" +# Style +header_file=".header.template" +body_begin_file=".beforebody" +body_end_file=".afterbody" diff --git a/.header.template b/.header.template new file mode 100644 index 0000000..09e841c --- /dev/null +++ b/.header.template @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/Markdown.pl b/Markdown.pl new file mode 100755 index 0000000..e4c8469 --- /dev/null +++ b/Markdown.pl @@ -0,0 +1,1450 @@ +#!/usr/bin/perl + +# +# Markdown -- A text-to-HTML conversion tool for web writers +# +# Copyright (c) 2004 John Gruber +# +# + + +package Markdown; +require 5.006_000; +use strict; +use warnings; + +use Digest::MD5 qw(md5_hex); +use vars qw($VERSION); +$VERSION = '1.0.1'; +# Tue 14 Dec 2004 + +## Disabled; causes problems under Perl 5.6.1: +# use utf8; +# binmode( STDOUT, ":utf8" ); # c.f.: http://acis.openlib.org/dev/perl-unicode-struggle.html + + +# +# Global default settings: +# +my $g_empty_element_suffix = " />"; # Change to ">" for HTML output +my $g_tab_width = 4; + + +# +# Globals: +# + +# Regex to match balanced [brackets]. See Friedl's +# "Mastering Regular Expressions", 2nd Ed., pp. 328-331. +my $g_nested_brackets; +$g_nested_brackets = qr{ + (?> # Atomic matching + [^\[\]]+ # Anything other than brackets + | + \[ + (??{ $g_nested_brackets }) # Recursive set of nested brackets + \] + )* +}x; + + +# Table of hash values for escaped characters: +my %g_escape_table; +foreach my $char (split //, '\\`*_{}[]()>#+-.!') { + $g_escape_table{$char} = md5_hex($char); +} + + +# Global hashes, used by various utility routines +my %g_urls; +my %g_titles; +my %g_html_blocks; + +# Used to track when we're inside an ordered or unordered list +# (see _ProcessListItems() for details): +my $g_list_level = 0; + + +#### Blosxom plug-in interface ########################################## + +# Set $g_blosxom_use_meta to 1 to use Blosxom's meta plug-in to determine +# which posts Markdown should process, using a "meta-markup: markdown" +# header. If it's set to 0 (the default), Markdown will process all +# entries. +my $g_blosxom_use_meta = 0; + +sub start { 1; } +sub story { + my($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_; + + if ( (! $g_blosxom_use_meta) or + (defined($meta::markup) and ($meta::markup =~ /^\s*markdown\s*$/i)) + ){ + $$body_ref = Markdown($$body_ref); + } + 1; +} + + +#### Movable Type plug-in interface ##################################### +eval {require MT}; # Test to see if we're running in MT. +unless ($@) { + require MT; + import MT; + require MT::Template::Context; + import MT::Template::Context; + + eval {require MT::Plugin}; # Test to see if we're running >= MT 3.0. + unless ($@) { + require MT::Plugin; + import MT::Plugin; + my $plugin = new MT::Plugin({ + name => "Markdown", + description => "A plain-text-to-HTML formatting plugin. (Version: $VERSION)", + doc_link => 'http://daringfireball.net/projects/markdown/' + }); + MT->add_plugin( $plugin ); + } + + MT::Template::Context->add_container_tag(MarkdownOptions => sub { + my $ctx = shift; + my $args = shift; + my $builder = $ctx->stash('builder'); + my $tokens = $ctx->stash('tokens'); + + if (defined ($args->{'output'}) ) { + $ctx->stash('markdown_output', lc $args->{'output'}); + } + + defined (my $str = $builder->build($ctx, $tokens) ) + or return $ctx->error($builder->errstr); + $str; # return value + }); + + MT->add_text_filter('markdown' => { + label => 'Markdown', + docs => 'http://daringfireball.net/projects/markdown/', + on_format => sub { + my $text = shift; + my $ctx = shift; + my $raw = 0; + if (defined $ctx) { + my $output = $ctx->stash('markdown_output'); + if (defined $output && $output =~ m/^html/i) { + $g_empty_element_suffix = ">"; + $ctx->stash('markdown_output', ''); + } + elsif (defined $output && $output eq 'raw') { + $raw = 1; + $ctx->stash('markdown_output', ''); + } + else { + $raw = 0; + $g_empty_element_suffix = " />"; + } + } + $text = $raw ? $text : Markdown($text); + $text; + }, + }); + + # If SmartyPants is loaded, add a combo Markdown/SmartyPants text filter: + my $smartypants; + + { + no warnings "once"; + $smartypants = $MT::Template::Context::Global_filters{'smarty_pants'}; + } + + if ($smartypants) { + MT->add_text_filter('markdown_with_smartypants' => { + label => 'Markdown With SmartyPants', + docs => 'http://daringfireball.net/projects/markdown/', + on_format => sub { + my $text = shift; + my $ctx = shift; + if (defined $ctx) { + my $output = $ctx->stash('markdown_output'); + if (defined $output && $output eq 'html') { + $g_empty_element_suffix = ">"; + } + else { + $g_empty_element_suffix = " />"; + } + } + $text = Markdown($text); + $text = $smartypants->($text, '1'); + }, + }); + } +} +else { +#### BBEdit/command-line text filter interface ########################## +# Needs to be hidden from MT (and Blosxom when running in static mode). + + # We're only using $blosxom::version once; tell Perl not to warn us: + no warnings 'once'; + unless ( defined($blosxom::version) ) { + use warnings; + + #### Check for command-line switches: ################# + my %cli_opts; + use Getopt::Long; + Getopt::Long::Configure('pass_through'); + GetOptions(\%cli_opts, + 'version', + 'shortversion', + 'html4tags', + ); + if ($cli_opts{'version'}) { # Version info + print "\nThis is Markdown, version $VERSION.\n"; + print "Copyright 2004 John Gruber\n"; + print "http://daringfireball.net/projects/markdown/\n\n"; + exit 0; + } + if ($cli_opts{'shortversion'}) { # Just the version number string. + print $VERSION; + exit 0; + } + if ($cli_opts{'html4tags'}) { # Use HTML tag style instead of XHTML + $g_empty_element_suffix = ">"; + } + + + #### Process incoming text: ########################### + my $text; + { + local $/; # Slurp the whole file + $text = <>; + } + print Markdown($text); + } +} + + + +sub Markdown { +# +# Main function. The order in which other subs are called here is +# essential. Link and image substitutions need to happen before +# _EscapeSpecialChars(), so that any *'s or _'s in the +# and tags get encoded. +# + my $text = shift; + + # Clear the global hashes. If we don't clear these, you get conflicts + # from other articles when generating a page which contains more than + # one article (e.g. an index page that shows the N most recent + # articles): + %g_urls = (); + %g_titles = (); + %g_html_blocks = (); + + + # Standardize line endings: + $text =~ s{\r\n}{\n}g; # DOS to Unix + $text =~ s{\r}{\n}g; # Mac to Unix + + # Make sure $text ends with a couple of newlines: + $text .= "\n\n"; + + # Convert all tabs to spaces. + $text = _Detab($text); + + # Strip any lines consisting only of spaces and tabs. + # This makes subsequent regexen easier to write, because we can + # match consecutive blank lines with /\n+/ instead of something + # contorted like /[ \t]*\n+/ . + $text =~ s/^[ \t]+$//mg; + + # Turn block-level HTML blocks into hash entries + $text = _HashHTMLBlocks($text); + + # Strip link definitions, store in hashes. + $text = _StripLinkDefinitions($text); + + $text = _RunBlockGamut($text); + + $text = _UnescapeSpecialChars($text); + + return $text . "\n"; +} + + +sub _StripLinkDefinitions { +# +# Strips link definitions from text, stores the URLs and titles in +# hash references. +# + my $text = shift; + my $less_than_tab = $g_tab_width - 1; + + # Link defs are in the form: ^[id]: url "optional title" + while ($text =~ s{ + ^[ ]{0,$less_than_tab}\[(.+)\]: # id = $1 + [ \t]* + \n? # maybe *one* newline + [ \t]* + ? # url = $2 + [ \t]* + \n? # maybe one newline + [ \t]* + (?: + (?<=\s) # lookbehind for whitespace + ["(] + (.+?) # title = $3 + [")] + [ \t]* + )? # title is optional + (?:\n+|\Z) + } + {}mx) { + $g_urls{lc $1} = _EncodeAmpsAndAngles( $2 ); # Link IDs are case-insensitive + if ($3) { + $g_titles{lc $1} = $3; + $g_titles{lc $1} =~ s/"/"/g; + } + } + + return $text; +} + + +sub _HashHTMLBlocks { + my $text = shift; + my $less_than_tab = $g_tab_width - 1; + + # Hashify HTML blocks: + # We only want to do this for block-level HTML tags, such as headers, + # lists, and tables. That's because we still want to wrap

s around + # "paragraphs" that are wrapped in non-block-level tags, such as anchors, + # phrase emphasis, and spans. The list of tags we're looking for is + # hard-coded: + my $block_tags_a = qr/p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del/; + my $block_tags_b = qr/p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math/; + + # First, look for nested blocks, e.g.: + #

+ #
+ # tags for inner block must be indented. + #
+ #
+ # + # The outermost tags must start at the left margin for this to match, and + # the inner nested divs must be indented. + # We need to do this before the next, more liberal match, because the next + # match will start at the first `
` and stop at the first `
`. + $text =~ s{ + ( # save in $1 + ^ # start of line (with /m) + <($block_tags_a) # start tag = $2 + \b # word break + (.*\n)*? # any number of lines, minimally matching + # the matching end tag + [ \t]* # trailing spaces/tabs + (?=\n+|\Z) # followed by a newline or end of document + ) + }{ + my $key = md5_hex($1); + $g_html_blocks{$key} = $1; + "\n\n" . $key . "\n\n"; + }egmx; + + + # + # Now match more liberally, simply from `\n` to `\n` + # + $text =~ s{ + ( # save in $1 + ^ # start of line (with /m) + <($block_tags_b) # start tag = $2 + \b # word break + (.*\n)*? # any number of lines, minimally matching + .* # the matching end tag + [ \t]* # trailing spaces/tabs + (?=\n+|\Z) # followed by a newline or end of document + ) + }{ + my $key = md5_hex($1); + $g_html_blocks{$key} = $1; + "\n\n" . $key . "\n\n"; + }egmx; + # Special case just for
. It was easier to make a special case than + # to make the other regex more complicated. + $text =~ s{ + (?: + (?<=\n\n) # Starting after a blank line + | # or + \A\n? # the beginning of the doc + ) + ( # save in $1 + [ ]{0,$less_than_tab} + <(hr) # start tag = $2 + \b # word break + ([^<>])*? # + /?> # the matching end tag + [ \t]* + (?=\n{2,}|\Z) # followed by a blank line or end of document + ) + }{ + my $key = md5_hex($1); + $g_html_blocks{$key} = $1; + "\n\n" . $key . "\n\n"; + }egx; + + # Special case for standalone HTML comments: + $text =~ s{ + (?: + (?<=\n\n) # Starting after a blank line + | # or + \A\n? # the beginning of the doc + ) + ( # save in $1 + [ ]{0,$less_than_tab} + (?s: + + ) + [ \t]* + (?=\n{2,}|\Z) # followed by a blank line or end of document + ) + }{ + my $key = md5_hex($1); + $g_html_blocks{$key} = $1; + "\n\n" . $key . "\n\n"; + }egx; + + + return $text; +} + + +sub _RunBlockGamut { +# +# These are all the transformations that form block-level +# tags like paragraphs, headers, and list items. +# + my $text = shift; + + $text = _DoHeaders($text); + + # Do Horizontal Rules: + $text =~ s{^[ ]{0,2}([ ]?\*[ ]?){3,}[ \t]*$}{\n tags around block-level tags. + $text = _HashHTMLBlocks($text); + + $text = _FormParagraphs($text); + + return $text; +} + + +sub _RunSpanGamut { +# +# These are all the transformations that occur *within* block-level +# tags like paragraphs, headers, and list items. +# + my $text = shift; + + $text = _DoCodeSpans($text); + + $text = _EscapeSpecialChars($text); + + # Process anchor and image tags. Images must come first, + # because ![foo][f] looks like an anchor. + $text = _DoImages($text); + $text = _DoAnchors($text); + + # Make links out of things like `` + # Must come after _DoAnchors(), because you can use < and > + # delimiters in inline links like [this](). + $text = _DoAutoLinks($text); + + $text = _EncodeAmpsAndAngles($text); + + $text = _DoItalicsAndBold($text); + + # Do hard breaks: + $text =~ s/ {2,}\n/ or tags. +# my $tags_to_skip = qr!<(/?)(?:pre|code|kbd|script|math)[\s>]!; + + foreach my $cur_token (@$tokens) { + if ($cur_token->[0] eq "tag") { + # Within tags, encode * and _ so they don't conflict + # with their use in Markdown for italics and strong. + # We're replacing each such character with its + # corresponding MD5 checksum value; this is likely + # overkill, but it should prevent us from colliding + # with the escape values by accident. + $cur_token->[1] =~ s! \* !$g_escape_table{'*'}!gx; + $cur_token->[1] =~ s! _ !$g_escape_table{'_'}!gx; + $text .= $cur_token->[1]; + } else { + my $t = $cur_token->[1]; + $t = _EncodeBackslashEscapes($t); + $text .= $t; + } + } + return $text; +} + + +sub _DoAnchors { +# +# Turn Markdown link shortcuts into XHTML
tags. +# + my $text = shift; + + # + # First, handle reference-style links: [link text] [id] + # + $text =~ s{ + ( # wrap whole match in $1 + \[ + ($g_nested_brackets) # link text = $2 + \] + + [ ]? # one optional space + (?:\n[ ]*)? # one optional newline followed by spaces + + \[ + (.*?) # id = $3 + \] + ) + }{ + my $result; + my $whole_match = $1; + my $link_text = $2; + my $link_id = lc $3; + + if ($link_id eq "") { + $link_id = lc $link_text; # for shortcut links like [this][]. + } + + if (defined $g_urls{$link_id}) { + my $url = $g_urls{$link_id}; + $url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid + $url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics/bold. + $result = "? # href = $3 + [ \t]* + ( # $4 + (['"]) # quote char = $5 + (.*?) # Title = $6 + \5 # matching quote + )? # title is optional + \) + ) + }{ + my $result; + my $whole_match = $1; + my $link_text = $2; + my $url = $3; + my $title = $6; + + $url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid + $url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics/bold. + $result = " tags. +# + my $text = shift; + + # + # First, handle reference-style labeled images: ![alt text][id] + # + $text =~ s{ + ( # wrap whole match in $1 + !\[ + (.*?) # alt text = $2 + \] + + [ ]? # one optional space + (?:\n[ ]*)? # one optional newline followed by spaces + + \[ + (.*?) # id = $3 + \] + + ) + }{ + my $result; + my $whole_match = $1; + my $alt_text = $2; + my $link_id = lc $3; + + if ($link_id eq "") { + $link_id = lc $alt_text; # for shortcut links like ![this][]. + } + + $alt_text =~ s/"/"/g; + if (defined $g_urls{$link_id}) { + my $url = $g_urls{$link_id}; + $url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid + $url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics/bold. + $result = "\"$alt_text\"";? # src url = $3 + [ \t]* + ( # $4 + (['"]) # quote char = $5 + (.*?) # title = $6 + \5 # matching quote + [ \t]* + )? # title is optional + \) + ) + }{ + my $result; + my $whole_match = $1; + my $alt_text = $2; + my $url = $3; + my $title = ''; + if (defined($6)) { + $title = $6; + } + + $alt_text =~ s/"/"/g; + $title =~ s/"/"/g; + $url =~ s! \* !$g_escape_table{'*'}!gx; # We've got to encode these to avoid + $url =~ s! _ !$g_escape_table{'_'}!gx; # conflicting with italics/bold. + $result = "\"$alt_text\"";" . _RunSpanGamut($1) . "\n\n"; + }egmx; + + $text =~ s{ ^(.+)[ \t]*\n-+[ \t]*\n+ }{ + "

" . _RunSpanGamut($1) . "

\n\n"; + }egmx; + + + # atx-style headers: + # # Header 1 + # ## Header 2 + # ## Header 2 with closing hashes ## + # ... + # ###### Header 6 + # + $text =~ s{ + ^(\#{1,6}) # $1 = string of #'s + [ \t]* + (.+?) # $2 = Header text + [ \t]* + \#* # optional closing #'s (not counted) + \n+ + }{ + my $h_level = length($1); + "" . _RunSpanGamut($2) . "\n\n"; + }egmx; + + return $text; +} + + +sub _DoLists { +# +# Form HTML ordered (numbered) and unordered (bulleted) lists. +# + my $text = shift; + my $less_than_tab = $g_tab_width - 1; + + # Re-usable patterns to match list item bullets and number markers: + my $marker_ul = qr/[*+-]/; + my $marker_ol = qr/\d+[.]/; + my $marker_any = qr/(?:$marker_ul|$marker_ol)/; + + # Re-usable pattern to match any entirel ul or ol list: + my $whole_list = qr{ + ( # $1 = whole list + ( # $2 + [ ]{0,$less_than_tab} + (${marker_any}) # $3 = first list item marker + [ \t]+ + ) + (?s:.+?) + ( # $4 + \z + | + \n{2,} + (?=\S) + (?! # Negative lookahead for another list item marker + [ \t]* + ${marker_any}[ \t]+ + ) + ) + ) + }mx; + + # We use a different prefix before nested lists than top-level lists. + # See extended comment in _ProcessListItems(). + # + # Note: There's a bit of duplication here. My original implementation + # created a scalar regex pattern as the conditional result of the test on + # $g_list_level, and then only ran the $text =~ s{...}{...}egmx + # substitution once, using the scalar as the pattern. This worked, + # everywhere except when running under MT on my hosting account at Pair + # Networks. There, this caused all rebuilds to be killed by the reaper (or + # perhaps they crashed, but that seems incredibly unlikely given that the + # same script on the same server ran fine *except* under MT. I've spent + # more time trying to figure out why this is happening than I'd like to + # admit. My only guess, backed up by the fact that this workaround works, + # is that Perl optimizes the substition when it can figure out that the + # pattern will never change, and when this optimization isn't on, we run + # afoul of the reaper. Thus, the slightly redundant code to that uses two + # static s/// patterns rather than one conditional pattern. + + if ($g_list_level) { + $text =~ s{ + ^ + $whole_list + }{ + my $list = $1; + my $list_type = ($3 =~ m/$marker_ul/) ? "ul" : "ol"; + # Turn double returns into triple returns, so that we can make a + # paragraph for the last item in a list, if necessary: + $list =~ s/\n{2,}/\n\n\n/g; + my $result = _ProcessListItems($list, $marker_any); + $result = "<$list_type>\n" . $result . "\n"; + $result; + }egmx; + } + else { + $text =~ s{ + (?:(?<=\n\n)|\A\n?) + $whole_list + }{ + my $list = $1; + my $list_type = ($3 =~ m/$marker_ul/) ? "ul" : "ol"; + # Turn double returns into triple returns, so that we can make a + # paragraph for the last item in a list, if necessary: + $list =~ s/\n{2,}/\n\n\n/g; + my $result = _ProcessListItems($list, $marker_any); + $result = "<$list_type>\n" . $result . "\n"; + $result; + }egmx; + } + + + return $text; +} + + +sub _ProcessListItems { +# +# Process the contents of a single ordered or unordered list, splitting it +# into individual list items. +# + + my $list_str = shift; + my $marker_any = shift; + + + # The $g_list_level global keeps track of when we're inside a list. + # Each time we enter a list, we increment it; when we leave a list, + # we decrement. If it's zero, we're not in a list anymore. + # + # We do this because when we're not inside a list, we want to treat + # something like this: + # + # I recommend upgrading to version + # 8. Oops, now this line is treated + # as a sub-list. + # + # As a single paragraph, despite the fact that the second line starts + # with a digit-period-space sequence. + # + # Whereas when we're inside a list (or sub-list), that line will be + # treated as the start of a sub-list. What a kludge, huh? This is + # an aspect of Markdown's syntax that's hard to parse perfectly + # without resorting to mind-reading. Perhaps the solution is to + # change the syntax rules such that sub-lists must start with a + # starting cardinal number; e.g. "1." or "a.". + + $g_list_level++; + + # trim trailing blank lines: + $list_str =~ s/\n{2,}\z/\n/; + + + $list_str =~ s{ + (\n)? # leading line = $1 + (^[ \t]*) # leading whitespace = $2 + ($marker_any) [ \t]+ # list marker = $3 + ((?s:.+?) # list item text = $4 + (\n{1,2})) + (?= \n* (\z | \2 ($marker_any) [ \t]+)) + }{ + my $item = $4; + my $leading_line = $1; + my $leading_space = $2; + + if ($leading_line or ($item =~ m/\n{2,}/)) { + $item = _RunBlockGamut(_Outdent($item)); + } + else { + # Recursion for sub-lists: + $item = _DoLists(_Outdent($item)); + chomp $item; + $item = _RunSpanGamut($item); + } + + "
  • " . $item . "
  • \n"; + }egmx; + + $g_list_level--; + return $list_str; +} + + + +sub _DoCodeBlocks { +# +# Process Markdown `
    ` blocks.
    +#	
    +
    +	my $text = shift;
    +
    +	$text =~ s{
    +			(?:\n\n|\A)
    +			(	            # $1 = the code block -- one or more lines, starting with a space/tab
    +			  (?:
    +			    (?:[ ]{$g_tab_width} | \t)  # Lines must start with a tab or a tab-width of spaces
    +			    .*\n+
    +			  )+
    +			)
    +			((?=^[ ]{0,$g_tab_width}\S)|\Z)	# Lookahead for non-space at line-start, or end of doc
    +		}{
    +			my $codeblock = $1;
    +			my $result; # return value
    +
    +			$codeblock = _EncodeCode(_Outdent($codeblock));
    +			$codeblock = _Detab($codeblock);
    +			$codeblock =~ s/\A\n+//; # trim leading newlines
    +			$codeblock =~ s/\s+\z//; # trim trailing whitespace
    +
    +			$result = "\n\n
    " . $codeblock . "\n
    \n\n"; + + $result; + }egmx; + + return $text; +} + + +sub _DoCodeSpans { +# +# * Backtick quotes are used for spans. +# +# * You can use multiple backticks as the delimiters if you want to +# include literal backticks in the code span. So, this input: +# +# Just type ``foo `bar` baz`` at the prompt. +# +# Will translate to: +# +#

    Just type foo `bar` baz at the prompt.

    +# +# There's no arbitrary limit to the number of backticks you +# can use as delimters. If you need three consecutive backticks +# in your code, use four for delimiters, etc. +# +# * You can use spaces to get literal backticks at the edges: +# +# ... type `` `bar` `` ... +# +# Turns to: +# +# ... type `bar` ... +# + + my $text = shift; + + $text =~ s@ + (`+) # $1 = Opening run of ` + (.+?) # $2 = The code block + (?$c
    "; + @egsx; + + return $text; +} + + +sub _EncodeCode { +# +# Encode/escape certain characters inside Markdown code runs. +# The point is that in code, these characters are literals, +# and lose their special Markdown meanings. +# + local $_ = shift; + + # Encode all ampersands; HTML entities are not + # entities within a Markdown code span. + s/&/&/g; + + # Encode $'s, but only if we're running under Blosxom. + # (Blosxom interpolates Perl variables in article bodies.) + { + no warnings 'once'; + if (defined($blosxom::version)) { + s/\$/$/g; + } + } + + + # Do the angle bracket song and dance: + s! < !<!gx; + s! > !>!gx; + + # Now, escape characters that are magic in Markdown: + s! \* !$g_escape_table{'*'}!gx; + s! _ !$g_escape_table{'_'}!gx; + s! { !$g_escape_table{'{'}!gx; + s! } !$g_escape_table{'}'}!gx; + s! \[ !$g_escape_table{'['}!gx; + s! \] !$g_escape_table{']'}!gx; + s! \\ !$g_escape_table{'\\'}!gx; + + return $_; +} + + +sub _DoItalicsAndBold { + my $text = shift; + + # must go first: + $text =~ s{ (\*\*|__) (?=\S) (.+?[*_]*) (?<=\S) \1 } + {$2}gsx; + + $text =~ s{ (\*|_) (?=\S) (.+?) (?<=\S) \1 } + {$2}gsx; + + return $text; +} + + +sub _DoBlockQuotes { + my $text = shift; + + $text =~ s{ + ( # Wrap whole match in $1 + ( + ^[ \t]*>[ \t]? # '>' at the start of a line + .+\n # rest of the first line + (.+\n)* # subsequent consecutive lines + \n* # blanks + )+ + ) + }{ + my $bq = $1; + $bq =~ s/^[ \t]*>[ \t]?//gm; # trim one level of quoting + $bq =~ s/^[ \t]+$//mg; # trim whitespace-only lines + $bq = _RunBlockGamut($bq); # recurse + + $bq =~ s/^/ /g; + # These leading spaces screw with
     content, so we need to fix that:
    +			$bq =~ s{
    +					(\s*
    .+?
    ) + }{ + my $pre = $1; + $pre =~ s/^ //mg; + $pre; + }egsx; + + "
    \n$bq\n
    \n\n"; + }egmx; + + + return $text; +} + + +sub _FormParagraphs { +# +# Params: +# $text - string to process with html

    tags +# + my $text = shift; + + # Strip leading and trailing lines: + $text =~ s/\A\n+//; + $text =~ s/\n+\z//; + + my @grafs = split(/\n{2,}/, $text); + + # + # Wrap

    tags. + # + foreach (@grafs) { + unless (defined( $g_html_blocks{$_} )) { + $_ = _RunSpanGamut($_); + s/^([ \t]*)/

    /; + $_ .= "

    "; + } + } + + # + # Unhashify HTML blocks + # + foreach (@grafs) { + if (defined( $g_html_blocks{$_} )) { + $_ = $g_html_blocks{$_}; + } + } + + return join "\n\n", @grafs; +} + + +sub _EncodeAmpsAndAngles { +# Smart processing for ampersands and angle brackets that need to be encoded. + + my $text = shift; + + # Ampersand-encoding based entirely on Nat Irons's Amputator MT plugin: + # http://bumppo.net/projects/amputator/ + $text =~ s/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/&/g; + + # Encode naked <'s + $text =~ s{<(?![a-z/?\$!])}{<}gi; + + return $text; +} + + +sub _EncodeBackslashEscapes { +# +# Parameter: String. +# Returns: The string, with after processing the following backslash +# escape sequences. +# + local $_ = shift; + + s! \\\\ !$g_escape_table{'\\'}!gx; # Must process escaped backslashes first. + s! \\` !$g_escape_table{'`'}!gx; + s! \\\* !$g_escape_table{'*'}!gx; + s! \\_ !$g_escape_table{'_'}!gx; + s! \\\{ !$g_escape_table{'{'}!gx; + s! \\\} !$g_escape_table{'}'}!gx; + s! \\\[ !$g_escape_table{'['}!gx; + s! \\\] !$g_escape_table{']'}!gx; + s! \\\( !$g_escape_table{'('}!gx; + s! \\\) !$g_escape_table{')'}!gx; + s! \\> !$g_escape_table{'>'}!gx; + s! \\\# !$g_escape_table{'#'}!gx; + s! \\\+ !$g_escape_table{'+'}!gx; + s! \\\- !$g_escape_table{'-'}!gx; + s! \\\. !$g_escape_table{'.'}!gx; + s{ \\! }{$g_escape_table{'!'}}gx; + + return $_; +} + + +sub _DoAutoLinks { + my $text = shift; + + $text =~ s{<((https?|ftp):[^'">\s]+)>}{
    $1}gi; + + # Email addresses: + $text =~ s{ + < + (?:mailto:)? + ( + [-.\w]+ + \@ + [-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+ + ) + > + }{ + _EncodeEmailAddress( _UnescapeSpecialChars($1) ); + }egix; + + return $text; +} + + +sub _EncodeEmailAddress { +# +# Input: an email address, e.g. "foo@example.com" +# +# Output: the email address as a mailto link, with each character +# of the address encoded as either a decimal or hex entity, in +# the hopes of foiling most address harvesting spam bots. E.g.: +# +# foo +# @example.com +# +# Based on a filter by Matthew Wickline, posted to the BBEdit-Talk +# mailing list: +# + + my $addr = shift; + + srand; + my @encode = ( + sub { '&#' . ord(shift) . ';' }, + sub { '&#x' . sprintf( "%X", ord(shift) ) . ';' }, + sub { shift }, + ); + + $addr = "mailto:" . $addr; + + $addr =~ s{(.)}{ + my $char = $1; + if ( $char eq '@' ) { + # this *must* be encoded. I insist. + $char = $encode[int rand 1]->($char); + } elsif ( $char ne ':' ) { + # leave ':' alone (to spot mailto: later) + my $r = rand; + # roughly 10% raw, 45% hex, 45% dec + $char = ( + $r > .9 ? $encode[2]->($char) : + $r < .45 ? $encode[1]->($char) : + $encode[0]->($char) + ); + } + $char; + }gex; + + $addr = qq{$addr}; + $addr =~ s{">.+?:}{">}; # strip the mailto: from the visible part + + return $addr; +} + + +sub _UnescapeSpecialChars { +# +# Swap back in all the special characters we've hidden. +# + my $text = shift; + + while( my($char, $hash) = each(%g_escape_table) ) { + $text =~ s/$hash/$char/g; + } + return $text; +} + + +sub _TokenizeHTML { +# +# Parameter: String containing HTML markup. +# Returns: Reference to an array of the tokens comprising the input +# string. Each token is either a tag (possibly with nested, +# tags contained therein, such as , or a +# run of text between tags. Each element of the array is a +# two-element array; the first is either 'tag' or 'text'; +# the second is the actual value. +# +# +# Derived from the _tokenize() subroutine from Brad Choate's MTRegex plugin. +# +# + + my $str = shift; + my $pos = 0; + my $len = length $str; + my @tokens; + + my $depth = 6; + my $nested_tags = join('|', ('(?:<[a-z/!$](?:[^<>]') x $depth) . (')*>)' x $depth); + my $match = qr/(?s: ) | # comment + (?s: <\? .*? \?> ) | # processing instruction + $nested_tags/ix; # nested tags + + while ($str =~ m/($match)/g) { + my $whole_tag = $1; + my $sec_start = pos $str; + my $tag_start = $sec_start - length $whole_tag; + if ($pos < $tag_start) { + push @tokens, ['text', substr($str, $pos, $tag_start - $pos)]; + } + push @tokens, ['tag', $whole_tag]; + $pos = pos $str; + } + push @tokens, ['text', substr($str, $pos, $len - $pos)] if $pos < $len; + \@tokens; +} + + +sub _Outdent { +# +# Remove one level of line-leading tabs or spaces +# + my $text = shift; + + $text =~ s/^(\t|[ ]{1,$g_tab_width})//gm; + return $text; +} + + +sub _Detab { +# +# Cribbed from a post by Bart Lateur: +# +# + my $text = shift; + + $text =~ s{(.*?)\t}{$1.(' ' x ($g_tab_width - length($1) % $g_tab_width))}ge; + return $text; +} + + +1; + +__END__ + + +=pod + +=head1 NAME + +B + + +=head1 SYNOPSIS + +B [ B<--html4tags> ] [ B<--version> ] [ B<-shortversion> ] + [ I ... ] + + +=head1 DESCRIPTION + +Markdown is a text-to-HTML filter; it translates an easy-to-read / +easy-to-write structured text format into HTML. Markdown's text format +is most similar to that of plain text email, and supports features such +as headers, *emphasis*, code blocks, blockquotes, and links. + +Markdown's syntax is designed not as a generic markup language, but +specifically to serve as a front-end to (X)HTML. You can use span-level +HTML tags anywhere in a Markdown document, and you can use block level +HTML tags (like
    and as well). + +For more information about Markdown's syntax, see: + + http://daringfireball.net/projects/markdown/ + + +=head1 OPTIONS + +Use "--" to end switch parsing. For example, to open a file named "-z", use: + + Markdown.pl -- -z + +=over 4 + + +=item B<--html4tags> + +Use HTML 4 style for empty element tags, e.g.: + +
    + +instead of Markdown's default XHTML style tags, e.g.: + +
    + + +=item B<-v>, B<--version> + +Display Markdown's version number and copyright information. + + +=item B<-s>, B<--shortversion> + +Display the short-form version number. + + +=back + + + +=head1 BUGS + +To file bug reports or feature requests (other than topics listed in the +Caveats section above) please send email to: + + support@daringfireball.net + +Please include with your report: (1) the example input; (2) the output +you expected; (3) the output Markdown actually produced. + + +=head1 VERSION HISTORY + +See the readme file for detailed release notes for this version. + +1.0.1 - 14 Dec 2004 + +1.0 - 28 Aug 2004 + + +=head1 AUTHOR + + John Gruber + http://daringfireball.net + + PHP port and other contributions by Michel Fortin + http://michelf.com + + +=head1 COPYRIGHT AND LICENSE + +Copyright (c) 2003-2004 John Gruber + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +* Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +* Neither the name "Markdown" nor the names of its contributors may + be used to endorse or promote products derived from this software + without specific prior written permission. + +This software is provided by the copyright holders and contributors "as +is" and any express or implied warranties, including, but not limited +to, the implied warranties of merchantability and fitness for a +particular purpose are disclaimed. In no event shall the copyright owner +or contributors be liable for any direct, indirect, incidental, special, +exemplary, or consequential damages (including, but not limited to, +procurement of substitute goods or services; loss of use, data, or +profits; or business interruption) however caused and on any theory of +liability, whether in contract, strict liability, or tort (including +negligence or otherwise) arising in any way out of the use of this +software, even if advised of the possibility of such damage. + +=cut diff --git a/bb.sh b/bb.sh new file mode 100755 index 0000000..d07ed1d --- /dev/null +++ b/bb.sh @@ -0,0 +1,1174 @@ +#!/usr/bin/env bash + +# BashBlog, a simple blog system written in a single bash script +# (C) Carlos Fenollosa , 2011-2016 and contributors +# https://github.com/carlesfe/bashblog/contributors +# Check out README.md for more details + +# Global variables +# It is recommended to perform a 'rebuild' after changing any of this in the code + +# Config file. Any settings "key=value" written there will override the +# global_variables defaults. Useful to avoid editing bb.sh and having to deal +# with merges in VCS +global_config=".config" + +# This function will load all the variables defined here. They might be overridden +# by the 'global_config' file contents +global_variables() { + global_software_name="bashblog" + global_software_version="2.8" + + # Blog title + global_title="my tildelog" + # The typical subtitle for each blog + global_description="a blog about tildes" + # The public base URL for this blog + global_url="https://tilde.team/~$USER/blog" + + # Your name + global_author="~$USER" + # You can use twitter or facebook or anything for global_author_url + global_author_url="https://tilde.team/~$USER/" + # Your email + global_email="$USER@tilde.team" + + # CC by-nc-nd is a good starting point, you can change this to "©" for Copyright + global_license="CC by-nc-nd" + + + # Leave this empty (i.e. "") if you don't want to use feedburner, + # or change it to your own URL + global_feedburner="" + + # Change this to your username if you want to use twitter for comments + global_twitter_username="" + # Set this to false for a Twitter button with share count. The cookieless version + # is just a link. + global_twitter_cookieless="true" + # Default search page, where tweets more than a week old are hidden + global_twitter_search="twitter" + + # Blog generated files + # index page of blog (it is usually good to use "index.html" here) + index_file="index.html" + number_of_index_articles="10" + # global archive + archive_index="all_posts.html" + tags_index="all_tags.html" + + # ignore gophermap file + gophermap="gophermap" + + # ignore gemini generation script and gemini index + gemini_index="index.gmi" + + # Non blogpost files. Bashblog will ignore these. Useful for static pages and custom content + # Add them as a bash array, e.g. non_blogpost_files=("news.html" "test.html") + non_blogpost_files=() + + # feed file (rss in this case) + blog_feed="feed.rss" + number_of_feed_articles="50" + # "cut" blog entry when putting it to index page. Leave blank for full articles in front page + # i.e. include only up to first '
    ', or '----' in markdown + cut_do="cut" + # When cutting, cut also tags? If "no", tags will appear in index page for cut articles + cut_tags="yes" + # Regexp matching the HTML line where to do the cut + # note that slash is regexp separator so you need to prepend it with backslash + cut_line='
    ' + # save markdown file when posting with "bb post -m". Leave blank to discard it. + save_markdown="yes" + # prefix for tags/categories files + # please make sure that no other html file starts with this prefix + prefix_tags="tag_" + # personalized header and footer (only if you know what you're doing) + # DO NOT name them .header.html, .footer.html or they will be overwritten + # leave blank to generate them, recommended + header_file="" + footer_file="" + # extra content to add just after we open the tag + # and before the actual blog content + body_begin_file="" + # extra content to add just before we cloese ) + body_end_file="" + # CSS files to include on every page, f.ex. css_include=('main.css' 'blog.css') + # leave empty to use generated + css_include=() + # HTML files to exclude from index, f.ex. post_exclude=('imprint.html 'aboutme.html') + html_exclude=() + + # Localization and i18n + # "Comments?" (used in twitter link after every post) + template_comments="comments?" + # "Read more..." (link under cut article on index page) + template_read_more="read more..." + # "View more posts" (used on bottom of index page as link to archive) + template_archive="archive" + # "All posts" (title of archive page) + template_archive_title="all posts" + # "All tags" + template_tags_title="all tags" + # "posts" (on "All tags" page, text at the end of each tag line, like "2. Music - 15 posts") + template_tags_posts="posts" + template_tags_posts_2_4="posts" # Some slavic languages use a different plural form for 2-4 items + template_tags_posts_singular="post" + # "Posts tagged" (text on a title of a page with index of one tag, like "My Blog - Posts tagged "Music"") + template_tag_title="posts tagged" + # "Tags:" (beginning of line in HTML file with list of all tags for this article) + template_tags_line_header="tags:" + # "Back to the index page" (used on archive page, it is link to blog index) + template_archive_index_page="back home" + # "Subscribe" (used on bottom of index page, it is link to RSS feed) + template_subscribe="rss" + # "Subscribe to this page..." (used as text for browser feed button that is embedded to html) + template_subscribe_browser_button="subscribe to this page..." + # "Tweet" (used as twitter text button for posting to twitter) + template_twitter_button="tweet" + template_twitter_comment="<type your comment here but please leave the URL so that other people can follow the comments>" + + # The locale to use for the dates displayed on screen + date_format="%B %d, %Y" + date_locale="C" + date_inpost="bashblog_timestamp" + # Don't change these dates + date_format_full="%a, %d %b %Y %H:%M:%S %z" + date_format_timestamp="%Y%m%d%H%M.%S" + date_allposts_header="%B %Y" + + # Perform the post title -> filename conversion + # Experts only. You may need to tune the locales too + # Leave empty for no conversion, which is not recommended + # This default filter respects backwards compatibility + convert_filename="iconv -f utf-8 -t ascii//translit | sed 's/^-*//' | tr [:upper:] [:lower:] | tr ' ' '-' | tr -dc '[:alnum:]-'" + + # URL where you can view the post while it's being edited + # same as global_url by default + # You can change it to path on your computer, if you write posts locally + # before copying them to the server + preview_url="" + + # Markdown location. Trying to autodetect by default. + # The invocation must support the signature 'markdown_bin in.md > out.html' + [[ -f Markdown.pl ]] && markdown_bin=./Markdown.pl || markdown_bin=$(which Markdown.pl 2>/dev/null || which markdown 2>/dev/null) +} + +# Check for the validity of some variables +# DO NOT EDIT THIS FUNCTION unless you know what you're doing +global_variables_check() { + [[ $header_file == .header.html ]] && + echo "Please check your configuration. '.header.html' is not a valid value for the setting 'header_file'" && + exit + [[ $footer_file == .footer.html ]] && + echo "Please check your configuration. '.footer.html' is not a valid value for the setting 'footer_file'" && + exit +} + + +# Test if the markdown script is working correctly +test_markdown() { + [[ -n $markdown_bin ]] && + ( + [[ $("$markdown_bin" <<< $'line 1\n\nline 2') == $'

    line 1

    \n\n

    line 2

    ' ]] || + [[ $("$markdown_bin" <<< $'line 1\n\nline 2') == $'

    line 1

    \n

    line 2

    ' ]] + ) +} + + +# Parse a Markdown file into HTML and return the generated file +markdown() { + out=${1%.md}.html + while [[ -f $out ]]; do out=${out%.html}.$RANDOM.html; done + $markdown_bin "$1" > "$out" + echo "$out" +} + +# Reads HTML file from stdin, prints its content to stdout +# $1 where to start ("text" or "entry") +# $2 where to stop ("text" or "entry") +# $3 "cut" to remove text from
    to +# note that this does not remove
    line itself, +# so you can see if text was cut or not +get_html_file_content() { + awk "//, //{ + if (!// && !//) print + if (\"$3\" == \"cut\" && /$cut_line/){ + if (\"$2\" == \"text\") exit # no need to read further + while (getline > 0 && !//) { + if (\"$cut_tags\" == \"no\" && /^

    $template_tags_line_header/ ) print + } + } + }" +} + +# Edit an existing, published .html file while keeping its original timestamp +# Please note that this function does not automatically republish anything, as +# it is usually called from 'main'. +# +# Note that it edits HTML file, even if you wrote the post as markdown originally +# Note that if you edit title then filename might also change +# +# $1 the file to edit +# $2 (optional) edit mode: +# "keep" to keep old filename +# "full" to edit full HTML, and not only text part (keeps old filename) +# leave empty for default behavior (edit only text part and change name) +edit() { + [[ ! -f "${1%%.*}.html" ]] && echo "Can't edit post "${1%%.*}.html", did you mean to use \"bb.sh post \"?" && exit -1 + # Original post timestamp + edit_timestamp=$(LC_ALL=C date -r "${1%%.*}.html" +"$date_format_full" ) + touch_timestamp=$(LC_ALL=C date -r "${1%%.*}.html" +"$date_format_timestamp") + tags_before=$(tags_in_post "${1%%.*}.html") + if [[ $2 == full ]]; then + $EDITOR "$1" + filename=$1 + else + if [[ ${1##*.} == md ]]; then + test_markdown + if (($? != 0)); then + echo "Markdown is not working, please edit HTML file directly." + exit + fi + # editing markdown file + $EDITOR "$1" + TMPFILE=$(markdown "$1") + filename=${1%%.*}.html + else + # Create the content file + TMPFILE=$(basename "$1").$RANDOM.html + # Title + get_post_title "$1" > "$TMPFILE" + # Post text with plaintext tags + get_html_file_content 'text' 'text' <"$1" | sed "/^

    $template_tags_line_header/s|\\1|\\1|g" >> "$TMPFILE" + $EDITOR "$TMPFILE" + filename=$1 + fi + rm "$filename" + if [[ $2 == keep ]]; then + parse_file "$TMPFILE" "$edit_timestamp" "$filename" + else + parse_file "$TMPFILE" "$edit_timestamp" # this command sets $filename as the html processed file + [[ ${1##*.} == md ]] && mv "$1" "${filename%%.*}.md" 2>/dev/null + fi + rm "$TMPFILE" + fi + touch -t "$touch_timestamp" "$filename" + touch -t "$touch_timestamp" "$1" + chmod 644 "$filename" + echo "Posted $filename" + tags_after=$(tags_in_post "$filename") + relevant_tags=$(echo "$tags_before $tags_after" | tr ',' ' ' | tr ' ' '\n' | sort -u | tr '\n' ' ') + if [[ ! -z $relevant_tags ]]; then + relevant_posts="$(posts_with_tags $relevant_tags) $filename" + rebuild_tags "$relevant_posts" "$relevant_tags" + fi +} + +# Create a Twitter summary (twitter "card") for the post +# +# $1 the post file +# $2 the title +twitter_card() { + [[ -z $global_twitter_username ]] && return + + echo "" + echo "" + echo "" # Twitter truncates at 70 char + description=$(grep -v "^

    $template_tags_line_header" "$1" | sed -e 's/<[^>]*>//g' | head -c 250 | tr '\n' ' ' | sed "s/\"/'/g") + echo "" + image=$(sed -n 's/.*" +} + +# Adds the code needed by the twitter button +# +# $1 the post URL +twitter() { + [[ -z $global_twitter_username ]] && return + + if [[ $global_twitter_cookieless == true ]]; then + id=$RANDOM + + search_engine="https://twitter.com/search?q=" + + echo "

    $template_comments $template_twitter_button " + echo " 

    " + return; + else + echo "

    $template_comments "; + fi + + echo "$template_twitter_button " + echo "

    " +} + +# Check if the file is a 'boilerplate' (i.e. not a post) +# The return values are designed to be used like this inside a loop: +# is_boilerplate_file && continue +# +# $1 the file +# +# Return 0 (bash return value 'true') if the input file is an index, feed, etc +# or 1 (bash return value 'false') if it is a blogpost +is_boilerplate_file() { + name=${1#./} + # First check against user-defined non-blogpost pages + for item in "${non_blogpost_files[@]}"; do + [[ "$name" == "$item" ]] && return 0 + done + + case $name in + ( "$index_file" | "$archive_index" | "$gophermap" | "$gemini_index" | "$tags_index" | "$footer_file" | "$header_file" | "$global_analytics_file" | "$prefix_tags"* ) + return 0 ;; + ( * ) # Check for excluded + for excl in "${html_exclude[@]}"; do + [[ $name == "$excl" ]] && return 0 + done + return 1 ;; + esac +} + +# Adds all the bells and whistles to format the html page +# Every blog post is marked with a and +# which is parsed afterwards in the other functions. There is also a marker +# to determine just the beginning of the text body of the post +# +# $1 a file with the body of the content +# $2 the output file +# $3 "yes" if we want to generate the index.html, +# "no" to insert new blog posts +# $4 title for the html header +# $5 original blog timestamp +# $6 post author +create_html_page() { + content=$1 + filename=$2 + index=$3 + title=$4 + timestamp=$5 + author=$6 + + # Create the actual blog post + # html, head + { + cat ".header.html" + echo "$title" + twitter_card "$content" "$title" + echo "" + # stuff to add before the actual body content + [[ -n $body_begin_file ]] && cat "$body_begin_file" + # body divs + echo '
    ' + echo '
    ' + # blog title + echo '
    ' + cat .title.html + echo '
    ' # title, header, headerholder + echo '
    ' + + file_url=${filename#./} + file_url=${file_url%.rebuilt} # Get the correct URL when rebuilding + # one blog entry + if [[ $index == no ]]; then + echo '' # marks the beginning of the whole post + echo "

    " + # remove possible

    's on the title because of markdown conversion + title=${title//

    /} + title=${title//<\/p>/} + echo "$title" + echo '

    ' + if [[ -z $timestamp ]]; then + echo "" + else + echo "" + fi + if [[ -z $timestamp ]]; then + echo -n "
    $(LC_ALL=$date_locale date +"$date_format")" + else + echo -n "
    $(LC_ALL=$date_locale date +"$date_format" --date="$timestamp")" + fi + [[ -n $author ]] && echo -e " — \n$author" + echo "
    " + echo '' # This marks the text body, after the title, date... + fi + cat "$content" # Actual content + if [[ $index == no ]]; then + echo -e '\n' + + twitter "$global_url/$file_url" + + echo '' # absolute end of the post + fi + + echo '
    ' # content + + # page footer + cat .footer.html + # close divs + echo '
    ' # divbody and divbodyholder + [[ -n $body_end_file ]] && cat "$body_end_file" + echo '' + } > "$filename" +} + +# Parse the plain text file into an html file +# +# $1 source file name +# $2 (optional) timestamp for the file +# $3 (optional) destination file name +# note that although timestamp is optional, something must be provided at its +# place if destination file name is provided, i.e: +# parse_file source.txt "" destination.html +parse_file() { + # Read for the title and check that the filename is ok + title="" + while IFS='' read -r line; do + if [[ -z $title ]]; then + # remove extra

    and

    added by markdown + title=$(echo "$line" | sed 's/<\/*p>//g') + if [[ -n $3 ]]; then + filename=$3 + else + filename=$title + [[ -n $convert_filename ]] && + filename=$(echo "$title" | eval "$convert_filename") + [[ -n $filename ]] || + filename=$RANDOM # don't allow empty filenames + + filename=$filename.html + + # Check for duplicate file names + while [[ -f $filename ]]; do + filename=${filename%.html}$RANDOM.html + done + fi + content=$filename.tmp + # Parse possible tags + elif [[ $line == "

    $template_tags_line_header"* ]]; then + tags=$(echo "$line" | cut -d ":" -f 2- | sed -e 's/<\/p>//g' -e 's/^ *//' -e 's/ *$//' -e 's/, /,/g') + IFS=, read -r -a array <<< "$tags" + + echo -n "

    $template_tags_line_header " >> "$content" + for item in "${array[@]}"; do + echo -n "$item, " + done | sed 's/, $/<\/p>/g' >> "$content" + else + echo "$line" >> "$content" + fi + done < "$1" + + # Create the actual html page + create_html_page "$content" "$filename" no "$title" "$2" "$global_author" + rm "$content" +} + +# Manages the creation of the text file and the parsing to html file +# also the drafts +write_entry() { + test_markdown && fmt=md || fmt=html + f=$2 + [[ $2 == -html ]] && fmt=html && f=$3 + + if [[ -n $f ]]; then + TMPFILE=$f + if [[ ! -f $TMPFILE ]]; then + echo "The file doesn't exist" + delete_includes + exit + fi + # guess format from TMPFILE + extension=${TMPFILE##*.} + [[ $extension == md || $extension == html ]] && fmt=$extension + # but let user override it (`bb.sh post -html file.md`) + [[ $2 == -html ]] && fmt=html + # Test if Markdown is working before re-posting a .md file + if [[ $extension == md ]]; then + test_markdown + if (($? != 0)); then + echo "Markdown is not working, please edit HTML file directly." + exit + fi + fi + else + TMPFILE=.entry-$RANDOM.$fmt + echo -e "Title on this line\n" >> "$TMPFILE" + + [[ $fmt == html ]] && cat << EOF >> "$TMPFILE" +

    The rest of the text file is an html blog post. The process will continue as soon +as you exit your editor.

    + +

    $template_tags_line_header keep-this-tag-format, tags-are-optional, example

    +EOF + [[ $fmt == md ]] && cat << EOF >> "$TMPFILE" +The rest of the text file is a **Markdown** blog post. The process will continue +as soon as you exit your editor. + +$template_tags_line_header keep-this-tag-format, tags-are-optional, beware-with-underscores-in-markdown, example +EOF + fi + chmod 600 "$TMPFILE" + + post_status="E" + filename="" + while [[ $post_status != "p" && $post_status != "P" ]]; do + [[ -n $filename ]] && rm "$filename" # Delete the generated html file, if any + $EDITOR "$TMPFILE" + if [[ $fmt == md ]]; then + html_from_md=$(markdown "$TMPFILE") + parse_file "$html_from_md" + rm "$html_from_md" + else + parse_file "$TMPFILE" # this command sets $filename as the html processed file + fi + + chmod 644 "$filename" + [[ -n $preview_url ]] || preview_url=$global_url + echo "To preview the entry, open $preview_url/$filename in your browser" + + echo -n "[P]ost this entry, [E]dit again, [D]raft for later? (p/E/d) " + read -r post_status + if [[ $post_status == d || $post_status == D ]]; then + mkdir -p "drafts/" + chmod 700 "drafts/" + + title=$(head -n 1 $TMPFILE) + [[ -n $convert_filename ]] && title=$(echo "$title" | eval "$convert_filename") + [[ -n $title ]] || title=$RANDOM + + draft=drafts/$title.$fmt + mv "$TMPFILE" "$draft" + chmod 600 "$draft" + rm "$filename" + delete_includes + echo "Saved your draft as '$draft'" + exit + fi + done + + if [[ $fmt == md && -n $save_markdown ]]; then + mv "$TMPFILE" "${filename%%.*}.md" + else + rm "$TMPFILE" + fi + chmod 644 "$filename" + echo "Posted $filename" + relevant_tags=$(tags_in_post $filename) + if [[ -n $relevant_tags ]]; then + relevant_posts="$(posts_with_tags $relevant_tags) $filename" + rebuild_tags "$relevant_posts" "$relevant_tags" + fi +} + +# Create an index page with all the posts +all_posts() { + echo -n "Creating an index page with all the posts " + contentfile=$archive_index.$RANDOM + while [[ -f $contentfile ]]; do + contentfile=$archive_index.$RANDOM + done + + { + echo "

    $template_archive_title

    " + prev_month="" + while IFS='' read -r i; do + is_boilerplate_file "$i" && continue + echo -n "." 1>&3 + # Month headers + month=$(LC_ALL=$date_locale date -r "$i" +"$date_allposts_header") + if [[ $month != "$prev_month" ]]; then + [[ -n $prev_month ]] && echo "" # Don't close ul before first header + echo "

    $month

    " + echo "
      " + prev_month=$month + fi + # Title + title=$(get_post_title "$i") + echo -n "
    • $title —" + # Date + date=$(LC_ALL=$date_locale date -r "$i" +"$date_format") + echo " $date
    • " + done < <(ls -t ./*.html) + echo "" 1>&3 + echo "
    " + echo "" + } 3>&1 >"$contentfile" + + create_html_page "$contentfile" "$archive_index.tmp" yes "$global_title — $template_archive_title" "$global_author" + mv "$archive_index.tmp" "$archive_index" + chmod 644 "$archive_index" + rm "$contentfile" +} + +# Create an index page with all the tags +all_tags() { + echo -n "Creating an index page with all the tags " + contentfile=$tags_index.$RANDOM + while [[ -f $contentfile ]]; do + contentfile=$tags_index.$RANDOM + done + + { + echo "

    $template_tags_title

    " + echo "
      " + for i in $prefix_tags*.html; do + [[ -f "$i" ]] || break + echo -n "." 1>&3 + nposts=$(grep -c "<\!-- text begin -->" "$i") + tagname=${i#"$prefix_tags"} + tagname=${tagname%.html} + case $nposts in + 1) word=$template_tags_posts_singular;; + 2|3|4) word=$template_tags_posts_2_4;; + *) word=$template_tags_posts;; + esac + echo "
    • $tagname — $nposts $word
    • " + done + echo "" 1>&3 + echo "
    " + echo "" + } 3>&1 > "$contentfile" + + create_html_page "$contentfile" "$tags_index.tmp" yes "$global_title — $template_tags_title" "$global_author" + mv "$tags_index.tmp" "$tags_index" + chmod 644 "$tags_index" + rm "$contentfile" +} + +# Generate the index.html with the content of the latest posts +rebuild_index() { + echo -n "Rebuilding the index " + newindexfile=$index_file.$RANDOM + contentfile=$newindexfile.content + while [[ -f $newindexfile ]]; do + newindexfile=$index_file.$RANDOM + contentfile=$newindexfile.content + done + + # Create the content file + { + n=0 + while IFS='' read -r i; do + is_boilerplate_file "$i" && continue; + if ((n >= number_of_index_articles)); then break; fi + if [[ -n $cut_do ]]; then + get_html_file_content 'entry' 'entry' 'cut' <"$i" | awk "/$cut_line/ { print \"

    $template_read_more

    \" ; next } 1" + else + get_html_file_content 'entry' 'entry' <"$i" + fi + echo -n "." 1>&3 + n=$(( n + 1 )) + done < <(ls -t ./*.html) # sort by date, newest first + + feed=$blog_feed + if [[ -n $global_feedburner ]]; then feed=$global_feedburner; fi + echo "" + } 3>&1 >"$contentfile" + + echo "" + + create_html_page "$contentfile" "$newindexfile" yes "$global_title" "$global_author" + rm "$contentfile" + mv "$newindexfile" "$index_file" + chmod 644 "$index_file" +} + +# Finds all tags referenced in one post. +# Accepts either filename as first argument, or post content at stdin +# Prints one line with space-separated tags to stdout +tags_in_post() { + sed -n "/^

    $template_tags_line_header/{s/^

    $template_tags_line_header//;s/<[^>]*>//g;s/[ ,]\+/ /g;p;}" "$1" | tr ', ' ' ' +} + +# Finds all posts referenced in a number of tags. +# Arguments are tags +# Prints one line with space-separated tags to stdout +posts_with_tags() { + (($# < 1)) && return + set -- "${@/#/$prefix_tags}" + set -- "${@/%/.html}" + sed -n '/^

    /{s/.*href="\([^"]*\)">.*/\1/;p;}' "$@" 2> /dev/null +} + +# Rebuilds tag_*.html files +# if no arguments given, rebuilds all of them +# if arguments given, they should have this format: +# "FILE1 [FILE2 [...]]" "TAG1 [TAG2 [...]]" +# where FILEn are files with posts which should be used for rebuilding tags, +# and TAGn are names of tags which should be rebuilt. +# example: +# rebuild_tags "one_post.html another_article.html" "example-tag another-tag" +# mind the quotes! +rebuild_tags() { + if (($# < 2)); then + # will process all files and tags + files=$(ls -t ./*.html) + all_tags=yes + else + # will process only given files and tags + files=$(printf '%s\n' $1 | sort -u) + files=$(ls -t $files) + tags=$2 + fi + echo -n "Rebuilding tag pages " + n=0 + if [[ -n $all_tags ]]; then + rm ./"$prefix_tags"*.html &> /dev/null + else + for i in $tags; do + rm "./$prefix_tags$i.html" &> /dev/null + done + fi + # First we will process all files and create temporal tag files + # with just the content of the posts + tmpfile=tmp.$RANDOM + while [[ -f $tmpfile ]]; do tmpfile=tmp.$RANDOM; done + while IFS='' read -r i; do + is_boilerplate_file "$i" && continue; + echo -n "." + if [[ -n $cut_do ]]; then + get_html_file_content 'entry' 'entry' 'cut' <"$i" | awk "/$cut_line/ { print \"

    $template_read_more

    \" ; next } 1" + else + get_html_file_content 'entry' 'entry' <"$i" + fi >"$tmpfile" + for tag in $(tags_in_post "$i"); do + if [[ -n $all_tags || " $tags " == *" $tag "* ]]; then + cat "$tmpfile" >> "$prefix_tags$tag".tmp.html + fi + done + done <<< "$files" + rm "$tmpfile" + # Now generate the tag files with headers, footers, etc + while IFS='' read -r i; do + tagname=${i#./"$prefix_tags"} + tagname=${tagname%.tmp.html} + create_html_page "$i" "$prefix_tags$tagname.html" yes "$global_title — $template_tag_title \"$tagname\"" "$global_author" + rm "$i" + done < <(ls -t ./"$prefix_tags"*.tmp.html 2>/dev/null) + echo +} + +# Return the post title +# +# $1 the html file +get_post_title() { + awk '/

    /, /<\/a><\/h3>/{if (!/

    / && !/<\/a><\/h3>/) print}' "$1" +} + +# Return the post author +# +# $1 the html file +get_post_author() { + awk '/
    .+/, //{if (!/
    .+/ && !//) print}' "$1" | sed 's/<\/div>//g' +} + +# Displays a list of the tags +# +# $2 if "-n", tags will be sorted by number of posts +list_tags() { + if [[ $2 == -n ]]; then do_sort=1; else do_sort=0; fi + + ls ./$prefix_tags*.html &> /dev/null + (($? != 0)) && echo "No posts yet. Use 'bb.sh post' to create one" && return + + lines="" + for i in $prefix_tags*.html; do + [[ -f "$i" ]] || break + nposts=$(grep -c "<\!-- text begin -->" "$i") + tagname=${i#"$prefix_tags"} + tagname=${tagname#.html} + ((nposts > 1)) && word=$template_tags_posts || word=$template_tags_posts_singular + line="$tagname # $nposts # $word" + lines+=$line\\n + done + + if (( do_sort == 1 )); then + echo -e "$lines" | column -t -s "#" | sort -nrk 2 + else + echo -e "$lines" | column -t -s "#" + fi +} + +# Displays a list of the posts +list_posts() { + ls ./*.html &> /dev/null + (($? != 0)) && echo "No posts yet. Use 'bb.sh post' to create one" && return + + lines="" + n=1 + while IFS='' read -r i; do + is_boilerplate_file "$i" && continue + line="$n # $(get_post_title "$i") # $(LC_ALL=$date_locale date -r "$i" +"$date_format")" + lines+=$line\\n + n=$(( n + 1 )) + done < <(ls -t ./*.html) + + echo -e "$lines" | column -t -s "#" +} + +# Generate the feed file +make_rss() { + echo -n "Making RSS " + + rssfile=$blog_feed.$RANDOM + while [[ -f $rssfile ]]; do rssfile=$blog_feed.$RANDOM; done + + { + pubdate=$(LC_ALL=C date +"$date_format_full") + echo '' + echo '' + echo "$global_title$global_url/$index_file" + echo "$global_descriptionen" + echo "$pubdate" + echo "$pubdate" + echo "" + + n=0 + while IFS='' read -r i; do + is_boilerplate_file "$i" && continue + ((n >= number_of_feed_articles)) && break # max 10 items + echo -n "." 1>&3 + echo '' + get_post_title "$i" + echo '$global_url/${i#./}" + echo "$global_url/$i" + echo "$(get_post_author "$i")" + echo "$(LC_ALL=C date -r "$i" +"$date_format_full")" + + n=$(( n + 1 )) + done < <(ls -t ./*.html) + + echo '' + } 3>&1 >"$rssfile" + echo "" + + mv "$rssfile" "$blog_feed" + chmod 644 "$blog_feed" +} + +make_gophermap() { + if [ ! -d "${HOME}/public_gopher" ]; then + printf "Creating gopher hole\\n" + mkdir "${HOME}/public_gopher" + fi + + if [ ! -L "${HOME}/public_gopher/blog" ]; then + ln -s "${HOME}/public_html/blog/" "${HOME}/public_gopher/blog" + fi + + if [ ! -f "${HOME}/public_gopher/blog/$gophermap" ]; then + cat <<- 'EOF' > $HOME/public_html/blog/$gophermap + #!/usr/bin/env sh + printf "my bashblog posts\n" + user=$(stat -c '%U' .) + for post in $(ls -t *.md); do + post=$(basename $post) + printf "0$post\t/~$user/blog/$post\ttilde.team\t70\n" + done + EOF + chmod +x $HOME/public_html/blog/$gophermap + fi + chmod 644 *.md +} + +make_gemini() { + if [ ! -d "${HOME}/public_gemini" ]; then + printf "Creating ~/public_gemini\\n" + mkdir "${HOME}/public_gemini" + fi + + if [ ! -L "${HOME}/public_gemini/blog" ]; then + ln -s "${HOME}/public_html/blog/" "${HOME}/public_gopher/blog" + fi + + if [ ! -f "${HOME}/public_gemini/blog/$gemini_index" ]; then + cat <<- 'EOF' > $HOME/public_gemini/blog/$gemini_index + #!/usr/bin/env sh + printf "20 text/gemini\r\n" + printf "my bashblog posts\r\n" + user=$(stat -c '%U' $0) + for post in $(ls -t /home/$user/public_gemini/blog/*.md); do + post=$(basename $post) + printf "=> /~$user/blog/$post $post\r\n" + done + EOF + chmod +x ${HOME}/public_gemini/blog/$gemini_index + fi +} + +# generate headers, footers, etc +create_includes() { + { + echo "

    $global_title

    " + echo "
    $global_description
    " + } > ".title.html" + + if [[ -f $header_file ]]; then cp "$header_file" .header.html + else { + echo '' + echo '' + echo '' + echo '' + printf '\n' "${css_include[@]}" + if [[ -z $global_feedburner ]]; then + echo "" + else + echo "" + fi + } > ".header.html" + fi + + if [[ -f $footer_file ]]; then cp "$footer_file" .footer.html + else { + protected_mail=${global_email//@/@} + protected_mail=${protected_mail//./.} + echo "
    $global_license $global_author$protected_mail
    " + echo 'generated with bashblog, a single bash script to easily create blogs like this one
    ' + } >> ".footer.html" + fi +} + +# Delete the temporarily generated include files +delete_includes() { + rm ".title.html" ".footer.html" ".header.html" +} + +# Create the css file from scratch +create_css() { + # To avoid overwriting manual changes. However it is recommended that + # this function is modified if the user changes the blog.css file + (( ${#css_include[@]} > 0 )) && return || css_include=('main.css' 'blog.css') + if [[ ! -f blog.css ]]; then + # blog.css directives will be loaded after main.css and thus will prevail + echo '#title{font-size: x-large;} + a.ablack{color:black !important;} + li{margin-bottom:8px;} + ul,ol{margin-left:24px;margin-right:24px;} + #all_posts{margin-top:24px;text-align:center;} + .subtitle{font-size:small;margin:12px 0px;} + .content p{margin-left:24px;margin-right:24px;} + h1{margin-bottom:12px !important;} + #description{font-size:large;margin-bottom:12px;} + h3{margin-top:42px;margin-bottom:8px;} + h4{margin-left:24px;margin-right:24px;} + img{max-width:100%;} + #twitter{line-height:20px;vertical-align:top;text-align:right;font-style:italic;color:#333;margin-top:24px;font-size:14px;}' > blog.css + fi + + # If there is a style.css from the parent page (i.e. some landing page) + # then use it. This directive is here for compatibility with my own + # home page. Feel free to edit it out, though it doesn't hurt + if [[ -f ../style.css ]] && [[ ! -f main.css ]]; then + ln -s "../style.css" "main.css" + elif [[ ! -f main.css ]]; then + echo 'body{font-family:Georgia,"Times New Roman",Times,serif;margin:0;padding:0;background-color:#F3F3F3;} + #divbodyholder{padding:5px;background-color:#DDD;width:100%;max-width:874px;margin:24px auto;} + #divbody{border:solid 1px #ccc;background-color:#fff;padding:0px 48px 24px 48px;top:0;} + .headerholder{background-color:#f9f9f9;border-top:solid 1px #ccc;border-left:solid 1px #ccc;border-right:solid 1px #ccc;} + .header{width:100%;max-width:800px;margin:0px auto;padding-top:24px;padding-bottom:8px;} + .content{margin-bottom:5%;} + .nomargin{margin:0;} + .description{margin-top:10px;border-top:solid 1px #666;padding:10px 0;} + h3{font-size:20pt;width:100%;font-weight:bold;margin-top:32px;margin-bottom:0;} + .clear{clear:both;} + #footer{padding-top:10px;border-top:solid 1px #666;color:#333333;text-align:center;font-size:small;font-family:"Courier New","Courier",monospace;} + a{text-decoration:none;color:#003366 !important;} + a:visited{text-decoration:none;color:#336699 !important;} + blockquote{background-color:#f9f9f9;border-left:solid 4px #e9e9e9;margin-left:12px;padding:12px 12px 12px 24px;} + blockquote img{margin:12px 0px;} + blockquote iframe{margin:12px 0px;}' > main.css + fi +} + +# Regenerates all the single post entries, keeping the post content but modifying +# the title, html structure, etc +rebuild_all_entries() { + echo -n "Rebuilding all entries " + + for i in ./*.html; do + is_boilerplate_file "$i" && continue; + contentfile=.tmp.$RANDOM + while [[ -f $contentfile ]]; do contentfile=.tmp.$RANDOM; done + + echo -n "." + # Get the title and entry, and rebuild the html structure from scratch (divs, title, description...) + title=$(get_post_title "$i") + + get_html_file_content 'text' 'text' <"$i" >> "$contentfile" + + # Read timestamp from post, if present, and sync file timestamp + timestamp=$(awk '// { print }' "$i" | cut -d '#' -f 2) + [[ -n $timestamp ]] && touch -t "$timestamp" "$i" + # Read timestamp from file in correct format for 'create_html_page' + timestamp=$(LC_ALL=C date -r "$i" +"$date_format_full") + + create_html_page "$contentfile" "$i.rebuilt" no "$title" "$timestamp" "$(get_post_author "$i")" + # keep the original timestamp! + timestamp=$(LC_ALL=C date -r "$i" +"$date_format_timestamp") + mv "$i.rebuilt" "$i" + chmod 644 "$i" + touch -t "$timestamp" "$i" + rm "$contentfile" + done + echo "" +} + +# Displays the help +usage() { + echo "$global_software_name v$global_software_version" + echo "usage: bb command [filename]" + echo "" + echo "Commands:" + echo " post [-html] [filename] insert a new blog post, or the filename of a draft to continue editing it" + echo " it tries to use markdown by default, and falls back to HTML if it's not available." + echo " use '-html' to override it and edit the post as HTML even when markdown is available" + echo " edit [-n|-f] [filename] edit an already published .html or .md file. **NEVER** edit manually a published .html file," + echo " always use this function as it keeps internal data and rebuilds the blog" + echo " use '-n' to give the file a new name, if title was changed" + echo " use '-f' to edit full html file, instead of just text part (also preserves name)" + echo " delete [filename] deletes the post and rebuilds the blog" + echo " rebuild regenerates all the pages and posts, preserving the content of the entries" + echo " reset deletes everything except this script. Use with a lot of caution and back up first!" + echo " list list all posts" + echo " tags [-n] list all tags in alphabetical order" + echo " use '-n' to sort list by number of posts" + echo "" + echo "for more information please see https://tilde.team/wiki/?page=tildeblogs" + echo "source here: https://tildegit.org/team/bashblog" +} + +# Delete all generated content, leaving only this script +reset() { + echo "Are you sure you want to delete all blog entries? Please write \"Yes, I am!\" " + read -r line + if [[ $line == "Yes, I am!" ]]; then + rm .*.html ./*.html ./*.css ./*.rss &> /dev/null + echo + echo "Deleted all posts, stylesheets and feeds." + echo "Kept your old '.backup.tar.gz' just in case, please delete it manually if needed." + else + echo "Phew! You dodged a bullet there. Nothing was modified." + fi +} + +# Detects if GNU date is installed +date_version_detect() { + date --version >/dev/null 2>&1 + if (($? != 0)); then + # date utility is BSD. Test if gdate is installed + if gdate --version >/dev/null 2>&1 ; then + date() { + gdate "$@" + } + else + # BSD date + date() { + if [[ $1 == -r ]]; then + # Fall back to using stat for 'date -r' + format=${3//+/} + stat -f "%Sm" -t "$format" "$2" + elif [[ $2 == --date* ]]; then + # convert between dates using BSD date syntax + command date -j -f "$date_format_full" "${2#--date=}" "$1" + else + # acceptable format for BSD date + command date -j "$@" + fi + } + fi + fi +} + +# Main function +# Encapsulated on its own function for readability purposes +# +# $1 command to run +# $2 file name of a draft to continue editing (optional) +do_main() { + # make sure we're in the right directory + [ $(pwd) != $HOME/public_html/blog ] && + echo "you're not in your blog directory. moving you there now" && cd $HOME/public_html/blog + + # Detect if using BSD date or GNU date + date_version_detect + # Load default configuration, then override settings with the config file + global_variables + [[ -f $global_config ]] && source "$global_config" &> /dev/null + global_variables_check + + # Check for $EDITOR + [[ -z $EDITOR ]] && + echo "Please set your \$EDITOR environment variable. For example, to use nano, add the line 'export EDITOR=nano' to your \$HOME/.bashrc file" && exit + + # Check for validity of argument + [[ $1 != "reset" && $1 != "post" && $1 != "rebuild" && $1 != "list" && $1 != "edit" && $1 != "delete" && $1 != "tags" ]] && + usage && exit + + [[ $1 == list ]] && + list_posts && exit + + [[ $1 == tags ]] && + list_tags "$@" && exit + + if [[ $1 == edit ]]; then + if (($# < 2)) || [[ ! -f ${!#} ]]; then + echo "Please enter a valid .md or .html file to edit" + exit + fi + fi + + # Test for existing html files + if ls ./*.html &> /dev/null; then + # We're going to back up just in case + tar -c -z -f ".backup.tar.gz" -- *.html && + chmod 600 ".backup.tar.gz" + elif [[ $1 == rebuild ]]; then + echo "Can't find any html files, nothing to rebuild" + exit + fi + + # Keep first backup of this day containing yesterday's version of the blog + [[ ! -f .yesterday.tar.gz || $(date -r .yesterday.tar.gz +'%d') != "$(date +'%d')" ]] && + cp .backup.tar.gz .yesterday.tar.gz &> /dev/null + + [[ $1 == reset ]] && + reset && exit + + create_css + create_includes + [[ $1 == post ]] && write_entry "$@" + [[ $1 == rebuild ]] && rebuild_all_entries && rebuild_tags + [[ $1 == delete ]] && rm "$2" &> /dev/null && rebuild_tags + if [[ $1 == edit ]]; then + if [[ $2 == -n ]]; then + edit "$3" + elif [[ $2 == -f ]]; then + edit "$3" full + else + edit "$2" keep + fi + fi + rebuild_index + all_posts + all_tags + make_rss + make_gophermap + make_gemini + delete_includes +} + + +# +# MAIN +# Do not change anything here. If you want to modify the code, edit do_main() +# +do_main "$@" + +# vim: set shiftwidth=4 tabstop=4 expandtab: diff --git a/gophermap b/gophermap new file mode 100755 index 0000000..eab4196 --- /dev/null +++ b/gophermap @@ -0,0 +1,7 @@ +#!/usr/bin/env sh +printf "my bashblog posts\n" +user=$(stat -c '%U' .) +for post in $(ls -t *.md); do +post=$(basename $post) +printf "0$post\t/~$user/blog/$post\ttilde.team\t70\n" +done