From aeb7fe69e8074f26c3e0bdfc59313f944400e6e7 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Mon, 22 Aug 2022 18:17:58 -0300 Subject: src/development/: WIP work on Perl md2html --- src/development/genhtml.sh | 70 ++++++++++++++++++-- src/development/md2html.pl | 162 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 223 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/development/genhtml.sh b/src/development/genhtml.sh index d48df68..631aa13 100755 --- a/src/development/genhtml.sh +++ b/src/development/genhtml.sh @@ -1,8 +1,66 @@ #!/bin/sh set -eu -MD="$1" -HTML="$(dirname "$MD")/$(basename "$MD" .md).html" +usage() { + cat <<-'EOF' | sed "s|@NAME@|$0|g" + Usage: + @NAME@ FILENAME + @NAME@ -h + EOF +} + +help() { + cat <<-'EOF' + + Options: + -h, --help show this message + + FILENAME the input markdown file + EOF +} + +for f in "$@"; do + case "$f" in + --) + break + ;; + --help) + usage + help + exit + ;; + *) + ;; + esac +done + +while getopts 'h' flag; do + case "$flag" in + h) + usage + help + exit + ;; + *) + usage >&2 + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) +F="${1:-}" + +assert_arg() { + if [ -z "$1" ]; then + printf 'Missing %s.\n\n' "$2" >&2 + usage >&2 + exit 2 + fi +} + +assert_arg "$F" 'FILENAME' + +HTML="$(dirname "$F")/$(basename "$F" .md).html" escape() { sed 's/a/a/' @@ -24,7 +82,9 @@ _() { langs='en pt fr eo es' # langs='' . src/development/config.env -. "${MD%.md}.env" +. "${F%.md}.env" + +# src/development/md snippets "$F" cat <<-EOF @@ -91,9 +151,11 @@ awk ' should_print {print} separator == 2 && !should_print { should_print = !should_print } -' "$MD" | +' "$F" | md2html +# src/development/md render + cat <<-EOF diff --git a/src/development/md2html.pl b/src/development/md2html.pl index e2f4cec..de29e40 100755 --- a/src/development/md2html.pl +++ b/src/development/md2html.pl @@ -1,9 +1,161 @@ #!/usr/bin/env perl use v5.34; -use CommonMark; +use warnings; +use feature 'signatures'; +no warnings 'experimental'; -my $doc = CommonMark->parse( - string => "# xablau\n# xupliu", -); -say $doc->render_html; +use Getopt::Std (); +use CommonMark (); +BEGIN { *CM:: = *CommonMark:: } + + + +sub usage($fh) { + print $fh <<~'EOF'; + Usage: + md + md -h + EOF +} + +sub help($fh) { + print $fh <<~'EOF'; + + Options: + -h, --help show this message + EOF +} + +for (@ARGV) { + last if $_ eq '--'; + if ($_ eq '--help') { + usage *STDOUT; + help *STDOUT; + exit; + } +} + +my %opts; +if (!Getopt::Std::getopts('h', \%opts)) { + usage *STDERR; + exit 2; +} + +if ($opts{h}) { + usage *STDOUT; + help *STDOUT; + exit; +} + +sub into_table($s) { + my @lines = (); + my $i = 1; + for (split "\n", $s) { + my $line = '' . $i++ . '' . $_ . ''; + push @lines, $line; + } + return join "\n", @lines; +} + +sub with_file($f, $mode, $block) { + \$block(); + +} + +given (shift @ARGV) { + shift @ARGV if defined($ARGV[0]) and $ARGV[0] eq '--'; + when ('snippets') { + for (@ARGV) { + open my $fh, '<', $_ or die "Can't open $_"; + my $doc = CM->parse(file => $fh); + my $iter = $doc->iterator; + my $i = 0; + while (my ($event_type, $node) = $iter->next) { + next if $node->get_type != CM::NODE_CODE_BLOCK; + my $f = "$_.$i.txt"; + $i++; + say 123; + with_file $f, '>', sub { + say 456; + }; + open my $fh, '>', $f or die "Can't open $f"; + print $fh $node->get_literal; + close $fh or die "Can't close $f"; + } + close $fh or die "Can't close $_"; + } + } + when ('render') { + my $s = CM->parse(file => *STDIN)->render(format => 'html'); + my @ret = (); + my $in_block = 0; + for (split "\n", $s) { + if (/^
', 'antes';
+		print $fh1 $s;
+		close $fh1;
+		open my $fh2, '>', 'depois';
+		print $fh2 $s2;
+		close $fh2;
+		say '=========' if $s eq $s2;
+
+		break;
+		say "$s";
+		my $doc = CM->parse(file => *STDIN);
+		my $iter = $doc->iterator;
+		while (my ($event_type, $node) = $iter->next) {
+			# say $node->get_on_exit;
+			# say $node->get_type_string;
+			# next;
+			# next if $node->get_type == CM::NODE_DOCUMENT;
+			# next unless $event_type == CM::EVENT_ENTER;
+			if ($node->get_type == CM::NODE_CODE_BLOCK) {
+				# say $node->get_on_exit;
+				# say $node->get_on_enter;
+				# say 123;
+				# next;
+				# $node->set_literal(into_table($node->get_literal));
+				# say 123;
+			} else {
+				# say $node->render(format => 'html');
+				# use Data::Dumper;
+				# say Dumper($node->get_type_string);
+			}
+		}
+		 say $doc->render(format => 'html', unsafe => 1);
+	}
+	default {
+		print STDERR "Bad ACTION \"$_\".\n\n";
+		usage *STDERR;
+		exit 2;
+	}
+}
+
+
+
+__END__
+
+# use Data::Dumper;
+# print Dumper(\%opts);
+
+# exit: extra newline in output
+# no warnings ('experimental::signatures', );
+# no warnings ('experimental::when', );
+# $Getopt::Std::STANDARD_HELP_VERSION = 1;
+perlcritic
+
+use builtin qw(true);
+if (true) {
+	say 'true';
+	exit 3;
+}
-- 
cgit v1.2.3