Ejemplo n.º 1
0
  def GenerateHTML(self, controller, minify=False):
    soup = polymer_soup.PolymerSoup(str(self._soup))

    # Remove decl
    for x in soup.contents:
     if isinstance(x, BeautifulSoup.Declaration):
      if _IsDoctype(x):
        x.extract()

    # Remove all imports
    imports = soup.findAll('link', rel='import')
    for imp in imports:
      imp.extract()

    # Remove all script links
    scripts_external = soup.findAll('script', src=True)
    for script in scripts_external:
      script.extract()

    # Remove all inline script
    scripts_external = soup.findAll('script', src=None)
    for script in scripts_external:
      script.extract()

    # Process all inline styles
    inline_styles = soup.findAll('style')
    for style in inline_styles:
      html = controller.GetHTMLForInlineStylesheet(str(style.string))
      if html:
        ns = BeautifulSoup.Tag(soup, 'style')
        ns.append(BeautifulSoup.NavigableString(html))
        style.replaceWith(ns)
      else:
        style.extract()

    # Rewrite all external stylesheet hrefs or remove, as needed
    stylesheet_links = soup.findAll('link', rel='stylesheet')
    for stylesheet_link in stylesheet_links:
      html = controller.GetHTMLForStylesheetHRef(stylesheet_link['href'])
      if html:
        tmp = polymer_soup.PolymerSoup(html).findChildren()
        assert len(tmp) == 1
        stylesheet_link.replaceWith(tmp[0])
      else:
        stylesheet_link.extract()

    # Remove comments if minifying.
    if minify:
      comments = soup.findAll(
          text=lambda text:isinstance(text, BeautifulSoup.Comment))
      for comment in comments:
        comment.extract()

    # We is done.
    return str(soup)
Ejemplo n.º 2
0
    def GenerateJSForHeadlessImport(self):
        soup = polymer_soup.PolymerSoup(str(self._soup))

        results_js = []

        # Turn all imports into loadHTML()
        imports = soup.findAll('link', rel='import')
        for imp in imports:
            path = imp.extract().get('href')
            assert path, 'Link import cannot be None'
            if path.startswith(os.path.sep) and not os.path.isfile(path):
                path = path[1:]
            results_js.append('loadHTML("%s");' % path)

        # Turn all script links into load()
        scripts_external = soup.findAll('script')
        for script in scripts_external:
            path = script.get('src')
            if path:
                if path.startswith(os.path.sep) and not os.path.isfile(path):
                    path = path[1:]
                results_js.append('load("%s");' % path)
                if script.contents:
                    assert not script.contents[0], (
                        'Script with source cannot have script content: %s' %
                        script.contents[0])
            elif script.contents:
                results_js.append(script.contents[0].strip())

        # We are done.
        return str('\n'.join(results_js))
Ejemplo n.º 3
0
 def __init__(self, html):
   self._soup = polymer_soup.PolymerSoup(html)
   self._inline_scripts = None