aboutsummaryrefslogtreecommitdiff
path: root/ssg
blob: e372d84b7791aa5c43eeef27295e713d4a7cd744 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/perl
use v5.38;
use strict;
use warnings;
use experimental 'signatures';

use local::lib;

use File::Path qw(make_path remove_tree);
use Path::Class;
use Getopt::Long;

# Root in directory.
our $indir;
our $outdir;
my $VERSION = "v0.1";

my @exlude_files = (
    qr/log\/\w+\/*/
);

my $with_sidebar = 1;

sub help {
    print<<EOF
$0 [switches] input-directory
    -v  print version and exits
    -h  print this help and exits
    -o  set the output directory (by default site.static/)
EOF
}

sub version {
    print <<EOF
$0 $VERSION
EOF
}

sub styles {
    # Unset $/, the Input Record Separator, to make <> give you the whole file at once.
    local $/=undef;
    open my $FILE, '<', "styles.css" or die "Cannot open file $!";
    my $styles = <$FILE>;
    close $FILE;

    return $styles;
}

sub markdown {
    my ($file) = @_;
    my $filename = "$file";
    $filename =~ s/\.html$/.md/g;
    return `markdown "$filename"`;
}

sub generate_page {
    my $file = $_[0];
    my $sidebar = "";
    if($with_sidebar) {
        $sidebar = transverse($indir, $file);
    }
    my $styles = styles;
    my $body = markdown($file);
    my $page = <<EOF
<!doctype html>
<html>
<head>
<title>Deprecated</title>
<link rel="icon" href="/favicon.png" type="image/png"/>
<meta name="viewport" content="width=device-width, initial-scale=0.9" />
<meta charset="UTF-8">
<style>
$styles
</style>
</head>
<body>
<div class="header">
<h1 class="headerTitle">
<a href="/">Deprecated</a> <span class="headerSubtitle">Coding for fun</span>
</h1>
</div>

<div id="side-bar">
$sidebar
</div>

<div id="main">
$body
</div>


<div id="footer">
<div class="right">Powered by <a href="http://git.deprecated.org/ssg.git/about">SSG</a></div>
</div>
</body>
</html>
EOF
;
    return $page;
}

sub generate_link {
    my ($file, $options) = @_;
    $options->{hl} = 0 if !exists($options->{hl});

    # Exclude links
    if ($file) {
        if (grep /^$file$/, @exlude_files) {
            say "excluding $file";
            return "";
        }
        my $path = $file->stringify;
        my $str = $file->basename;

        if($path =~ /index\.md$/) {
            return "";
        }

        # remove the input directory.
        $path =~ s/^$indir//g;
        if($file->is_dir()) {
            $str = "$str/" if $file->is_dir();
        } else {
            $path =~ s/\.md$/.html/g;
            $str =~ s/\.md$//g;
        }

        # Higlight?
        my $hl = "";
        if($options->{hl} eq 1) {
            $hl = "class='hl'";
        }

        return "<li><a $hl href='$path'>$str</a></li>";
    }
}

sub transverse {
    my ($dirname, $file_needed) = @_;
    my $retval = "";
    my $directory = dir($dirname);

    $retval = "<ul>";
    while(my $file = $directory->next) {
        my $filename = $file->stringify;

        # Some conditions
        next if $filename =~ /(\.|\.\.)$/;
        next if $filename eq $directory;

        if($file_needed eq $file or (
                $file_needed->dir eq $filename and
                $filename eq "index.md")) {
            $retval = "$retval" . generate_link($file, { hl => 1 });
        } else {
            $retval = "$retval" . generate_link($file);
        }

        if($file->is_dir()) {
            my $d = $file_needed->dir->stringify;
            if($file =~ /$d/ and !($d eq $directory)) {
                $retval = "$retval" . transverse($file, $file_needed);
            }
        }
    }
    $retval = "$retval</ul>";

    return $retval;
}

sub run_on {
    my $dirname = $_[0] or die "Cannot stat input directory";
    my $mddir = dir($dirname);

    while(my $file = $mddir->next) {
        my $outfile = $file->stringify;

        # Some conditions:
        # 1) next if file is . or ..
        # 2) next if outfile = directory with markdown
        # 3) die if the output file is in the input directory.
        next if $outfile eq $mddir;
        next if $outfile =~ m/(\.|\.\.)($|\/)/;
        $outfile =~ s/^$indir/$outdir\//g;
        $outfile !~ m/^$indir/ or die "trying to write in the same directory $outfile";

        if($file->is_dir()) {
            # make a directory
            make_path $outfile;
            run_on($file);
            next;
        }

        $outfile =~ s/md$/html/g;
        # This file it's just for pretty print
        print STDERR "* writing: " . file($outfile) . "\n";
        my $contents = generate_page $file;

        open my $FD, '>', "$outfile" or die " [ERROR] cannot open > $outfile: $!";;

        print $FD $contents or die " [ERROR] Cannot write $outfile: $!";

        close $FD or die " [ERROR] Cannot close $outfile: $!";
    }
}

sub main {
    my $help;
    my $version;
    my $output_path = 'site.static/';
    my $sidebar = 1;
    GetOptions(help => \$help,
               version => \$version,
               "output-path=s", \$output_path,
               "sidebar" => \$sidebar) or die "Error in command line arguments\n";
    $indir = shift @ARGV;

    if($help) {
        help;
        exit 0;
    } elsif ($version) {
        version();
        exit 0;
    }

    if (-d $output_path) {
        print STDERR "Removing $output_path\n";
        remove_tree($output_path);
    }

    $outdir = $output_path;
    mkdir $output_path;
    run_on $indir;
}
main;