Ejemplo n.º 1
0
def reorganize_by_chapters(section):
    def is_chapter_marker(x):
        return isinstance(
            x,
            Tag) and x.name == 'h1' and (not 'part' in x.attrs.get('id', ''))

    elements = section.contents
    sections = make_sections2(elements,
                              is_chapter_marker,
                              attrs={'level': 'sec-down'})
    res = tag_like(section)
    for header, section in sections:
        if not header:

            S = Tag(name='section')
            S.attrs['level'] = 'sec'
            S.attrs['class'] = 'without-header-inside'
            section2 = reorganize_by_section(section)
            S.append(section2)
            res.append(S)

        else:
            S = Tag(name='section')
            S.attrs['level'] = 'sec'
            S.attrs['class'] = 'with-header-inside'
            S.append(header)
            section2 = reorganize_by_section(section)
            S.append(section2)
            copy_attributes_from_header(S, header)
            res.append(S)
    return res
Ejemplo n.º 2
0
def reorganize_by_parts(body):
    elements = list(body.contents)
    with timeit('reorganize_by_parts:make_sections'):
        sections = make_sections2(elements,
                                  is_part_marker,
                                  attrs={'level': 'part-down'})

    with timeit('reorganize_by_parts:copying'):
        res = tag_like(body)

        for header, section in sections:
            if not header:
                S = Tag(name='section')
                S.attrs[ATTR_LEVEL] = 'part'
                S.attrs['class'] = CLASS_WITHOUT_HEADER
                S.append('\n')
                section2 = reorganize_by_chapters(section)
                S.append(section2)
                res.append('\n\n')
                res.append(S)
                res.append('\n\n')
            else:
                S = Tag(name='section')
                S.attrs[ATTR_LEVEL] = 'part'
                S.attrs['class'] = CLASS_WITH_HEADER
                S.append('\n')
                S.append(header)
                section2 = reorganize_by_chapters(section)
                S.append(section2)
                copy_attributes_from_header(S, header)
                res.append('\n\n')
                res.append(S)
                res.append('\n\n')
        return res
Ejemplo n.º 3
0
def reorganize_by_chapters(section):
    elements = list(section.contents)
    sections = make_sections2(elements,
                              is_chapter_marker,
                              attrs={'level': 'sec-down'})
    res = tag_like(section)
    for header, section in sections:
        if not header:

            S = Tag(name='section')
            S.attrs[ATTR_LEVEL] = 'sec'
            S.attrs['class'] = CLASS_WITHOUT_HEADER
            S.append('\n')
            section2 = reorganize_by_section(section)
            S.append(section2)
            res.append('\n\n')
            res.append(S)
            res.append('\n\n')

        else:
            S = Tag(name='section')
            S.attrs[ATTR_LEVEL] = 'sec'
            S.attrs['class'] = CLASS_WITH_HEADER
            S.append('\n')
            S.append(header)
            section2 = reorganize_by_section(section)
            S.append(section2)
            copy_attributes_from_header(S, header)
            res.append('\n\n')
            res.append(S)
            res.append('\n\n')
    return res
Ejemplo n.º 4
0
def reorganize_by_parts(body):
    elements = body.contents
    sections = make_sections2(elements,
                              is_part_marker,
                              attrs={'level': 'part-down'})
    res = tag_like(body)

    for header, section in sections:
        if not header:
            S = Tag(name='section')
            S.attrs['level'] = 'part'
            S.attrs['class'] = 'without-header-inside'
            section2 = reorganize_by_chapters(section)
            S.append(section2)
            res.append(S)
        else:
            S = Tag(name='section')
            S.attrs['level'] = 'part'
            S.attrs['class'] = 'with-header-inside'
            S.append(header)
            section2 = reorganize_by_chapters(section)
            S.append(section2)
            copy_attributes_from_header(S, header)
            res.append(S)
    return res
Ejemplo n.º 5
0
def reorganize_by_subsection(section):
    def is_section_marker(x):
        return isinstance(x, Tag) and x.name == 'h3'

    elements = section.contents
    sections = make_sections2(elements,
                              is_section_marker,
                              attrs={'level': 'subsub-down'})
    res = tag_like(section)
    for header, section in sections:
        if not header:
            S = Tag(name='section')
            S.attrs['level'] = 'subsub'
            S.attrs['class'] = 'without-header-inside'
            S.append(section)
            res.append(S)
        else:
            S = Tag(name='section')
            S.attrs['level'] = 'subsub'
            S.attrs['class'] = 'with-header-inside'
            S.append(header)
            S.append(section)
            copy_attributes_from_header(S, header)
            res.append(S)

    return res
Ejemplo n.º 6
0
def reorganize_by_subsection(section):
    def is_section_marker(x):
        return isinstance(x, Tag) and x.name == 'h3'

    elements = list(section.contents)
    sections = make_sections2(elements,
                              is_section_marker,
                              attrs={'level': 'subsub-down'})
    res = tag_like(section)
    for header, section in sections:
        if not header:
            S = Tag(name='section')
            S.attrs[ATTR_LEVEL] = 'subsub'
            S.attrs['class'] = CLASS_WITHOUT_HEADER
            S.append(section)
            res.append('\n\n')
            res.append(S)
            res.append('\n\n')
        else:
            S = Tag(name='section')
            S.attrs[ATTR_LEVEL] = 'subsub'
            S.attrs['class'] = CLASS_WITH_HEADER
            S.append(header)
            S.append(section)
            copy_attributes_from_header(S, header)
            res.append('\n\n')
            res.append(S)
            res.append('\n\n')

    return res
Ejemplo n.º 7
0
    def as_html(self, inline=False):
        div = Tag(name='div')

        if not inline:
            p = Tag(name='p')
            p.append('Jump to ')
            a = Tag(name='a')

            # if inline:
            #     href = '#%s' % self.element_id
            # else:
            href = '#%s' % self.element_id
            a.attrs['href'] = href
            a.append('element in output file')
            p.append(a)
            p.append('.')
            div.append(p)

        p = Tag(name='p')
        p.append('It happened at line %s of:' % self.line)
        div.append(p)

        div.append(self.original_file.as_html(inline=False))

        return div
Ejemplo n.º 8
0
def create_notes_from_elements(soup, res, location, unique):
    for klass, tag in MCDPManualConstants.classes_that_create_notes.items():
        markers = list(soup.select('.%s' % klass))
        # print('Found %d markers for class %s' % (len(markers), klass))
        for p in markers:
            div = Tag(name='div')
            s = Tag(name='p')
            s.append('The following was marked as "%s".' % klass)
            div.append(s)
            div2 = Tag(name='div')
            div2.attrs[
                'style'] = 'margin: 1em; font-size: 90%; background-color: #eee; border-radius: 5px; padding: 0.5em;'
            # Copy:
            p2 = get_sanitized_copy(p)
            div2.append(p2)
            div.append(div2)

            tags = [tag]
            from mcdp_docs.manual_join_imp import split_robustly
            assignees = split_robustly(p.attrs.get("for", ""), ',')
            for a in assignees:
                tags.append('for:%s' % a)

            note = Note(div, HTMLIDLocation.for_element(p, location, unique=unique),
                        stacklevel=0, tags=tuple(sorted(tags)))
            res.add_note(note)
Ejemplo n.º 9
0
def append_urls(id_log, log, where, url_to_resource):
    n = 0
    for rname in log.resources:

        dtr = DTR.from_yaml(log.resources[rname])
        size_mb = f"{dtr.size / (1000 * 1000.0):.1f} MB"
        s = f"{rname} ({size_mb}) "
        where.append(s)

        urls = [url_to_resource(log, rname)]
        #        if Gallery.deploy_ipfs:
        #            ipfs = log.resources[rname]['hash']['ipfs']
        #            urls = ['/ipfs/%s' % ipfs]
        #        else:
        #            urls = [x for x in dtr.urls if show_url(x)]

        for i, url in enumerate(urls):
            where.append(" ")
            a = Tag(name="a")
            a.attrs["download"] = f"{id_log}.{rname}"
            a.attrs["href"] = url
            a.append(f"link {i}")
            where.append(a)
            n += 1
        where.append(Tag(name="br"))
    return n
Ejemplo n.º 10
0
def get_links(build, branch=None):
    summary = Tag(name='summary')

    # if there is an "index" or "summary" use it
    res = Tag(name='div')
    for art in build.artefacts:
        # print 'display: %s' % art.display
        if art.display in ['index', 'summary']:
            # print('using %s as index' % str(art))
            a = Tag(name='a')
            path = art.rel
            if branch is not None:
                path = path.replace(
                    'builds/%s' % build.get_build_num(),
                    'branch/%s' % path_frag_from_branch(branch))

            a.attrs['href'] = path
            # a.append(art.display)
            a.append('index')
            # a.attrs['style'] = 'float:left'
            summary.append(a)
            summary.append(' ')

    summary.append('%d\xc2\xa0artefacts' % (len(build.artefacts)))

    s = Tag(name='details')
    s.append(summary)
    s.append(get_links_(build, branch))
    res.append(s)
    return res
Ejemplo n.º 11
0
def append_urls(id_log, log, where, url_to_resource):
    n = 0
    for rname in log.resources:

        dtr = DTR.from_yaml(log.resources[rname])
        size_mb = '%.1f MB' % (dtr.size / (1000 * 1000.0))
        s = '%s (%s) ' % (rname, size_mb)
        where.append(s)

        urls = [url_to_resource(log, rname)]
        #        if Gallery.deploy_ipfs:
        #            ipfs = log.resources[rname]['hash']['ipfs']
        #            urls = ['/ipfs/%s' % ipfs]
        #        else:
        #            urls = [x for x in dtr.urls if show_url(x)]

        for i, url in enumerate(urls):
            where.append(' ')
            a = Tag(name='a')
            a.attrs['download'] = "%s.%s" % (id_log, rname)
            a.attrs['href'] = url
            a.append('link %s' % i)
            where.append(a)
            n += 1
        where.append(Tag(name='br'))
    return n
Ejemplo n.º 12
0
 def ff(*args, **kwargs):
     res = f(*args, **kwargs)
     pre = Tag(name='pre')  # **{'class': 'print_value'})
     code = Tag(name='code')
     code.string = res
     pre.append(code)
     return pre
Ejemplo n.º 13
0
    def make(self, context):
        soup = context.soup
        id_ = '%s:section' % self.id_ 
        try:
            e = soup_find_absolutely(soup, id_)
        except KeyError:
            msg = 'Cannot find ID %r in document.' % id_
            d = Tag(name='div')
            t = Tag(name='code')
            t.append(self.id_)
            d.append(t)
            note_error2(t, 'ref error', msg)
            return [d]
        logger.info('Adding section %r' %  e.attrs['id'])
#         logger.info('e: ' + get_summary_of_section(e))
        e_copy = e.__copy__()
        
        for eid in self.exceptions:
            logger.info('Removing sections by id "%s"' % eid)
            look_for = eid + ':section'
            s = e_copy.find(id=look_for)
            if s is None:
                msg = 'Could not remove "%s" because could not find element with ID "%s"' % (eid, look_for)
                raise Exception(msg)
            s.extract()
#         logger.info('e_copy: ' + get_summary_of_section(e_copy))
        
        return [e_copy] 
Ejemplo n.º 14
0
 def as_html(self, inline=False):
     pre = Tag(name='pre')
     code = Tag(name='span')
     code.attrs['class'] = 'location'
     s = str(self)
     code.append(s)
     pre.append(code)
     return pre
Ejemplo n.º 15
0
 def as_html(self, inline=False):
     div = Tag(name='div')
     if inline:
         div.append(Comment(str(self)))
         pass
     else:
         p = Tag(name='p')
         p.append('Location not known more precisely.')
         div.append(p)
     return div
Ejemplo n.º 16
0
 def test_new_tag_creation(self):
     builder = builder_registry.lookup('html')()
     soup = self.soup("<body></body>", builder=builder)
     a = Tag(soup, builder, 'a')
     ol = Tag(soup, builder, 'ol')
     a['href'] = 'http://foo.com/'
     soup.body.insert(0, a)
     soup.body.insert(1, ol)
     assert soup.body.encode(
     ) == b'<body><a href="http://foo.com/"></a><ol></ol></body>'
Ejemplo n.º 17
0
def video_for_source_2(rel):
    video = Tag(name="video")
    video.attrs["width"] = 64
    video.attrs["height"] = 48
    video.attrs["loop"] = 1
    video.attrs["autoplay"] = 1
    source = Tag(name="source")
    source.attrs["src"] = rel
    source.attrs["type"] = "video/mp4"
    video.append(source)
    return video
Ejemplo n.º 18
0
def make_page(contents, head0, add_toc, extra_panel_content, add_home_link):
    """ Returns html (Beautiful Soup document) """
    html = Tag(name='html')

    head = head0.__copy__()
    html.append(head)
    body = Tag(name='body')

    with timeit('make_page() / copy toc'):
        if add_toc is not None:
            tocdiv = Tag(name='div')
            tocdiv.attrs['id'] = 'tocdiv'
            if add_home_link:
                a = Tag(name='a')
                a.append('Home')
                a.attrs['href'] = 'index.html'
                p = Tag(name='p')
                p.append(a)
                tocdiv.append(p)

            if extra_panel_content is not None:
                details = Tag(name='details')
                details.attrs['id'] = 'build-details'
                summary = Tag(name='summary')
                summary.append('build details')
                details.append(summary)
                details.append(extra_panel_content)
                tocdiv.append(details)

            tocdiv.append(add_toc)

            body.append(tocdiv)

    section_name = get_first_header_title(contents)
    if section_name is not None:
        section_name = section_name.replace('</code>', '</code> ')
        section_name = gettext(bs(section_name))
        title2 = Tag(name='title')
        title2.append(section_name)

        title = head.find('title')
        if title is None:
            head.append(title2)
        else:
            title.replace_with(title2)

    not_toc = Tag(name='div')
    not_toc.attrs['id'] = 'not-toc'
    not_toc.append(contents)
    body.append(not_toc)
    html.append(body)

    # delete the original one
    if False:
        main_toc = contents.find(id=MCDPManualConstants.MAIN_TOC_ID)
        if main_toc is not None:
            main_toc.extract()

    return html
Ejemplo n.º 19
0
def video_for_source_2(rel):
    video = Tag(name='video')
    video.attrs['width'] = 64
    video.attrs['height'] = 48
    video.attrs['loop'] = 1
    video.attrs['autoplay'] = 1
    source = Tag(name='source')
    source.attrs['src'] = rel
    source.attrs['type'] = 'video/mp4'
    video.append(source)
    return video
Ejemplo n.º 20
0
def video_for_source(rel, width="100%"):
    video = Tag(name="video")
    video.attrs["width"] = width
    #    video.attrs['height'] = height
    video.attrs["loop"] = 1
    video.attrs["autoplay"] = 1
    source = Tag(name="source")
    source.attrs["src"] = rel
    source.attrs["type"] = "video/mp4"
    video.append(source)
    return video
Ejemplo n.º 21
0
def video_for_source(rel, width='100%'):
    video = Tag(name='video')
    video.attrs['width'] = width
    #    video.attrs['height'] = height
    video.attrs['loop'] = 1
    video.attrs['autoplay'] = 1
    source = Tag(name='source')
    source.attrs['src'] = rel
    source.attrs['type'] = 'video/mp4'
    video.append(source)
    return video
Ejemplo n.º 22
0
def html_table_from_table(logs, url_to_resource):
    res = Tag(name='table')
    tbody = Tag(name='tbody')
    thead = Tag(name='thead')
    res.append(thead)
    res.append(tbody)
    for i, (_, log) in enumerate(logs.items()):
        trh, tr = get_row(i, log, url_to_resource)
        tbody.append(tr)
        tbody.append('\n')
    thead.append(trh)
    return res
Ejemplo n.º 23
0
def html_table_from_table(logs, url_to_resource):
    res = Tag(name="table")
    tbody = Tag(name="tbody")
    thead = Tag(name="thead")
    res.append(thead)
    res.append(tbody)
    for i, (_, log) in enumerate(logs.items()):
        trh, tr = get_row(i, log, url_to_resource)
        tbody.append(tr)
        tbody.append("\n")
    thead.append(trh)  # FIXME
    return res
Ejemplo n.º 24
0
    def as_html(self, inline=False):
        div = Tag(name='div')
        pre = Tag(name='pre')
        code = Tag(name='span')
        code.attrs['class'] = 'location'
        code.append(str(self.where))
        pre.append(code)
        div.append(pre)

        div.append(self.parent.as_html(inline=inline))

        return div
Ejemplo n.º 25
0
def add_prev_next_links(filename2contents, only_for=None):
    new_one = OrderedDict()
    for filename, contents in list(filename2contents.items()):
        if only_for and not filename in only_for: continue

        id_prev = contents.attrs[ATTR_PREV]
        a_prev = Tag(name='a')
        a_prev.attrs['href'] = '#' + str(id_prev)
        a_prev.attrs['class'] = CLASS_LINK_PREV
        a_prev.append('prev')

        id_next = contents.attrs[ATTR_NEXT]
        a_next = Tag(name='a')
        a_next.attrs['href'] = '#' + str(id_next)
        a_next.attrs['class'] = CLASS_LINK_NEXT
        a_next.append('next')

        S = Tag(name='div')
        S.attrs['class'] = ['super']

        nav1 = Tag(name='div')
        add_class(nav1, 'navigation')
        if id_prev:
            nav1.append(a_prev.__copy__())
        if id_next:
            nav1.append(a_next.__copy__())
        spacer = Tag(name='div')
        spacer.attrs['style'] = 'clear:both'
        nav1.append(spacer)

        add_class(contents, 'main-section-for-page')

        contents2 = contents
        S.append(contents2)

        from .source_info_imp import get_main_header
        actual_id = get_main_header(contents2)

        if False:  # just checking
            e = contents2.find(id=actual_id)
            if e is not None:
                pass
            else:
                logger.error('not found %r' % actual_id)
        S.attrs['id'] = actual_id

        contents2.insert(0, nav1.__copy__())
        contents2.append(nav1.__copy__())

        new_one[filename] = S

    return new_one
Ejemplo n.º 26
0
    def make(self, context):
#         <section class="with-header-inside" id="part:all:section" level="part"><h1 counter-part="1" id="part:all" label-name="All units" label-number="A" label-self="" label-what="Part" label-what-number="Part A" label-what-number-name="Part A - All units">All units</h1><section class="without-header-inside" level="sec"><section class="without-header-inside" level="sub"><div style="display:none">Because of mathjax bug</div>
# </section>
        section = Tag(name='section')
        section.attrs['class'] = ['with-header-inside']
        section.attrs['level'] = ['part']
        section.attrs['id'] = "part:%s:section" % self.id_part
        h = Tag(name='h1') 
        h.attrs['id'] = 'part:' + self.id_part
        h.append(self.title)
        section.append(h)
        append_all(section, self.contents.make(context))
        return [section]
Ejemplo n.º 27
0
def make_last_modified(files_contents, nmax=100):
    res = AugmentedResult()
    files_contents = [DocToJoin(*x) for x in files_contents]
    files_contents = [_ for _ in files_contents if _.source_info]

    files_contents = list(
        sorted(files_contents,
               key=lambda x: x.source_info.last_modified,
               reverse=True))

    r = Tag(name='fragment')
    r.append('\n')
    h = Tag(name='h1')
    h.append('Last modified')
    h.attrs['id'] = 'sec:last-modified'
    r.append(h)
    r.append('\n')

    ul = Tag(name='ul')
    ul.append('\n')
    for d in files_contents[:nmax]:
        li = Tag(name='li')
        when = d.source_info.last_modified
        when_s = time.strftime("%a, %b %d", when)
        #          %H:%M
        li.append(when_s)
        li.append(': ')

        hid = get_main_header(bs(d.contents))
        if hid is None:
            what = "File %s" % d.docname
        else:
            what = Tag(name='a')
            what.attrs['href'] = '#' + hid
            what.attrs['class'] = MCDPManualConstants.CLASS_NUMBER_NAME

        li.append(what)
        li.append(' (')
        name = d.source_info.author.name
        li.append(name)
        li.append(')')

        ul.append(li)
        ul.append('\n')

    r.append(ul)
    s = to_html_stripping_fragment(r)
    #     print s

    res.set_result(s)
    return res
Ejemplo n.º 28
0
def load_tag():
    """Helper function to create a complex bs4 tag for testing."""

    tag = Tag(name='Out')
    inn1a = Tag(name='Inn1')
    inn1b = Tag(name='Inn1')

    inn1a.append('words words')
    inn1b.append('more words')

    tag.append(inn1a)
    tag.append(inn1b)

    return tag
Ejemplo n.º 29
0
def add_prev_next_links(filename2contents):
    for filename, contents in filename2contents.items():
        id_prev = contents.attrs['prev']
        if id_prev is not None:
            a = Tag(name='a')
            a.attrs['href'] = '#' + id_prev
            a.append('prev')
            contents.insert(0, a)

        id_next = contents.attrs['next']
        if id_next is not None:
            a = Tag(name='a')
            a.attrs['href'] = '#' + id_next
            a.append('next')
            contents.append(a)
Ejemplo n.º 30
0
def embed_css_files(soup):
    """ Look for <link> elements of CSS and embed them if they are local files"""
    # <link href="..." rel="stylesheet" type="text/css"/>
    for link in list(
            soup.findAll('link', attrs={
                'rel': 'stylesheet',
                'href': True
            })):
        href = link.attrs['href']
        if href.startswith('file://'):
            filename = href.replace('file://', '')
        elif href.startswith('/'):  # not on windows?
            filename = href
        else:
            filename = None

        if filename is not None:

            if not os.path.exists(filename):
                msg = 'Cannot find CSS file %s' % filename
                logger.error(msg)
            else:
                logger.info('Embedding %r' % friendly_path(filename))
                data = open(filename).read()
                style = Tag(name='style')
                style.attrs['type'] = 'text/css'
                style.string = data
                link.replace_with(style)