Add script to convert markdown files to texi

We can easily convert markdown files with pandoc:
    pandoc --from markdown --to texinfo index.md -o index.texi
    rm -f index.md

And since our modified version of Untitled has support for texi, it
ends up generating html files.

But then we end up with a problem in the title. For instance
index.texi generates a web page with "What is this?
{#What-is-this_003f}" instead of the expected "Free your BIOS today!"
title.

This can be fixed manualy by doing the following change to index.texi:
     @node Top
    -@top Free your BIOS today!
    +@top Top
    +
    +@node
    +@subsection Free your BIOS today!

But there are more than 80 markdown files, so that would require to do
that 80 times manually.

So this quick and dirty script can be used to do that automatically
instead.

Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
This commit is contained in:
Denis 'GNUtoo' Carikli 2023-08-16 23:50:28 +02:00
parent b7030e428d
commit 92a56bf0ee
Signed by: GNUtoo
GPG Key ID: 5F5DFCC14177E263
1 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,72 @@
#!/usr/bin/env python3
#
# Copyright (C) 2023 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import enum
import os
import re
import sh
import sys
def usage(progname):
print ("Usage: {} <path/to/file.md>".format(progname))
class State(enum.Enum):
NOT_FOUND = 0
FOUND_TOP_NODE = 1
def fixup_texinfo(input_path):
output_path = str(sh.mktemp()).replace(os.linesep, '')
input_texi_file = open(input_path, 'r')
output_texi_file = open(output_path, 'w')
state = State.NOT_FOUND
for line in input_texi_file:
line = line.replace(os.linesep, '')
if line == '@node Top':
state = State.FOUND_TOP_NODE
output_texi_file.write(line + os.linesep)
elif state == State.FOUND_TOP_NODE and line.startswith("@top "):
output_texi_file.write("@top top" + os.linesep)
output_texi_file.write(os.linesep)
output_texi_file.write("@subsection {}".format(
line.split(" ")[1]) + os.linesep)
else:
output_texi_file.write(line + os.linesep)
output_texi_file.close()
input_texi_file.close()
sh.mv ("-f", output_path, input_path)
def convert(input_path, output_path):
sh.pandoc("--from", "markdown",
"--to", "texinfo",
input_path,
"-o", output_path)
sh.rm("-f", input_path)
if len(sys.argv) != 2:
usage(sys.argv[0])
sys.exit(64) # EX_USAGE in sysexits.h
input_path = sys.argv[1]
output_path = re.sub('\.md$', '', input_path) + ".texi"
convert(input_path, output_path)
fixup_texinfo(output_path)