website-backend/webcc

  1 #!/usr/bin/perl -CSD
  2 use warnings;
  3 use strict;
  4 use v5.22;
  5 
  6 use File::Path qw(make_path);
  7 use DateTime::Format::ISO8601;
  8 use POSIX qw(strftime);
  9 use HTML::Entities;
 10 
 11 my $FH;
 12 
 13 # default definitions
 14 my $domain = "nekodev";
 15 my $date_html = '';
 16 my $date_str = '';
 17 my $title = '';
 18 my $outputFile;
 19 
 20 # mapping from domains (to navigation current page, output dir, etc)
 21 my %id_cur_page = (
 22     'nekodev' => '',
 23     'blog.nekodev' => '',
 24     'code.nekodev' => ''
 25 );
 26 
 27 my %output_dir = (
 28     'nekodev' => '../nekodev',
 29     'blog.nekodev' => '../blog.nekodev',
 30     'code.nekodev' => '../code.nekodev',
 31 
 32     'code-tmp' => '../code-tmp',
 33     '' => '../tmp',
 34     'tmpdir' => '../tmp'
 35 );
 36 
 37 
 38 
 39 # parse file name
 40 my $argvSize = 0+@ARGV;
 41 if ($argvSize > 0) {
 42     warn 'Processing: '.$ARGV[0]."\n";
 43 
 44     die "too many arguments" if $argvSize > 1;
 45 
 46     my ($basename,$fileext) = $ARGV[0] =~ /(.*)\.(.*)/;
 47 
 48     print ' type: '.$fileext."\n";
 49     $domain = "nekodev" if $fileext eq "web";
 50     $domain = "blog.nekodev" if $fileext eq "blog";
 51     $domain = "code.nekodev" if $fileext eq "code";
 52 
 53     # open output file
 54     $outputFile = $output_dir{$domain}.'/'.$basename.'.html';
 55 
 56     warn $outputFile, "\n";
 57 
 58     make_path($outputFile =~ /(.*)\//);
 59     open($FH, '>', $outputFile) or die "cannot open file $outputFile: $!";
 60     select $FH;
 61 }
 62 
 63 
 64 # parse file parameters
 65 $id_cur_page{$domain} = 'id="cur-navpage"';
 66 
 67 while (<<>>) {
 68     chomp;
 69     my $currentTag = $_;
 70     $currentTag eq '[title]' and chomp($title = <<>>);
 71 
 72     if ($currentTag eq '[date]') {
 73         chomp($date_str = <<>>);
 74         my $date = DateTime::Format::ISO8601->parse_datetime( $date_str );
 75 
 76         # make sure we have the right format
 77         $date_str = $date->iso8601;
 78 
 79         # format nice <time> tag with locale dependent text
 80         $date->set_locale('de-DE');
 81         #my $localeDate_str = $date->strftime('%c');
 82         my $localeDate_str = $date->strftime('%A, %d. %B %Y, %H:%M');
 83         $date_html = "<time datetime=$date_str>$localeDate_str</time>";
 84     }
 85 
 86     last if $currentTag eq "[content]";
 87 }
 88 
 89 
 90 # write content
 91 print<<EOF;
 92 <html>
 93  <head>
 94   <meta charset="utf-8"/>
 95   <title>$title - $domain</title>
 96   <link title="dark" rel="stylesheet" type="text/css" href="colors-dark.css"/>
 97   <link title="light" rel="alternate stylesheet" type="text/css" href="colors-light.css"/>
 98   <link rel="stylesheet" type="text/css" href="stylesheet.css"/>
 99   <meta name="viewport" content="width=device-width, initial-scale=1">
100  </head>
101  <body>
102   <nav id="navbar">
103    <ul>
104     <li $id_cur_page{'nekodev'} tabindex="1">
105      <a href="http://nekodev.net/index.html">Home</a>
106      <ul>
107       <li tabindex="2"><a href="http://nekodev.net/projects.html">Projects</a></li>
108       <li tabindex="3"><a href="http://nekodev.net/about.html">About</a></li>
109      </ul>
110     <li $id_cur_page{'blog.nekodev'} tabindex="4">
111      <a href="http://blog.nekodev.net">Blog</a>
112      <ul>
113       <li tabindex="5"><a href="http://blog.nekodev.net/alpha_index.html">Nach Titel</a></li>
114       <li tabindex="6"><a href="http://blog.nekodev.net/date_index.html">Nach Datum</a></li>
115      </ul>
116     </li>
117     </li>
118     <li $id_cur_page{'code.nekodev'} tabindex="7">
119      <a href="http://code.nekodev.net">Code</a>
120      <ul>
121       <li tabindex="8"><a href="test.html">Test</a></li>
122      </ul>
123     </li>
124    </ul>
125   </nav>
126   <h1>$title</h1>
127   $date_html
128 EOF
129 
130 while (<<>>)
131 {
132     # insert code when referenced by code tag
133     if ($_ eq "[code]\n")
134     {
135         chomp ( my $codefile = <<>> );
136         open my $codefh, "<", $output_dir{'code-tmp'}.'/'.$codefile.'.code' or die "cant find code file $codefile: $!";
137         while ( <$codefh> ne "[content]\n" ) {};
138         print while <$codefh>;
139     }
140 
141     my $encoded =  encode_entities($_, '^\n\x20-\x25\x27-\x7e');
142     print $encoded if defined $encoded;
143 }
144 
145 print<<EOF;
146  </body>
147 </html>
148 EOF
149 
150 close $FH if defined $FH;
151 
152 
153 # build blog index
154 if ($domain eq 'blog.nekodev' and defined $outputFile) {
155     open my $blogIndexFile, '>>', $output_dir{'tmpdir'}.'/blog_index';
156     $outputFile =~ s/$output_dir{'blog.nekodev'}\///;
157     print {$blogIndexFile} <<EOF;
158 $outputFile
159 $title
160 $date_str
161 EOF
162 }

Impressum