Page MenuHomeSoftware Heritage
Paste P776

hackmd2rst
ActivePublic

Authored by zack on Sep 23 2020, 11:14 PM.
#!/usr/bin/perl -w
# converts markdown exported from hackmd.io to reStructuredText
# SPDX-FileCopyrightText: © 2020 Stefano Zacchiroli <zack@upsilon.cc>
# SPDX-License-Identifier: GPL-3.0-or-later
use strict;
my $md_fname = $ARGV[0];
die "Usage: hackmd2rst MARKDOWN_FILE\n" unless defined $md_fname;
open(my $rst, "pandoc -f markdown -t rst '$md_fname' |")
or die "pandoc conversion failure";
while (my $line = <$rst>) {
chomp $line;
if ($line =~ /^\s*\[TOC\]\s*$/) {
print(".. toctree::\n");
print(" :maxdepth: 2\n");
} elsif ($line =~ /^\s+\[[^\]]+\]\s+(.*)/) {
print(".. note::\n\n");
print(" $1\n");
} else {
print $line, "\n";
}
}

Event Timeline

zack changed the title of this paste from untitled to hackmd2rst.
zack updated the paste's language from autodetect to perl.