-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_export.pl
executable file
·118 lines (95 loc) · 3.5 KB
/
script_export.pl
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
#!/usr/bin/perl
use strict;
use warnings;
# for filepath computing
use File::Basename;
# to decode HTML entities (mandatory for highlighting, see below)
use HTML::Entities;
# to communicate both read and write with source-highlighting
use IPC::Open3;
# useful files and directories
my $DIR_SOURCE = "./source";
my $DIR_EXPORT = "./export";
my $FILE_TOP_HTML = "$DIR_SOURCE/top.html";
my $FILE_BOT_HTML = "$DIR_SOURCE/bot.html";
my $DIR_SOURCE_DATA = "$DIR_SOURCE/data";
my $DIR_EXPORT_DATA = "$DIR_EXPORT/data";
# some checks on source dirs
checkfile("edr",$DIR_SOURCE);
checkfile("efr",$FILE_TOP_HTML);
checkfile("efr",$FILE_BOT_HTML);
checkfile("edr",$DIR_SOURCE_DATA);
# create export dir if not existing
system ("mkdir -vp $DIR_EXPORT");
checkfile("edw",$DIR_EXPORT);
# delete symlink if existing and create new one
if (-e $DIR_EXPORT_DATA) { system("unlink $DIR_EXPORT_DATA"); }
system ("ln -vs ../$DIR_SOURCE_DATA $DIR_EXPORT_DATA");
checkfile("elw",$DIR_EXPORT_DATA);
# get the markdown pages filepaths
my @all_md_filepaths = split (/\n/, `ls -1 $DIR_SOURCE/*.md`);
# export each markdown to html export file
foreach my $md_filepath (@all_md_filepaths) {
$md_filepath =~ s/\s//go ;
checkfile("er",$md_filepath);
# convert md to html
my $html_content = `cmark --unsafe --to html $md_filepath` ;
# highlighting code nodes
$html_content =~
s/<pre><code class="([\w-]*)">([\s\S]*?)<\/code><\/pre>/replace($1,$2)/geo ;
# preparing top content
my ($title) = $html_content =~ m/<h1>(.*?)<\/h1>/o ;
my $top_html = `cat $FILE_TOP_HTML` ;
$top_html =~ s/<title><\/title>/"<title>$title<\/title>"/eo ;
# computing filepath
my $html_basename = basename($md_filepath);
$html_basename =~ s/\.md$/\.html/o;
my $html_filepath = "$DIR_EXPORT/$html_basename" ;
# write to filepath
open (my $fd_html, '>', $html_filepath)
or die ("Cannot open $html_filepath");
print $fd_html ($top_html, $html_content);
close($fd_html);
# adding bot content
system("cat $FILE_BOT_HTML >> $html_filepath");
print ("Parsed $md_filepath -> $html_filepath\n") ;
}
# function to replace code node with highlighted code node
sub replace {
my ($language_name,$content) = @_ ;
# extract name of language
my $name = $language_name ;
$name =~ s/language-//o ;
# decode HTML entities, because cmark already encode them
# source-highlight is not going to treat '&' as '&'
decode_entities($content);
# open command source-highlight
my $cmd = "source-highlight -s $name -f html-css --failsafe" ;
open3(my $fdw_cmd, my $fdr_cmd, ">&STDERR", $cmd)
or die("Could not call source-highlight correctly");
# print content to command
print $fdw_cmd ($content);
close($fdw_cmd);
# read result content from command
my $code = do { local $/ = undef ; <$fdr_cmd>; };
close($fdr_cmd);
# replacing bad html wrapper with correct html5 one
$code =~ s/<tt>/"<code class=\"$language_name\" >"/eo ;
$code =~ s/<\/tt>/<\/code>/o ;
return $code;
}
# function to check files
sub checkfile { my ($checks, $filepath) = @_ ;
if ($checks =~ m/d/o) {
(-d $filepath) or die ("Path $filepath is not a directory"); }
if ($checks =~ m/e/o) {
(-e $filepath) or die ("Path $filepath does not exist"); }
if ($checks =~ m/f/o) {
(-f $filepath) or die ("Path $filepath is not a file"); }
if ($checks =~ m/l/o) {
(-l $filepath) or die ("Path $filepath is not a symbolic link"); }
if ($checks =~ m/r/o) {
(-r $filepath) or die("Path $filepath can not be read"); }
if ($checks =~ m/w/o) {
(-w $filepath) or die("Path $filepath can not be written"); }
}