# Path: tilde/core/common.py
# def html_formula(string):
#     sub, html_formula = False, ''
#     for i in string:
#         if i.isdigit() or i == '.' or i == '-':
#             if not sub:
#                 html_formula += '<sub>'
#                 sub = True
#         else:
#             if sub and i != 'd':
#                 html_formula += '</sub>'
#                 sub = False
#         html_formula += i
#     if sub: html_formula += '</sub>'
#     return html_formula
# 
# def num2name(target, rules, mapping):
#     return mapping[rules['cid']][int(target)] if rules['enumerated'] else target


import os
from tilde.core.common import html_formula, num2name




def wrap_cell(entity, json_obj, mapping, table_view=False):
    '''
    Cell wrappers
    for customizing the GUI data table

    TODO : must coincide with hierarchy!
    TODO : simplify this!
    '''
    html_class = '' # for GUI javascript
    out = ''

    #if 'cell_wrapper' in entity: # TODO : this bound type was defined by apps only
    #    out = entity['cell_wrapper'](json_obj)
    #else:

    if entity['multiple']:
        out = ", ".join(  map(lambda x: num2name(x, entity, mapping), json_obj.get(entity['source'], ['&mdash;']))  )

    elif entity['is_chem_formula']:

# Path: tilde/core/common.py
# def html_formula(string):
#     sub, html_formula = False, ''
#     for i in string:
#         if i.isdigit() or i == '.' or i == '-':
#             if not sub:
#                 html_formula += '<sub>'
#                 sub = True
#         else:
#             if sub and i != 'd':
#                 html_formula += '</sub>'
#                 sub = False
#         html_formula += i
#     if sub: html_formula += '</sub>'
#     return html_formula
# 
# def num2name(target, rules, mapping):
#     return mapping[rules['cid']][int(target)] if rules['enumerated'] else target


# Path: tilde/berlinium/categs.py
import os
from tilde.core.common import html_formula, num2name





def wrap_cell(entity, json_obj, mapping, table_view=False):
    '''
    Cell wrappers
    for customizing the GUI data table

    TODO : must coincide with hierarchy!
    TODO : simplify this!
    '''
    html_class = '' # for GUI javascript
    out = ''

    #if 'cell_wrapper' in entity: # TODO : this bound type was defined by apps only
    #    out = entity['cell_wrapper'](json_obj)
    #else:

    if entity['multiple']:
        out = ", ".join(  map(lambda x: num2name(x, entity, mapping), json_obj.get(entity['source'], ['&mdash;']))  )

    elif entity['is_chem_formula']:

        out = html_formula(json_obj[ entity['source'] ]) if entity['source'] in json_obj else '&mdash;'