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 iteritems(alternate_urls): 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 iteritems(alternate_urls): 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 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) - `_`: this can be used to automatically translate a given string. - `is_language_rtl`: True if the chosen language should be read right to left. """ from werkzeug.routing import BuildError from invenio.base.i18n import is_language_rtl from flask.ext.babel import get_translations from flask.ext.login import current_user from invenio.utils.url 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') 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 iteritems(alternate_urls)) try: from invenio.modules.records.api import get_record # should not be global due to bibfield_config except: get_record = lambda *args, **kwargs: None # from invenio.modules.formatter.engine import TEMPLATE_CONTEXT_FUNCTIONS_CACHE return dict( 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, #**TEMPLATE_CONTEXT_FUNCTIONS_CACHE.template_context_functions )
def inject_utils(): """ Inject variables and functions to jinja 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) - ``_``: this can be used to automatically translate a given string. - ``is_language_rtl``: True if the chosen language should be read right to left. """ from werkzeug.routing import BuildError from invenio.base.i18n import is_language_rtl from flask.ext.login import current_user from invenio.utils.url import create_url, get_canonical_and_alternates_urls def invenio_url_for(endpoint, **values): try: return url_for(endpoint, **values) except BuildError: if re.match("https?://", endpoint, re.IGNORECASE): return endpoint if endpoint.startswith('.'): endpoint = request.blueprint + endpoint url = create_url('/' + endpoint.replace('.', '/'), values, False) return url.decode('utf-8') user = current_user._get_current_object() canonical_url, alternate_urls = get_canonical_and_alternates_urls( request.path) alternate_urls = dict((ln.replace('_', '-'), alternate_url) for ln, alternate_url in iteritems(alternate_urls)) try: # should not be global due to bibfield_config from invenio.modules.records.api import get_record except: get_record = lambda *args, **kwargs: None return dict( current_user=user, is_language_rtl=is_language_rtl, canonical_url=canonical_url, alternate_urls=alternate_urls, get_record=get_record, url_for=invenio_url_for, )
def inject_utils(): """Inject variables and functions to jinja 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) - ``_``: this can be used to automatically translate a given string. - ``is_language_rtl``: True if the chosen language should be read right to left. """ from invenio.base.i18n import is_language_rtl from invenio_records.api import get_record from invenio.utils.url import create_url, get_canonical_and_alternates_urls def invenio_url_for(endpoint, **values): try: return url_for(endpoint, **values) except BuildError: if re.match("https?://", endpoint, re.IGNORECASE): return endpoint if endpoint.startswith('.'): endpoint = request.blueprint + endpoint url = create_url('/' + endpoint.replace('.', '/'), values, False) return url.decode('utf-8') user = current_user._get_current_object() canonical_url, alternate_urls = get_canonical_and_alternates_urls( request.path) alternate_urls = dict((ln.replace('_', '-'), alternate_url) for ln, alternate_url in iteritems(alternate_urls)) return dict( current_user=user, is_language_rtl=is_language_rtl, canonical_url=canonical_url, alternate_urls=alternate_urls, get_record=get_record, url_for=invenio_url_for, )