From efb2a942e046f26fb10ab0b1a8c1be98d14f99e3 Mon Sep 17 00:00:00 2001
From: Richard Berger <richard.berger@temple.edu>
Date: Thu, 16 Mar 2017 21:51:14 -0400
Subject: [PATCH] Add utility to detect duplicate anchors in documentation
 files

---
 .../converters/lammpsdoc/doc_anchor_check.py  | 60 +++++++++++++++++++
 doc/utils/converters/setup.py                 |  3 +-
 2 files changed, 62 insertions(+), 1 deletion(-)
 create mode 100755 doc/utils/converters/lammpsdoc/doc_anchor_check.py

diff --git a/doc/utils/converters/lammpsdoc/doc_anchor_check.py b/doc/utils/converters/lammpsdoc/doc_anchor_check.py
new file mode 100755
index 0000000000..2eeed9077b
--- /dev/null
+++ b/doc/utils/converters/lammpsdoc/doc_anchor_check.py
@@ -0,0 +1,60 @@
+#! /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()
diff --git a/doc/utils/converters/setup.py b/doc/utils/converters/setup.py
index b8a6a1a78f..f4656a7f69 100644
--- a/doc/utils/converters/setup.py
+++ b/doc/utils/converters/setup.py
@@ -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 ']
       },
 )
-- 
GitLab