From feef75c5df98eb79f7ca26fca66d577932fd4a34 Mon Sep 17 00:00:00 2001 From: Remy Oukaour Date: Wed, 13 Dec 2017 16:46:31 -0500 Subject: Document more bugs and glitches Add a toc.py script to auto-generate tables of contents in Markdown files --- tools/toc.py | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 tools/toc.py (limited to 'tools') diff --git a/tools/toc.py b/tools/toc.py new file mode 100644 index 000000000..1d7a58cec --- /dev/null +++ b/tools/toc.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +""" +Usage: python3 toc.py [-n] files.md... +Replace a "## TOC" heading in a Markdown file with a table of contents, +generated from the other headings in the file. Supports multiple files. +Use "-n" for numbered list items. +Headings must start with "##" signs to be detected. +""" + +import sys +import re +from collections import namedtuple + +toc_name = 'Contents' +valid_toc_headings = {'## TOC', '##TOC'} + +TocItem = namedtuple('TocItem', ['name', 'anchor', 'level']) +punctuation_regexp = re.compile(r'[^\w\- ]+') + +def name_to_anchor(name): + # GitHub's algorithm for generating anchors from headings + # https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/toc_filter.rb + anchor = name.strip().lower() # lowercase + anchor = re.sub(punctuation_regexp, '', anchor) # remove punctuation + anchor = anchor.replace(' ', '-') # replace spaces with dash + return anchor + +def get_toc_index(lines): + toc_index = None + for i, line in enumerate(lines): + if line.rstrip() in valid_toc_headings: + toc_index = i + break + return toc_index + +def get_toc_items(lines, toc_index): + for i, line in enumerate(lines): + if i <= toc_index: + continue + if line.startswith('##'): + name = line.lstrip('#') + level = len(line) - len(name) - len('##') + name = name.strip() + anchor = name_to_anchor(name) + yield TocItem(name, anchor, level) + +def toc_string(toc_items, numeric): + lines = ['## %s' % toc_name, ''] + for name, anchor, level in toc_items: + padding = ' ' * level + line = '%s- [%s](#%s)' % (padding, name, anchor) + lines.append(line) + return '\n'.join(lines) + '\n' + +def add_toc(filename, numeric): + with open(filename, 'r', encoding='utf-8') as f: + lines = f.readlines() + toc_index = get_toc_index(lines) + if toc_index is None: + return None # no TOC heading + toc_items = list(get_toc_items(lines, toc_index)) + if not toc_items: + return False # no content headings + with open(filename, 'w', encoding='utf-8') as f: + for i, line in enumerate(lines): + if i == toc_index: + f.write(toc_string(toc_items, numeric)) + else: + f.write(line) + return True # OK + +def main(): + if len(sys.argv) < 2: + print('*** ERROR: Not enough arguments') + print(__doc__) + exit(1) + del sys.argv[0] + numeric = False + if sys.argv[0] == '-n': + numeric = True + del sys.argv[0] + if not sys.argv: + print('*** ERROR: No filenames specified') + exit(1) + for filename in sys.argv: + print(filename) + result = add_toc(filename, numeric) + if result is None: + print('*** WARNING: No "## TOC" heading found') + elif result is False: + print('*** WARNING: No content headings found') + else: + print('OK') + +if __name__ == '__main__': + main() -- cgit v1.2.3 From 3e5b6322e1167758fedaec5b3930eadaeaa8ec33 Mon Sep 17 00:00:00 2001 From: Remy Oukaour Date: Wed, 13 Dec 2017 16:57:29 -0500 Subject: Don't bother supporting numbered lists --- tools/toc.py | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) (limited to 'tools') diff --git a/tools/toc.py b/tools/toc.py index 1d7a58cec..9bdc8cca6 100644 --- a/tools/toc.py +++ b/tools/toc.py @@ -5,7 +5,6 @@ Usage: python3 toc.py [-n] files.md... Replace a "## TOC" heading in a Markdown file with a table of contents, generated from the other headings in the file. Supports multiple files. -Use "-n" for numbered list items. Headings must start with "##" signs to be detected. """ @@ -46,7 +45,7 @@ def get_toc_items(lines, toc_index): anchor = name_to_anchor(name) yield TocItem(name, anchor, level) -def toc_string(toc_items, numeric): +def toc_string(toc_items): lines = ['## %s' % toc_name, ''] for name, anchor, level in toc_items: padding = ' ' * level @@ -54,7 +53,7 @@ def toc_string(toc_items, numeric): lines.append(line) return '\n'.join(lines) + '\n' -def add_toc(filename, numeric): +def add_toc(filename): with open(filename, 'r', encoding='utf-8') as f: lines = f.readlines() toc_index = get_toc_index(lines) @@ -66,27 +65,19 @@ def add_toc(filename, numeric): with open(filename, 'w', encoding='utf-8') as f: for i, line in enumerate(lines): if i == toc_index: - f.write(toc_string(toc_items, numeric)) + f.write(toc_string(toc_items)) else: f.write(line) return True # OK def main(): if len(sys.argv) < 2: - print('*** ERROR: Not enough arguments') - print(__doc__) - exit(1) - del sys.argv[0] - numeric = False - if sys.argv[0] == '-n': - numeric = True - del sys.argv[0] - if not sys.argv: print('*** ERROR: No filenames specified') + print(__doc__) exit(1) - for filename in sys.argv: + for filename in sys.argv[1:]: print(filename) - result = add_toc(filename, numeric) + result = add_toc(filename) if result is None: print('*** WARNING: No "## TOC" heading found') elif result is False: -- cgit v1.2.3 From 01bd8ac94cc11c086167a2a2d8e7b335f033deca Mon Sep 17 00:00:00 2001 From: Remy Oukaour Date: Fri, 15 Dec 2017 20:12:00 -0500 Subject: Move old baserom.gbc 'compare' scripts to tools/ --- tools/compare.sh | 11 +++++++++++ tools/compare2.sh | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100755 tools/compare.sh create mode 100755 tools/compare2.sh (limited to 'tools') diff --git a/tools/compare.sh b/tools/compare.sh new file mode 100755 index 000000000..d013a88ab --- /dev/null +++ b/tools/compare.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# Compares baserom.gbc and pokecrystal.gbc + +# create baserom.txt if necessary +if [ ! -f baserom.txt ]; then + hexdump -C baserom.gbc > baserom.txt +fi + +hexdump -C pokecrystal.gbc > pokecrystal.txt + +diff -u baserom.txt pokecrystal.txt | less diff --git a/tools/compare2.sh b/tools/compare2.sh new file mode 100755 index 000000000..64695229e --- /dev/null +++ b/tools/compare2.sh @@ -0,0 +1,51 @@ +#!/bin/sh +# Compares baserom.gbc and pokecrystal.gbc + +# create baserom.txt if necessary +crystal_md5=9f2922b235a5eeb78d65594e82ef5dde +if [ ! -f baserom.gbc ]; then + echo "FATAL: Baserom not found" + exit 1 +fi + +if [ $1 ]; then + if [ $1 == "-v" ]; then + verbose=1 + else + verbose = 0 + fi +else + verbose=0 +fi + +base_md5=`md5sum baserom.gbc | cut -d' ' -f1` +if [ $verbose == 1 ]; then + echo "baserom.gbc: $base_md5" +fi +if [ $base_md5 != $crystal_md5 ]; then + echo "FATAL: Baserom is incorrect" + exit 1 +fi + +built_md5=`md5sum pokecrystal.gbc | cut -d' ' -f1` +if [ $verbose == 1 ]; then + echo "pokecrystal.gbc: $built_md5" +fi +if [ $built_md5 != $crystal_md5 ] +then + if [ $verbose == 1 ]; then + echo "Checksums do not match, here's where the ROMs differ..." + fi + if [ ! -f baserom.txt ]; then + hexdump -C baserom.gbc > baserom.txt + fi + + hexdump -C pokecrystal.gbc > pokecrystal.txt + + diff -u baserom.txt pokecrystal.txt | less +else + if [ $verbose == 1 ]; then + echo "Checksums match! :D" + fi +fi + -- cgit v1.2.3