URI: 
       Config.pm - lsg - Lumidify Site Generator
  HTML git clone git://lumidify.org/lsg.git (fast, but not encrypted)
  HTML git clone https://lumidify.org/lsg.git (encrypted, but very slow)
  HTML git clone git://4kcetb7mo7hj6grozzybxtotsub5bempzo4lirzc3437amof2c2impyd.onion/lsg.git (over tor)
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       Config.pm (2804B)
       ---
            1 #!/usr/bin/env perl
            2 
            3 # LSG::Config - configuration for the LSG
            4 # Written by lumidify <nobody@lumidify.org>
            5 #
            6 # To the extent possible under law, the author has dedicated
            7 # all copyright and related and neighboring rights to this
            8 # software to the public domain worldwide. This software is
            9 # distributed without any warranty.
           10 #
           11 # You should have received a copy of the CC0 Public Domain
           12 # Dedication along with this software. If not, see
           13 # <http://creativecommons.org/publicdomain/zero/1.0/>.
           14 
           15 package LSG::Config;
           16 use strict;
           17 use warnings;
           18 use utf8;
           19 use open qw< :encoding(UTF-8) >;
           20 binmode STDIN, ":encoding(UTF-8)";
           21 binmode STDOUT, ":encoding(UTF-8)";
           22 binmode STDERR, ":encoding(UTF-8)";
           23 
           24 use Exporter qw(import);
           25 our @EXPORT_OK = qw($config);
           26 
           27 # Yes, I know this isn't just used for real config
           28 # FIXME: separate fields from config.ini from other parts to avoid conflicts
           29 our $config;
           30 
           31 sub read_modified_dates {
           32         my $path = shift;
           33         my %dates = (pages => {}, templates => {});
           34         if (!-f $path) {
           35                 warn "Unable to open \"$path\". Using empty modified_dates.\n";
           36                 return \%dates;
           37         }
           38         open (my $fh, "<", $path) or die "Unable to open $path: $!\n";
           39         foreach (<$fh>) {
           40                 chomp;
           41                 my @fields = split(" ", $_, 2);
           42                 my $date = $fields[0];
           43                 my $filename = $fields[1];
           44                 if ($filename =~ /\Apages\//) {
           45                         $dates{"pages"}->{substr($filename, 6)} = $date;
           46                 } elsif ($filename =~ /\Atemplates\//) {
           47                         $dates{"templates"}->{substr($filename, 10)} = $date;
           48                 } else {
           49                         die "Invalid file path \"$filename\" in \"$path\".\n";
           50                 }
           51         }
           52         close($fh);
           53         return \%dates;
           54 }
           55 
           56 sub write_modified_dates {
           57         my $path = shift;
           58         open(my $fh, ">", $path) or die "Unable to open \"$path\": $!\n";
           59         foreach my $pageid (keys %{$config->{"metadata"}}) {
           60                 foreach my $lang (keys %{$config->{"metadata"}->{$pageid}->{"modified"}}) {
           61                         print($fh $config->{"metadata"}->{$pageid}->{"modified"}->{$lang} . " pages/$pageid.$lang\n");
           62                 }
           63         }
           64         foreach my $template (keys %{$config->{"templates"}}) {
           65                 print($fh $config->{"templates"}->{$template}->{"modified"} . " templates/$template\n");
           66         }
           67         close($fh);
           68 }
           69 
           70 sub read_config {
           71         my $path = shift;
           72         my %config;
           73         open (my $fh, "<", $path) or die "Unable to open $path: #!\n";
           74         my $section = "";
           75         foreach (<$fh>) {
           76                 chomp;
           77                 if ($_ eq "") {
           78                         $section = "";
           79                         next;
           80                 }
           81                 if (/^\[(.*)\]$/) {
           82                         $section = $1;
           83                         next;
           84                 }
           85                 # FIXME: report errors properly
           86                 my ($key, $value) = split("=", $_, 2);
           87                 if ($value =~ /:/) {
           88                         my @value = split(":", $value);
           89                         $value = \@value;
           90                 }
           91                 if ($section) {
           92                         $config{$section}->{$key} = $value;
           93                 } else {
           94                         $config{$key} = $value;
           95                 }
           96         }
           97         close($fh);
           98         return \%config;
           99 }
          100 
          101 sub init_config {
          102         my ($config_path, $modified_path) = @_;
          103         $config = read_config($config_path);
          104         $config->{"modified_dates"} = read_modified_dates($modified_path);
          105 }
          106 
          107 1;