def test_arxiv_id_jinja_escapes(self): h = 'sosmooth.org' app.config['SERVER_NAME'] = h with app.app_context(): jenv = Environment(autoescape=True) jenv.filters['urlize'] = jenv.filters['urlize'] = urlizer( ['arxiv_id', 'doi', 'url']) self.assertEqual( jenv.from_string( '{{"something hep-th/9901002 or other"|urlize|safe}}' ).render(), f'something <a class="link-https" data-arxiv-id="hep-th/9901002" href="https://arxiv.org/abs/hep-th/9901002">hep-th/9901002</a> or other' ) self.assertEqual( jenv.from_string( '{{"<script>bad junk</script> something 10.1103/PhysRevD.76.013009"|urlize|safe}}' ).render(), '<script>bad junk</script> something <a class="link-https link-external" data-doi="10.1103/PhysRevD.76.013009" href="https://arxiv.org/ct?url=https%3A%2F%2Fdx.doi.org%2F10.1103%2FPhysRevD.76.013009&v=d0670bbf" rel="external noopener nofollow">10.1103/PhysRevD.76.013009</a>' ) self.assertEqual( jenv.from_string( '{{"<script>bad junk</script> http://google.com bla bla ' 'hep-th/9901002 bla"|urlize|safe}}').render(), '<script>bad junk</script> ' '<a class="link-external link-http" href="http://google.com" rel="external noopener nofollow">this http URL</a> bla bla ' f'<a class="link-https" data-arxiv-id="hep-th/9901002" href="https://arxiv.org/abs/hep-th/9901002">hep-th/9901002</a> bla', 'should not double escape')
def test_with_jinja(self): """Basic urlize DOI filter test.""" with app.app_context(): jenv = Environment(autoescape=True) jenv.filters['urlize'] = urlizer(['doi']) self.assertEqual( jenv.from_string( '{{"something 10.1103/PhysRevD.76.013009 or other"|urlize}}' ).render(), 'something <a class="link-https link-external" data-doi="10.1103/PhysRevD.76.013009" href="https://arxiv.org/ct?url=https%3A%2F%2Fdx.doi.org%2F10.1103%2FPhysRevD.76.013009&amp;v=d0670bbf" rel="external noopener nofollow">10.1103/PhysRevD.76.013009</a> or other' )
def register_filters(app: Flask) -> None: """ Register base template filters on a Flask app. Parameters ---------- app : :class:`Flask` """ app.template_filter('abstract_lf_to_br')(abstract_lf_to_br) app.template_filter('urlize')(urlizer()) app.template_filter('tex2utf')(partial(f_tex2utf, greek=True)) app.template_filter('tex2utf_no_symbols')(partial(f_tex2utf, greek=False)) app.template_filter('canonical_url')(canonical_url) app.template_filter('clickthrough_url')(clickthrough_url) app.template_filter('get_category_display')(get_category_display) app.template_filter('get_archive_display')(get_archive_display) app.template_filter('get_group_display')(get_group_display) app.template_filter('embed_content')(embed_content) app.template_filter('tidy_filesize')(tidy_filesize) app.template_filter('as_eastern')(as_eastern) app.template_filter('abs_doi_to_urls')(urlizer(['doi_field'])) app.template_filter('arxiv_id_urlize')(urlizer(['arxiv_id']))
def create_web_app() -> Flask: """Initialize an instance of the browse web application.""" app = Flask('browse', static_url_path=f'/static/browse/{APP_VERSION}') app.config.from_pyfile('config.py') # type: ignore # TODO Only needed until this route is added to arxiv-base if 'URLS' not in app.config: app.config['URLS'] = [] app.config['URLS'].append( ('search_archive', '/search/<archive>', BASE_SERVER)) models.init_app(app) # type: ignore Base(app) Auth(app) app.register_blueprint(ui.blueprint) s3.init_app(app) if not app.jinja_env.globals: app.jinja_env.globals = {} app.jinja_env.globals['canonical_url'] = canonical_url if not app.jinja_env.filters: app.jinja_env.filters = {} app.jinja_env.filters['entity_to_utf'] = entity_to_utf app.jinja_env.filters['clickthrough_url_for'] = clickthrough_url app.jinja_env.filters['show_email_hash'] = \ partial(generate_show_email_hash, secret=app.config.get('SHOW_EMAIL_SECRET')) app.jinja_env.filters['arxiv_id_urls'] = urlizer(['arxiv_id']) app.jinja_env.filters['arxiv_urlize'] = urlizer(['arxiv_id', 'doi', 'url']) app.jinja_env.filters['arxiv_id_doi_filter'] = urlizer(['arxiv_id', 'doi']) return app
def test_with_jinja_escapes(self): """Test the tex2utf filter with jinja escapes.""" with app.app_context(): jenv = Environment(autoescape=True) jenv.filters['urlize'] = urlizer(['arxiv_id', 'doi']) # TODO: urlize doesn't seem to return a Markup object? self.assertEqual( jenv.from_string( '{{"something 10.1103/PhysRevD.76.013009 or other"|urlize}}' ).render(), 'something <a class="link-https link-external" data-doi="10.1103/PhysRevD.76.013009" href="https://arxiv.org/ct?url=https%3A%2F%2Fdx.doi.org%2F10.1103%2FPhysRevD.76.013009&amp;v=d0670bbf" rel="external noopener nofollow">10.1103/PhysRevD.76.013009</a> or other' ) self.assertEqual( jenv.from_string( '{{"<script>bad junk</script> something 10.1103/PhysRevD.76.013009"|urlize}}' ).render(), '<script>bad junk</script> something <a class="link-https link-external" data-doi="10.1103/PhysRevD.76.013009" href="https://arxiv.org/ct?url=https%3A%2F%2Fdx.doi.org%2F10.1103%2FPhysRevD.76.013009&amp;v=d0670bbf" rel="external noopener nofollow">10.1103/PhysRevD.76.013009</a>' )
def test_doi_filter(self): """Test the urlizer DOI filter.""" with app.app_context(): s = 'some test string 23$6#$5<>&456 http://google.com/notadoi' urlize_dois = urlizer(['doi']) self.assertEqual(urlize_dois(s), str(escape(s))) doi = '10.1103/PhysRevD.76.013009' doiurl = urlize_dois(doi) self.assertRegex(doiurl, r'^<a', 'should start with a tag') self.assertEqual( doiurl, str( Markup( '<a class="link-https link-external" data-doi="10.1103/PhysRevD.76.013009" href="https://arxiv.org/ct?url=https%3A%2F%2Fdx.doi.org%2F10.1103%2FPhysRevD.76.013009&v=d0670bbf" rel="external noopener nofollow">10.1103/PhysRevD.76.013009</a>' ))) s = f'something something {doi} endthing' doiurl = urlize_dois(s) self.assertRegex(doiurl, r'<a .* href="', 'Should have an <A> tag') self.assertRegex(doiurl, '^something something ') self.assertRegex(doiurl, ' endthing$') txt = '10.1103/PhysRevA.99.013009 10.1103/PhysRevZ.44.023009 10.1103/PhysRevX.90.012309 10.1103/BioRevX.44.123456' self.assertEqual( urlize_dois(txt), str( Markup( '<a class="link-https link-external" data-doi="10.1103/PhysRevA.99.013009" href="https://arxiv.org/ct?url=https%3A%2F%2Fdx.doi.org%2F10.1103%2FPhysRevA.99.013009&v=94f8600c" rel="external noopener nofollow">10.1103/PhysRevA.99.013009</a> ' '<a class="link-https link-external" data-doi="10.1103/PhysRevZ.44.023009" href="https://arxiv.org/ct?url=https%3A%2F%2Fdx.doi.org%2F10.1103%2FPhysRevZ.44.023009&v=bba1640c" rel="external noopener nofollow">10.1103/PhysRevZ.44.023009</a> ' '<a class="link-https link-external" data-doi="10.1103/PhysRevX.90.012309" href="https://arxiv.org/ct?url=https%3A%2F%2Fdx.doi.org%2F10.1103%2FPhysRevX.90.012309&v=3a1daa37" rel="external noopener nofollow">10.1103/PhysRevX.90.012309</a> ' '<a class="link-https link-external" data-doi="10.1103/BioRevX.44.123456" href="https://arxiv.org/ct?url=https%3A%2F%2Fdx.doi.org%2F10.1103%2FBioRevX.44.123456&v=c8298ca6" rel="external noopener nofollow">10.1103/BioRevX.44.123456</a>' ))) txt = '<script>Im from the user and Im bad</script>' self.assertEqual( urlize_dois(f'{doi} {txt}'), str( Markup( f'<a class="link-https link-external" data-doi="10.1103/PhysRevD.76.013009" href="https://arxiv.org/ct?url=https%3A%2F%2Fdx.doi.org%2F10.1103%2FPhysRevD.76.013009&v=d0670bbf" rel="external noopener nofollow">10.1103/PhysRevD.76.013009</a> <script>Im from the user and Im bad</script>' )))