Commit efb2a942 authored by Richard Berger's avatar Richard Berger
Browse files

Add utility to detect duplicate anchors in documentation files

parent 3a2da51a
Loading
Loading
Loading
Loading
+60 −0
Original line number Diff line number Diff line
#! /usr/bin/env python3
# LAMMPS Documentation Utilities
#
# Scan for duplicate anchor labels in documentation files
#
# Copyright (C) 2017 Richard Berger
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
import re
import sys
import argparse

def main():
    parser = argparse.ArgumentParser(description='scan for duplicate anchor labels in documentation files')
    parser.add_argument('files',  metavar='file', nargs='+', help='one or more files to scan')
    parsed_args = parser.parse_args()

    anchor_pattern = re.compile(r'^:link\(([^,\)]*)\)')
    anchors = {}

    for filename in parsed_args.files:
        with open(filename, 'rt') as f:
            for line_number, line in enumerate(f):
                m = anchor_pattern.match(line)
                if m:
                    label = m.group(1)
                    if label in anchors:
                        anchors[label].append((filename, line_number))
                    else:
                        anchors[label] = [(filename, line_number)]

    count = 0

    for label in sorted(anchors.keys()):
        if len(anchors[label]) > 1:
            print(label)
            count += 1
            for filename, line_number in anchors[label]:
                print(" - %s:%d" % (filename, line_number))


    if count > 0:
        print("Found %d anchor label errors." % count)
        sys.exit(1)
    else:
        print("No anchor label errors.")

if __name__ == "__main__":
    main()
+2 −1
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ setup(name='LAMMPS Documentation Utilities',
      tests_require=['nose'],
      entry_points = {
          "console_scripts": ['txt2html = lammpsdoc.txt2html:main',
                              'txt2rst  = lammpsdoc.txt2rst:main']
                              'txt2rst  = lammpsdoc.txt2rst:main',
                              'doc_anchor_check = lammpsdoc.doc_anchor_check:main ']
      },
)