|
| 1 | +#!/usr/bin/perl |
| 2 | +use strict; |
| 3 | +use warnings; |
| 4 | +use IPC::System::Simple qw(system); |
| 5 | +use Markdent::Simple::Document; |
| 6 | +use Markdent::Simple::Fragment; |
| 7 | +use File::Map qw(map_file); |
| 8 | +use File::Path qw(make_path); |
| 9 | +use Cwd 'abs_path'; |
| 10 | + |
| 11 | +use constant false => 0; |
| 12 | +use constant true => 1; |
| 13 | + |
| 14 | +main(); |
| 15 | + |
| 16 | +sub main{ |
| 17 | + # turn off buffering of output |
| 18 | + $| = 1; |
| 19 | + print "Generating utplsql Documenation \n"; |
| 20 | + print "results will be found in ..\\docs folder \n"; |
| 21 | + # change current working directory to location of script. |
| 22 | + # this keeps realative paths later in script easier to maintain. |
| 23 | + # Source: https://sysengineers.wordpress.com/2009/12/04/changing-working-directory-to-script-location-in-perl/ |
| 24 | + my $path = abs_path($0); |
| 25 | + $path =~ s/builddocs.pl//gi; |
| 26 | + chdir ($path); |
| 27 | + |
| 28 | + # Create output directories if the don't exists |
| 29 | + make_path("../docs"); |
| 30 | + |
| 31 | + # Execute NaturalDocs |
| 32 | + # -i InputDirectory |
| 33 | + # -o FramedHTML or HTML OutputDirectory (Using HTML so we can directly link to documenation pages easier.) |
| 34 | + # -p ProectDirectory |
| 35 | + # -xi ExcludeDirFromInput |
| 36 | + # -r RebuildAllFiles |
| 37 | + my @nd_args = qw( |
| 38 | + -i ../ |
| 39 | + -o HTML ../docs/ |
| 40 | + -p ./project |
| 41 | + -xi ../.travis |
| 42 | + -xi ../examples |
| 43 | + -xi ../lib |
| 44 | + -xi ../tools |
| 45 | + -xi ../build |
| 46 | + -r |
| 47 | + ); |
| 48 | + system($^X, "../tools/ndocs/NaturalDocs", @nd_args); |
| 49 | + |
| 50 | + #convert markdown files to html |
| 51 | + #convert_markdown_file("../CONTRIBUTING.md","../docs/files/docsource/topics/contributing-txt.html","How to contribute to utPLSQL"); |
| 52 | + convert_markdown_and_insert("../CONTRIBUTING.md","../docs/files/docsource/topics/contributing-txt.html"); |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + #Checking for current/past errors with NaturalDocs |
| 57 | + check_project_file_for_error("project/Menu.txt"); |
| 58 | + check_project_file_for_error("project/Languages.txt"); |
| 59 | + check_project_file_for_error("project/Topics.txt"); |
| 60 | + if (-e "project/LastCrash.txt") { |
| 61 | + die "project/LastCrash.txt found, indicating a prior NaturalDocs problem that should be fixed. Note: File must be manually removed to continue"; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +sub check_project_file_for_error { |
| 66 | + my $file_to_check = shift; |
| 67 | + map_file(my $menufile,$file_to_check); |
| 68 | + if ($menufile =~ /ERROR/) { die "ERROR found $file_to_check" } |
| 69 | +} |
| 70 | + |
| 71 | +sub convert_markdown { |
| 72 | + my $markdowntext = shift; |
| 73 | + my $fragment = shift; |
| 74 | + my $title = shift; |
| 75 | + |
| 76 | + # convert markdown to html |
| 77 | + my $htmltext; |
| 78 | + if ($fragment = true) { |
| 79 | + my $parser = Markdent::Simple::Fragment->new(); |
| 80 | + $htmltext = $parser->markdown_to_html( |
| 81 | + markdown => $markdowntext); |
| 82 | + } else { |
| 83 | + my $parser = Markdent::Simple::Document->new(); |
| 84 | + $htmltext = $parser->markdown_to_html( |
| 85 | + markdown => $markdowntext, |
| 86 | + title => $title); |
| 87 | + } |
| 88 | + |
| 89 | + #return |
| 90 | + $htmltext; |
| 91 | +} |
| 92 | + |
| 93 | +sub convert_markdown_and_insert { |
| 94 | + my $source = shift; |
| 95 | + my $dest = shift; |
| 96 | + |
| 97 | + # memory map input file |
| 98 | + map_file(my $markdown, $source); |
| 99 | + |
| 100 | + # convert markdown to html |
| 101 | + my $html = convert_markdown($markdown,true); |
| 102 | + |
| 103 | + # read output file |
| 104 | + open FILE, "<$dest" or die "Couldn't open file: $!"; |
| 105 | + binmode FILE; |
| 106 | + my $output = do { local $/; <FILE> }; |
| 107 | + close FILE; |
| 108 | + |
| 109 | + #search and replace |
| 110 | + $output =~ s/$source/$html/g; |
| 111 | + |
| 112 | + # write output file |
| 113 | + open FILE, ">$dest" or die "Couldn't open file: $!"; |
| 114 | + binmode FILE; |
| 115 | + print FILE $output; |
| 116 | + close FILE; |
| 117 | + |
| 118 | + print "Inserted $source as html into $dest \n" |
| 119 | +} |
| 120 | + |
| 121 | +sub convert_markdown_file { |
| 122 | + my $source = shift; |
| 123 | + my $dest = shift; |
| 124 | + my $title = shift; |
| 125 | + |
| 126 | + # memory map input file |
| 127 | + map_file(my $markdown, $source); |
| 128 | + |
| 129 | + # convert markdown to html |
| 130 | + my $html = convert_markdown($markdown,false,$title); |
| 131 | + |
| 132 | + # check output file exists, and deletes. |
| 133 | + if (-f $dest) { |
| 134 | + unlink $dest or die "Cannot delete $dest: $!"; |
| 135 | + } |
| 136 | + |
| 137 | + # write markdown to file |
| 138 | + my $OUTFILE; |
| 139 | + open $OUTFILE, '>>', $dest or die "Cannot open $dest"; |
| 140 | + print { $OUTFILE } $html or die "Cannot write to $dest"; |
| 141 | + close $OUTFILE or die "Cannot close $dest"; |
| 142 | + |
| 143 | + print "Converted $source to $dest \n" |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
0 commit comments