]> git.donarmstrong.com Git - lilypond.git/blobdiff - buildscripts/check_texi_refs.py
Merge master into nested-bookparts
[lilypond.git] / buildscripts / check_texi_refs.py
index c7f31fa5ad2c1cfe39a1e7e44a8816ba2d3bdde3..dff7e334f1ccc45d9e2ab039ef8b222ef8443a48 100755 (executable)
@@ -143,8 +143,10 @@ else:
         return None
 
 
-ref_re = re.compile (r'@(ref|ruser|rlearning|rprogram|rglos)\{([^,\\]*?)\}(.)',
-                     re.DOTALL)
+ref_re = re.compile \
+    ('@(ref|ruser|rlearning|rprogram|rglos)(?:\\{(?P<ref>[^,\\\\\\}]+?)|\
+named\\{(?P<refname>[^,\\\\]+?),(?P<display>[^,\\\\\\}]+?))\\}(?P<last>.)',
+     re.DOTALL)
 node_include_re = re.compile (r'(?m)^@(node|include)\s+(.+?)$')
 
 whitespace_re = re.compile (r'\s+')
@@ -327,18 +329,24 @@ in the list)\n")
         t -= 1
     raise InteractionError ("%d retries limit exceeded" % retries)
 
+refs_count = 0
 
 def check_ref (manual, file, m):
-    global fixes_count, bad_refs_count
+    global fixes_count, bad_refs_count, refs_count
+    refs_count += 1
     bad_ref = False
     fixed = True
     type = m.group (1)
-    original_name = m.group (2)
+    original_name = m.group ('ref') or m.group ('refname')
     name = whitespace_re.sub (' ', original_name). strip ()
     newline_indices = manuals[manual]['newline_indices'][file]
     line = which_line (m.start (), newline_indices)
-    linebroken = '\n' in m.group (2)
-    next_char = m.group (3)
+    linebroken = '\n' in original_name
+    original_display_name = m.group ('display')
+    next_char = m.group ('last')
+    if original_display_name: # the xref has an explicit display name
+        display_linebroken = '\n' in original_display_name
+        display_name = whitespace_re.sub (' ', original_display_name). strip ()
     commented_out = is_commented_out \
         (m.start (), m.end (), manuals[manual]['comments_boundaries'][file])
     useful_fix = not outdir in file
@@ -353,11 +361,13 @@ not followed by punctuation\n" % (file, line, name))
     new_name = name
 
     if type != 'ref' and type == manual and not commented_out:
-        bad_ref = True
-        stdout.write ("\n%s: %d: `%s': external %s x-ref should be internal\n"
-                      % (file, line, name, type))
-        if options.auto_fix or yes_prompt ("Fix this?"):
-            type = 'ref'
+        if useful_fix:
+            fixed = False
+            bad_ref = True
+            stdout.write ("\n%s: %d: `%s': external %s x-ref should be internal\n"
+                          % (file, line, name, type))
+            if options.auto_fix or yes_prompt ("Fix this?"):
+                type = 'ref'
 
     if type == 'ref':
         explicit_type = manual
@@ -367,10 +377,10 @@ not followed by punctuation\n" % (file, line, name))
         fixed = False
         stdout.write ('\n')
         if type == 'ref':
-            stdout.write ("%s: %d: `%s': wrong internal x-ref\n"
+            stdout.write ("\e[1;31m%s: %d: `%s': wrong internal x-ref\e[0m\n"
                           % (file, line, name))
         else:
-            stdout.write ("%s: %d: `%s': wrong external `%s' x-ref\n"
+            stdout.write ("\e[1;31m%s: %d: `%s': wrong external `%s' x-ref\e[0m\n"
                           % (file, line, name, type))
         # print context
         stdout.write ('--\n' + manuals[manual]['contents'][file]
@@ -385,11 +395,11 @@ not followed by punctuation\n" % (file, line, name))
             if name in manuals[k]['nodes']:
                 if k == manual:
                     found = ['ref']
-                    stdout.write ("  found as internal x-ref\n")
+                    stdout.write ("\e[1;32m  found as internal x-ref\e[0m\n")
                     break
                 else:
                     found.append (k)
-                    stdout.write ("  found as `%s' x-ref\n" % k)
+                    stdout.write ("\e[1;32m  found as `%s' x-ref\e[0m\n" % k)
 
         if (len (found) == 1
             and (options.auto_fix or yes_prompt ("Fix this x-ref?"))):
@@ -433,7 +443,7 @@ and x-ref by index number or beginning of name:\n", [''.join ([i[0], ' ', i[1]])
                         fixed = True
 
         if not fixed:
-            # all previous automatic fixes attempts failed,
+            # all previous automatic fixing attempts failed,
             # ask user for substring to look in node names
             while True:
                 node_list = search_prompt ()
@@ -466,11 +476,29 @@ please fix the code source instead of generated documentation.\n")
 
     # compute returned string
     if new_name == name:
-        return ('@%s{%s}' % (type, original_name)) + next_char
+        if bad_ref and (options.interactive or options.auto_fix):
+            # only the type of the ref was fixed
+            fixes_count += int (fixed)
+        if original_display_name:
+            return ('@%snamed{%s,%s}' % (type, original_name, original_display_name)) + next_char
+        else:
+            return ('@%s{%s}' % (type, original_name)) + next_char
     else:
-        fixes_count += 1
+        fixes_count += int (fixed)
         (ref, n) = preserve_linebreak (new_name, linebroken)
-        return ('@%s{%s}' % (type, ref)) + next_char + n
+        if original_display_name:
+            if bad_ref:
+                stdout.write ("Current display name is `%s'\n")
+                display_name = raw_input \
+                    ("Enter a new display name or press enter to keep the existing name:\n") \
+                    or display_name
+                (display_name, n) = preserve_linebreak (display_name, display_linebroken)
+            else:
+                display_name = original_display_name
+            return ('@%snamed{%s,%s}' % (type, ref, display_name)) + \
+                next_char + n
+        else:
+            return ('@%s{%s}' % (type, ref)) + next_char + n
 
 
 log.write ("Checking cross-references...\n")
@@ -489,5 +517,5 @@ except InteractionError, instance:
     log.write ("Operation refused by user: %s\nExiting.\n" % instance)
     sys.exit (3)
 
-log.write ("Done: %d bad x-refs found, fixed %d.\n" %
-           (bad_refs_count, fixes_count))
+log.write ("\e[1;36mDone: %d x-refs found, %d bad x-refs found, fixed %d.\e[0m\n" %
+           (refs_count, bad_refs_count, fixes_count))