# Path: chemdataextractor/doc/text.py
# class Footnote(Text):
# 
#     parsers = [ContextParser(), CaptionContextParser()]
# 
#     def _repr_html_(self):
#         return '<p class="cde-footnote">' + self.text + '</p>'
# 
# Path: chemdataextractor/scrape/pub/rsc.py
# def replace_rsc_img_chars(document):
#     """Replace image characters with unicode equivalents."""
#     image_re = re.compile('http://www.rsc.org/images/entities/(?:h[23]+_)?(?:[ib]+_)?char_([0-9a-f]{4})(?:_([0-9a-f]{4}))?\.gif')
#     for img in document.xpath('.//img[starts-with(@src, "http://www.rsc.org/images/entities/")]'):
#         m = image_re.match(img.get('src'))
#         if m:
#             u1, u2 = m.group(1), m.group(2)
#             if not u2 and u1 in RSC_IMG_CHARS:
#                 rep = RSC_IMG_CHARS[u1]
#             else:
#                 rep = ('\\u%s' % u1).encode('ascii').decode('unicode-escape')
#                 if u2:
#                     rep += ('\\u%s' % u2).encode('ascii').decode('unicode-escape')
#             if img.tail is not None:
#                 rep += img.tail  # Make sure we don't remove any tail text
#             parent = img.getparent()
#             if parent is not None:
#                 previous = img.getprevious()
#                 if previous is not None:
#                     previous.tail = (previous.tail or '') + rep
#                 else:
#                     parent.text = (parent.text or '') + rep
#                 parent.remove(img)
#     return document
# 
# Path: chemdataextractor/scrape/clean.py
# class Cleaner(object):
#     def __init__(self, **kwargs):
#     def __call__(self, doc):
#     def clean_html(self, html):
#     def clean_markup(self, markup, parser=None):
# 
# Path: chemdataextractor/reader/markup.py
# class HtmlReader(LxmlReader):
#     """Reader for generic HTML documents."""
# 
#     def detect(self, fstring, fname=None):
#         """"""
#         if fname and not (fname.endswith('.html') or fname.endswith('.htm')):
#             return False
#         return True
# 
#     def _make_tree(self, fstring):
#         root = etree.fromstring(fstring, parser=HTMLParser(encoding=get_encoding(fstring)))
#         return root


import logging
from ..doc.text import Footnote
from ..scrape.pub.rsc import replace_rsc_img_chars
from ..scrape.clean import clean
from .markup import HtmlReader



log = logging.getLogger(__name__)


class RscHtmlReader(HtmlReader):
    """Reader for HTML documents from the RSC."""

    cleaners = [clean, replace_rsc_img_chars]

    root_css = '#wrapper'
    title_css = 'h1, .title_heading'
    heading_css = 'h2, h3, h4, h5, h6, .a_heading, .b_heading, .c_heading, .c_heading_indent, .d_heading, .d_heading_indent'
    citation_css = 'span[id^="cit"]'
    table_css = '.table_caption'
    table_caption_css = 'span[id^="tab"]'
    table_head_row_css = '.table_caption + table thead tr'
    table_body_row_css = '.table_caption + table tbody tr'
    table_footnote_css = '.table_caption + table tfoot tr th .sup_inf'
    reference_css = 'small sup a, a[href^="#cit"], a[href^="#fn"], a[href^="#tab"]'
    figure_css = '.image_table'
    figure_caption_css = '.graphic_title'
    ignore_css = '.table_caption + table, .left_head, sup span.sup_ref, small sup a, a[href^="#fn"], .PMedLink'

    def _parse_table_footnotes(self, fns, refs, specials):
        """Override to account for awkward RSC table footnotes."""
        footnotes = []
        for fn in fns:
            footnote = self._parse_text(fn, refs=refs, specials=specials, element_cls=Footnote)[0]

# Path: chemdataextractor/doc/text.py
# class Footnote(Text):
# 
#     parsers = [ContextParser(), CaptionContextParser()]
# 
#     def _repr_html_(self):
#         return '<p class="cde-footnote">' + self.text + '</p>'
# 
# Path: chemdataextractor/scrape/pub/rsc.py
# def replace_rsc_img_chars(document):
#     """Replace image characters with unicode equivalents."""
#     image_re = re.compile('http://www.rsc.org/images/entities/(?:h[23]+_)?(?:[ib]+_)?char_([0-9a-f]{4})(?:_([0-9a-f]{4}))?\.gif')
#     for img in document.xpath('.//img[starts-with(@src, "http://www.rsc.org/images/entities/")]'):
#         m = image_re.match(img.get('src'))
#         if m:
#             u1, u2 = m.group(1), m.group(2)
#             if not u2 and u1 in RSC_IMG_CHARS:
#                 rep = RSC_IMG_CHARS[u1]
#             else:
#                 rep = ('\\u%s' % u1).encode('ascii').decode('unicode-escape')
#                 if u2:
#                     rep += ('\\u%s' % u2).encode('ascii').decode('unicode-escape')
#             if img.tail is not None:
#                 rep += img.tail  # Make sure we don't remove any tail text
#             parent = img.getparent()
#             if parent is not None:
#                 previous = img.getprevious()
#                 if previous is not None:
#                     previous.tail = (previous.tail or '') + rep
#                 else:
#                     parent.text = (parent.text or '') + rep
#                 parent.remove(img)
#     return document
# 
# Path: chemdataextractor/scrape/clean.py
# class Cleaner(object):
#     def __init__(self, **kwargs):
#     def __call__(self, doc):
#     def clean_html(self, html):
#     def clean_markup(self, markup, parser=None):
# 
# Path: chemdataextractor/reader/markup.py
# class HtmlReader(LxmlReader):
#     """Reader for generic HTML documents."""
# 
#     def detect(self, fstring, fname=None):
#         """"""
#         if fname and not (fname.endswith('.html') or fname.endswith('.htm')):
#             return False
#         return True
# 
#     def _make_tree(self, fstring):
#         root = etree.fromstring(fstring, parser=HTMLParser(encoding=get_encoding(fstring)))
#         return root


# Path: chemdataextractor/reader/rsc.py
import logging
from ..doc.text import Footnote
from ..scrape.pub.rsc import replace_rsc_img_chars
from ..scrape.clean import clean
from .markup import HtmlReader




log = logging.getLogger(__name__)


class RscHtmlReader(HtmlReader):
    """Reader for HTML documents from the RSC."""

    cleaners = [clean, replace_rsc_img_chars]

    root_css = '#wrapper'
    title_css = 'h1, .title_heading'
    heading_css = 'h2, h3, h4, h5, h6, .a_heading, .b_heading, .c_heading, .c_heading_indent, .d_heading, .d_heading_indent'
    citation_css = 'span[id^="cit"]'
    table_css = '.table_caption'
    table_caption_css = 'span[id^="tab"]'
    table_head_row_css = '.table_caption + table thead tr'
    table_body_row_css = '.table_caption + table tbody tr'
    table_footnote_css = '.table_caption + table tfoot tr th .sup_inf'
    reference_css = 'small sup a, a[href^="#cit"], a[href^="#fn"], a[href^="#tab"]'
    figure_css = '.image_table'
    figure_caption_css = '.graphic_title'
    ignore_css = '.table_caption + table, .left_head, sup span.sup_ref, small sup a, a[href^="#fn"], .PMedLink'

    def _parse_table_footnotes(self, fns, refs, specials):
        """Override to account for awkward RSC table footnotes."""
        footnotes = []
        for fn in fns:
            footnote = self._parse_text(fn, refs=refs, specials=specials, element_cls=Footnote)[0]

            footnote += Footnote('', id=fn.getprevious().get('id'))