def add_url(self, url, lastmod=datetime(1900, 1, 1), changefreq="", priority="", alternate=False):
       """ create a new url node. Returns the number of url nodes in sitemap"""
       self.num_urls += 1
       canonical_url, alternate_urls = get_canonical_and_alternates_urls(url, drop_ln=not alternate)
       url_node = u"""
 <url>
   <loc>%s</loc>%s
 </url>"""
       optional = ''
       if lastmod:
           optional += u"""
   <lastmod>%s</lastmod>""" % lastmod.strftime('%Y-%m-%dT%H:%M:%S' + \
                                               DEFAULT_TIMEZONE)
       if changefreq:
           optional += u"""
   <changefreq>%s</changefreq>""" % changefreq
       if priority:
           optional += u"""
   <priority>%s</priority>""" % priority
       if alternate:
           for ln, alternate_url in alternate_urls.iteritems():
               ln = ln.replace('_', '-') ## zh_CN -> zh-CN
               optional += u"""
   <xhtml:link rel="alternate" hreflang="%s" href="%s" />""" % (ln, encode_for_xml(alternate_url, quote=True))
       url_node %= (encode_for_xml(canonical_url), optional)
       self.file_size += len(url_node)
       self.filedescriptor.write(url_node)
       return self.num_urls
   def add_url(self,
               url,
               lastmod=datetime(1900, 1, 1),
               changefreq="",
               priority="",
               alternate=False):
       """ create a new url node. Returns the number of url nodes in sitemap"""
       self.num_urls += 1
       canonical_url, alternate_urls = get_canonical_and_alternates_urls(
           url, drop_ln=not alternate)
       url_node = u"""
 <url>
   <loc>%s</loc>%s
 </url>"""
       optional = ''
       if lastmod:
           optional += u"""
   <lastmod>%s</lastmod>""" % lastmod.strftime('%Y-%m-%dT%H:%M:%S' + \
                                               DEFAULT_TIMEZONE)
       if changefreq:
           optional += u"""
   <changefreq>%s</changefreq>""" % changefreq
       if priority:
           optional += u"""
   <priority>%s</priority>""" % priority
       if alternate:
           for ln, alternate_url in alternate_urls.iteritems():
               ln = ln.replace('_', '-')  ## zh_CN -> zh-CN
               optional += u"""
   <xhtml:link rel="alternate" hreflang="%s" href="%s" />""" % (
                   ln, encode_for_xml(alternate_url, quote=True))
       url_node %= (encode_for_xml(canonical_url), optional)
       self.file_size += len(url_node)
       self.filedescriptor.write(url_node)
       return self.num_urls
Beispiel #3
0
 def tmpl_canonical_and_alternate_urls(self, url):
     """
     Return the snippet of HTML to be put within the HTML HEAD tag in order
     to declare the canonical and language alternate URLs of a page.
     """
     canonical_url, alternate_urls = get_canonical_and_alternates_urls(url)
     out = """  <link rel="canonical" href="%s" />\n""" % cgi.escape(canonical_url, True)
     for ln, alternate_url in alternate_urls.iteritems():
         ln = ln.replace('_', '-') ## zh_CN -> zh-CN
         out += """  <link rel="alternate" hreflang="%s" href="%s" />\n""" % (ln, cgi.escape(alternate_url, True))
     return out
    def tmpl_canonical_and_alternate_urls(self, url, quote_path=True):
        """
        Return the snippet of HTML to be put within the HTML HEAD tag in order
        to declare the canonical and language alternate URLs of a page.

        @param quote_path: if True, the path section of the given
                           C{url} is quoted according to RFC 2396.
                           Useful if reading url from req.unparsed_uri
        """
        canonical_url, alternate_urls = get_canonical_and_alternates_urls(url, quote_path=quote_path)
        out = """  <link rel="canonical" href="%s" />\n""" % cgi.escape(canonical_url, True)
        for ln, alternate_url in alternate_urls.iteritems():
            ln = ln.replace('_', '-') ## zh_CN -> zh-CN
            out += """  <link rel="alternate" hreflang="%s" href="%s" />\n""" % (ln, cgi.escape(alternate_url, True))
        return out
Beispiel #5
0
    def tmpl_canonical_and_alternate_urls(self, url, quote_path=True):
        """
        Return the snippet of HTML to be put within the HTML HEAD tag in order
        to declare the canonical and language alternate URLs of a page.

        @param quote_path: if True, the path section of the given
                           C{url} is quoted according to RFC 2396.
                           Useful if reading url from req.unparsed_uri
        """
        canonical_url, alternate_urls = get_canonical_and_alternates_urls(url, quote_path=quote_path)
        out = """  <link rel="canonical" href="%s" />\n""" % cgi.escape(canonical_url, True)
        for ln, alternate_url in alternate_urls.iteritems():
            ln = ln.replace('_', '-') ## zh_CN -> zh-CN
            out += """  <link rel="alternate" hreflang="%s" href="%s" />\n""" % (ln, cgi.escape(alternate_url, True))
        return out
Beispiel #6
0
def inject_utils():
    """
    This will add some more variables and functions to the Jinja2 to execution
    context. In particular it will add:

    - `url_for`: an Invenio specific wrapper of Flask url_for, that will let you
                 obtain URLs for non Flask-native handlers (i.e. not yet ported
                 Invenio URLs)
    - `breadcrumbs`: this will be a list of three-elements tuples, containing
                 the hierarchy of Label -> URLs of navtrails/breadcrumbs.
    - `_`: this can be used to automatically translate a given string.
    - `is_language_rtl`: is True if the chosen language should be read right to left
    """
    from werkzeug.routing import BuildError

    from invenio.messages import is_language_rtl
    from invenio.webinterface_handler_flask_utils import _, guess_language
    from invenio.webuser_flask import current_user
    from invenio.urlutils import create_url, get_canonical_and_alternates_urls

    def invenio_url_for(endpoint, **values):
        try:
            return url_for(endpoint, **values)
        except BuildError:
            if endpoint.startswith('http://') or endpoint.startswith(
                    'https://'):
                return endpoint
            if endpoint.startswith('.'):
                endpoint = request.blueprint + endpoint
            return create_url('/' + '/'.join(endpoint.split('.')), values,
                              False).decode('utf-8')

    if request.endpoint in current_app.config['breadcrumbs_map']:
        breadcrumbs = current_app.config['breadcrumbs_map'][request.endpoint]
    elif request.endpoint:
        breadcrumbs = [(_('Home'), '')
                       ] + current_app.config['breadcrumbs_map'].get(
                           request.endpoint.split('.')[0], [])
    else:
        breadcrumbs = [(_('Home'), '')]

    user = current_user._get_current_object()
    canonical_url, alternate_urls = get_canonical_and_alternates_urls(
        request.environ['PATH_INFO'])
    alternate_urls = dict((ln.replace('_', '-'), alternate_url)
                          for ln, alternate_url in alternate_urls.iteritems())

    guess_language()

    from invenio.bibfield import get_record  # should not be global due to bibfield_config
    return dict(_=lambda *args, **kwargs: g._(*args, **kwargs),
                current_user=user,
                get_css_bundle=current_app.jinja_env.get_css_bundle,
                get_js_bundle=current_app.jinja_env.get_js_bundle,
                is_language_rtl=is_language_rtl,
                canonical_url=canonical_url,
                alternate_urls=alternate_urls,
                get_record=get_record,
                url_for=invenio_url_for,
                breadcrumbs=breadcrumbs,
                **TEMPLATE_CONTEXT_FILTERS)