aboutsummaryrefslogblamecommitdiff
path: root/src/development/md2html.pl
blob: de29e402eb9eaaf1a26bcd51625f6edaea78ed2e (plain) (tree)
1
2
3
4
5
6
7


                   


                           
 

























































































































































                                                                                     
#!/usr/bin/env perl

use v5.34;
use warnings;
use feature 'signatures';
no warnings 'experimental';

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 = '<tr><td>' . $i++ . '</td><td>' . $_ . '</td></tr>';
		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 (/^<pre><code/) {
				say $_;
			}
			push @ret, $_;
		}

		break;
		my $s2 = join("\n", @ret) . "\n";
		# say "antes: $s";
		# say "depois: " . join "\n", @ret;
		open my $fh1, '>', '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;
}