<commit_before>from sphinx.roles import XRefRole 
from docutils.nodes import reference, SkipNode
import json 

def loadTable():
   try:
      table=open('table.json')
      data = json.load(table)
      table.close()
      return data
   except IOError:
      print 'ERROR: No table.json file.'

class chap_ref(reference):
    pass

class chap_num(reference):
    pass

def html_visit_chap_ref(self, node):
    target = node['reftarget']   #.split('#')
    json_data = loadTable()
    caption = ''
    target_file = ''
    chapter = ''
    if target in json_data:
        chapter = str(json_data[json_data[target]]).split('.')[0]
        target_file = json_data[target]
        if node['refdoc']==target_file:   #target file and curent file are the same  
            link = "#" 
        else:
            link = "%s.html" %(target_file)
        if isinstance( node, chap_ref):
            caption = target
        if isinstance( node, chap_num):
            caption = chapter 
        html = '<a href="%s">%s</a>' %(link,  caption)
        self.body.append(html)
    else:
        print 'WARNING: Missing object reference %s' %target

    raise SkipNode


def setup(app):
    app.add_node(chap_ref, html=(html_visit_chap_ref, None))
    app.add_node(chap_num, html=(html_visit_chap_ref, None))
    app.add_role('chap', XRefRole(nodeclass=chap_ref))
    app.add_role('numchap', XRefRole(nodeclass=chap_num))<commit_msg>Writes directive errors to stderr<commit_after>from sphinx.roles import XRefRole 
from docutils.nodes import reference, SkipNode
import json 


# Prints the given string to standard error
def print_err(err_msg):
  sys.stderr.write('%s\n' % err_msg)


def loadTable():
   try:
      table=open('table.json')
      data = json.load(table)
      table.close()
      return data
   except IOError:
      print_err( 'ERROR: No table.json file.')



class chap_ref(reference):
    pass

class chap_num(reference):
    pass

def html_visit_chap_ref(self, node):
    target = node['reftarget']   #.split('#')
    json_data = loadTable()
    caption = ''
    target_file = ''
    chapter = ''
    if target in json_data:
        chapter = str(json_data[json_data[target]]).split('.')[0]
        target_file = json_data[target]
        if node['refdoc']==target_file:   #target file and curent file are the same  
            link = "#" 
        else:
            link = "%s.html" %(target_file)
        if isinstance( node, chap_ref):
            caption = target
        if isinstance( node, chap_num):
            caption = chapter 
        html = '<a href="%s">%s</a>' %(link,  caption)
        self.body.append(html)
    else:
        print_err( 'WARNING: Missing object reference %s' %target)

    raise SkipNode


def setup(app):
    app.add_node(chap_ref, html=(html_visit_chap_ref, None))
    app.add_node(chap_num, html=(html_visit_chap_ref, None))
    app.add_role('chap', XRefRole(nodeclass=chap_ref))
    app.add_role('numchap', XRefRole(nodeclass=chap_num))
{'fraction_non_alphanumeric': 0.12449664429530201, 'fraction_numerical': 0.0006711409395973154, 'mean_word_length': 2.8714285714285714, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2127915', 'n_tokens_mistral': 985, 'n_tokens_neox': 955, 'n_words': 228}