aboutsummaryrefslogtreecommitdiff
path: root/ssg
blob: a81678caea7e1335e646efebf965ee1b824553b3 (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
#!/usr/bin/perl
use strict;
use warnings;

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

# Root in directory.
my $dir;
my $VERSION = "v0.1";

sub help {
    print<<EOF
$0 [switches]
    -v  print version and exits
    -h  print this help and exits
    -d  set the input directory (by default in/)
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 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 = transverse($dir, $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 charset="UTF-8">
<style>
$styles
</style>
</head>
<body>
<div class="header">
<h1 class="headerTitle">
<a href="/">Deprecated</a> <span class="headerSubtitle">Just 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});

    # Get the path.
    my $path = $file->stringify;
    $path =~ s/^$dir//g;
    $path =~ s/\.md$/.html/g;

    # String to show
    my $str = $file->basename;
    $str =~ s/\.md$//g;

    $str = "$str/" if $file->is_dir();

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

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

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

    $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) {
            $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 $mddir = $_[0] || dir($dir);
    remove_tree $mddir;

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

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

        $outfile =~ s/$dir\//out\//g;
        !($outfile =~ m/^$dir/) or die "$outfile =~ m/$dir/";

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

        $outfile =~ s/md$/html/g;
        print STDERR "* writing: $outfile\n";
        my $contents = generate_page $file;

        open FD, ">$outfile";

        print FD $contents;

        close FD;
    }
}

sub main {
    my %opts = (
        'v' => 0,
        'h' => 0,
        'd' => "in"
    ); 
    getopts('vhd:', \%opts);

    if($opts{h})
    {
        help;
        exit 0;
    }
    elsif($opts{v})
    {
        version;
        exit 0;
    }
    elsif($opts{d})
    {
        $dir = dir $opts{d};
    }

    run_on dir($dir);
}

main @ARGV;