<commit_before>from docutils.nodes import figure, caption, Text, reference, raw, SkipNode, Element, topic
from sphinx.roles import XRefRole
from inlineav import av_dgm 
import json

# Element classes

class page_ref(reference):
    pass

class num_ref(reference):
    pass

figids_1={}  

table_doc={}   

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


# Visit/depart functions

def skip_page_ref(self, node):
    raise SkipNode

def latex_visit_page_ref(self, node):
    self.body.append("\\pageref{%s:%s}" % (node['refdoc'], node['reftarget']))
    raise SkipNode

def latex_visit_num_ref(self, node):
    fields = node['reftarget'].split('#')

    if len(fields) > 1:
        label, target = fields
        ref_link = '%s:%s' % (node['refdoc'], target)
        latex = "\\hyperref[%s]{%s \\ref*{%s}}" % (ref_link, label, ref_link)
        self.body.append(latex)
    else:
        self.body.append('\\ref{%s:%s}' % (node['refdoc'], fields[0]))
        
    raise SkipNode


def html_visit_num_ref(self, node):
    fields = node['reftarget'].split('#')
    json_data = loadTable()  
    if len(fields) > 1:
        label, target = fields
        target_file = ''
        chapter = json_data[target].rsplit('.',1)[0]  
        for name_l, idx in json_data.iteritems():
            if idx == chapter:    
                target_file = name_l   
        if node['refdoc']==target_file:   #target file and curent file are the same  
            link = "%s.html#%s" %(node['refdoc'], target.lower())  
        else:
            link = "%s.html#%s" %(target_file, target.lower())   
        html = '<a href="%s">%s</a>' %(link,  json_data[target][:-1])  
        self.body.append(html)
    else:
        self.body.append('<a href="%s.html">%s</a>' % (node['refdoc'], fields[0]))

    raise SkipNode



def doctree_read(app, doctree):
    # first generate figure numbers for each figure
    env = app.builder.env

    json_data = loadTable()
    i = getattr(env, 'i', 1)
    figids = getattr(env, 'figids', {})
    figid_docname_map = getattr(env, 'figid_docname_map', {})
    module = '' 
    num_module = 0  
    _table = 1
    _exple = 1
    _thrm = 1
    for figure_info in doctree.traverse(Element): # figure):
            if app.builder.name != 'latex' and app.config.number_figures:
                if env.docname != module:
                   i = 1
                   _table = 1
                   _exple = 1
                   _thrm = 1
            if isinstance( figure_info, figure):
                if  env.docname in json_data:
                    module = env.docname  
                    num_module = json_data[env.docname]  
                for cap in figure_info.traverse(caption):
                    cap[0] = Text(" %s %s.%d: %s" % (app.config.figure_caption_prefix, num_module, i, cap[0]))
                    figids_1[env.docname]= '%s.%d' %(num_module, i) 
                for id in figure_info['ids']:
                    figids[id] = i
                    figid_docname_map[id] = env.docname
        
                i += 1
            if isinstance( figure_info, av_dgm ): 
                module = env.docname
                i += 1
            if isinstance( figure_info, topic):
                numbered_label = ''
                if  env.docname in json_data:
                    module = env.docname
                    num_module = json_data[env.docname]
                if len(figure_info.attributes['ids']) > 0:
                    for label in figure_info.attributes['ids']:
                        xrefs = ''
                        if label in json_data:
                            xrefs = json_data[label]
                            if '#' in xrefs:
                                xrefs = xrefs[:-1]
                        numbered_label = ' %s' %xrefs
                        break
                if 'example' in figure_info.children[0].children[0].lower():
                    title = str(figure_info.children[0].children[0]) + numbered_label
                    figure_info.children[0].children[0] = Text(title) 
                    title = 'Example %s.%d:' %(num_module,_exple)
                    figure_info.children[0].children[0] = Text(title)
                    _exple += 1
                if 'table' in figure_info.children[0].children[0].lower():
                    title = str(figure_info.children[0].children[0]) + numbered_label
                    figure_info.children[0].children[0] = Text(title)
                    title = 'Table %s.%d: %s' %(num_module,_table,str(figure_info.children[0].children[0]).split('Table')[1])
                    figure_info.children[0].children[0] = Text(title)
                    _table += 1
                if 'theorem' in figure_info.children[0].children[0].lower():
                    title = str(figure_info.children[0].children[0]) + numbered_label
                    figure_info.children[0].children[0] = Text(title)
                    title = 'Theorem %s.%d: %s' %(num_module,_thrm,str(figure_info.children[0].children[0]).split('Theorem')[1])
                    figure_info.children[0].children[0] = Text(title)
                    _thrm += 1
    env.figid_docname_map = figid_docname_map
    env.i = i
    env.figids = figids


def doctree_resolved(app, doctree, docname):
    # replace numfig nodes with links
    figids = app.builder.env.figids
    if app.builder.name != 'latex':
        for ref_info in doctree.traverse(num_ref):
            if '#' in ref_info['reftarget']:
                label, target = ref_info['reftarget'].split('#')
                labelfmt = label + " %d"
            else:
                labelfmt = '%d'
                target = ref_info['reftarget']<commit_msg>Removed ':' in numerable elements caption/title<commit_after>from docutils.nodes import figure, caption, Text, reference, raw, SkipNode, Element, topic
from sphinx.roles import XRefRole
from inlineav import av_dgm 
import json

# Element classes

class page_ref(reference):
    pass

class num_ref(reference):
    pass

figids_1={}  

table_doc={}   

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


# Visit/depart functions

def skip_page_ref(self, node):
    raise SkipNode

def latex_visit_page_ref(self, node):
    self.body.append("\\pageref{%s:%s}" % (node['refdoc'], node['reftarget']))
    raise SkipNode

def latex_visit_num_ref(self, node):
    fields = node['reftarget'].split('#')

    if len(fields) > 1:
        label, target = fields
        ref_link = '%s:%s' % (node['refdoc'], target)
        latex = "\\hyperref[%s]{%s \\ref*{%s}}" % (ref_link, label, ref_link)
        self.body.append(latex)
    else:
        self.body.append('\\ref{%s:%s}' % (node['refdoc'], fields[0]))
        
    raise SkipNode


def html_visit_num_ref(self, node):
    fields = node['reftarget'].split('#')
    json_data = loadTable()  
    if len(fields) > 1:
        label, target = fields
        target_file = ''
        chapter = json_data[target].rsplit('.',1)[0]  
        for name_l, idx in json_data.iteritems():
            if idx == chapter:    
                target_file = name_l   
        if node['refdoc']==target_file:   #target file and curent file are the same  
            link = "%s.html#%s" %(node['refdoc'], target.lower())  
        else:
            link = "%s.html#%s" %(target_file, target.lower())   
        html = '<a href="%s">%s</a>' %(link,  json_data[target][:-1])  
        self.body.append(html)
    else:
        self.body.append('<a href="%s.html">%s</a>' % (node['refdoc'], fields[0]))

    raise SkipNode



def doctree_read(app, doctree):
    # first generate figure numbers for each figure
    env = app.builder.env

    json_data = loadTable()
    i = getattr(env, 'i', 1)
    figids = getattr(env, 'figids', {})
    figid_docname_map = getattr(env, 'figid_docname_map', {})
    module = '' 
    num_module = 0  
    _table = 1
    _exple = 1
    _thrm = 1
    for figure_info in doctree.traverse(Element): # figure):
            if app.builder.name != 'latex' and app.config.number_figures:
                if env.docname != module:
                   i = 1
                   _table = 1
                   _exple = 1
                   _thrm = 1
            if isinstance( figure_info, figure):
                if  env.docname in json_data:
                    module = env.docname  
                    num_module = json_data[env.docname]  
                for cap in figure_info.traverse(caption):
                    cap[0] = Text(" %s %s.%d: %s" % (app.config.figure_caption_prefix, num_module, i, cap[0]))
                    figids_1[env.docname]= '%s.%d' %(num_module, i) 
                for id in figure_info['ids']:
                    figids[id] = i
                    figid_docname_map[id] = env.docname
        
                i += 1
            if isinstance( figure_info, av_dgm ): 
                module = env.docname
                i += 1
            if isinstance( figure_info, topic):
                numbered_label = ''
                if  env.docname in json_data:
                    module = env.docname
                    num_module = json_data[env.docname]
                if len(figure_info.attributes['ids']) > 0:
                    for label in figure_info.attributes['ids']:
                        xrefs = ''
                        if label in json_data:
                            xrefs = json_data[label]
                            if '#' in xrefs:
                                xrefs = xrefs[:-1]
                        numbered_label = ' %s' %xrefs
                        break
                if 'example' in figure_info.children[0].children[0].lower():
                    title = str(figure_info.children[0].children[0]) + numbered_label
                    figure_info.children[0].children[0] = Text(title) 
                    title = 'Example %s.%d ' %(num_module,_exple)
                    figure_info.children[0].children[0] = Text(title)
                    _exple += 1
                if 'table' in figure_info.children[0].children[0].lower():
                    title = str(figure_info.children[0].children[0]) + numbered_label
                    figure_info.children[0].children[0] = Text(title)
                    title = 'Table %s.%d %s' %(num_module,_table,str(figure_info.children[0].children[0]).split('Table')[1])
                    figure_info.children[0].children[0] = Text(title)
                    _table += 1
                if 'theorem' in figure_info.children[0].children[0].lower():
                    title = str(figure_info.children[0].children[0]) + numbered_label
                    figure_info.children[0].children[0] = Text(title)
                    title = 'Theorem %s.%d %s' %(num_module,_thrm,str(figure_info.children[0].children[0]).split('Theorem')[1])
                    figure_info.children[0].children[0] = Text(title)
                    _thrm += 1
    env.figid_docname_map = figid_docname_map
    env.i = i
    env.figids = figids


def doctree_resolved(app, doctree, docname):
    # replace numfig nodes with links
    figids = app.builder.env.figids
    if app.builder.name != 'latex':
        for ref_info in doctree.traverse(num_ref):
            if '#' in ref_info['reftarget']:
                label, target = ref_info['reftarget'].split('#')
                labelfmt = label + " %d"
            else:
                labelfmt = '%d'
                target = ref_info['reftarget']
{'fraction_non_alphanumeric': 0.12982905982905982, 'fraction_numerical': 0.009743589743589744, 'mean_word_length': 1.801963601532567, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2004448', 'n_tokens_mistral': 3734, 'n_tokens_neox': 3506, 'n_words': 755}