def _generate_site_links(html_snippet, datamanager): """ Generates a site link, similarly to django's URL tag ; tag must be in the form [ GAME_PAGE_LINK "click here" "pychronia.views.homepage" ]. Escape this tag with ``verbatim text`` form, if it's first processed by restructuredtext, else smart-quotes will break this GAME_PAGE_LINK tag. """ if __debug__: datamanager.notify_event("GENERATE_SITE_LINKS") def site_link_attr_generator(match): matched_str = match.group("view") if "." not in matched_str: matched_str = "pychronia_game.views." + matched_str try: link = game_view_url(matched_str, datamanager=datamanager, neutral_link=True) return dict(href=link) except Exception: logging.warning("Error in generate_site_links for match %r", matched_str, exc_info=True) return None # abort link creation regex = r"""\[\s*GAME_PAGE_LINK\s*('|")(?P<content>[^"]+)('|")\s*('|")(?P<view>[.\w]+)('|")\s*]""" # content will be the text used as link html_res = autolinker.generate_links( html_snippet, regex=regex, link_attr_generator=site_link_attr_generator) return html_res
def _generate_encyclopedia_links(html_snippet, datamanager, excluded_link=None): """ Replaces identified keywords by links to corresponding encyclopedia pages. """ if __debug__: datamanager.notify_event("GENERATE_ENCYCLOPEDIA_LINKS") keywords_mapping = datamanager.get_encyclopedia_keywords_mapping( excluded_link=excluded_link, only_primary_keywords=True) def encyclopedia_link_attr_generator(match): matched_str = match.group(0) assert matched_str # detecting here WHICH keyword triggered the match would be possible, but expensive... let's postpone that link = game_view_url("pychronia_game.views.view_encyclopedia", datamanager=datamanager, neutral_link=True) link += "?search=%s" % urllib.parse.quote_plus( matched_str.encode("utf8"), safe=b"") return dict(href=link) regex = autolinker.join_regular_expressions_as_disjunction(list( keywords_mapping.keys()), as_words=True) ##print(">>>>>>>> REGEX", repr(regex)) if regex: html_res = autolinker.generate_links( html_snippet, regex=regex, link_attr_generator=encyclopedia_link_attr_generator) else: html_res = html_snippet # no changes return html_res
def _generate_messaging_links(html_snippet, datamanager): """ Generates "new message" links for emails identified, provided they have had their @ escapes with a backslash (else they end up as standard mailto links because of docutils systems). ATM we also generate links for current user, but it's not a problem. """ if __debug__: datamanager.notify_event("GENERATE_MESSAGING_LINKS") def email_link_attr_generator(match): matched_str = match.group(0) link = game_view_url("pychronia_game.views.compose_message", datamanager=datamanager) link += "?recipient=%s" % urllib.quote_plus(matched_str.encode("utf8"), safe=b"") return dict(href=link) regex = r"\b[-_\w.]+@\w+\.\w+\b" html_res = autolinker.generate_links(html_snippet, regex=regex, link_attr_generator=email_link_attr_generator) return html_res
def _generate_site_links(html_snippet, datamanager): """ Generates a site link, similarly to django's URL tag ; tag must be in the form [ GAME_PAGE_LINK "click here" "pychronia.views.homepage" ] Rg, in rst-generated text. """ if __debug__: datamanager.notify_event("GENERATE_SITE_LINKS") def site_link_attr_generator(match): matched_str = match.group("view") if "." not in matched_str: matched_str = "pychronia_game.views." + matched_str try: link = game_view_url(matched_str, datamanager=datamanager) return dict(href=link) except Exception: logging.warning("Error in generate_site_links for match %r", matched_str, exc_info=True) return None # abort link creation regex = r"""\[\s*GAME_PAGE_LINK\s*('|")(?P<content>[^"]+)('|")\s*('|")(?P<view>[.\w]+)('|")\s*]""" # content will be the text used as link html_res = autolinker.generate_links(html_snippet, regex=regex, link_attr_generator=site_link_attr_generator) return html_res
def _generate_encyclopedia_links(html_snippet, datamanager, excluded_link=None): """ Replaces identified keywords by links to corresponding encyclopedia pages. """ if __debug__: datamanager.notify_event("GENERATE_ENCYCLOPEDIA_LINKS") keywords_mapping = datamanager.get_encyclopedia_keywords_mapping(excluded_link=excluded_link, only_primary_keywords=True) def encyclopedia_link_attr_generator(match): matched_str = match.group(0) assert matched_str # detecting here WHICH keyword triggered the match would be possible, but expensive... let's postpone that link = game_view_url("pychronia_game.views.view_encyclopedia", datamanager=datamanager) link += "?search=%s" % urllib.quote_plus(matched_str.encode("utf8"), safe=b"") return dict(href=link) regex = autolinker.join_regular_expressions_as_disjunction(keywords_mapping.keys(), as_words=True) ##print (">>>>>>>> REGEX", repr(regex)) if regex: html_res = autolinker.generate_links(html_snippet, regex=regex, link_attr_generator=encyclopedia_link_attr_generator) else: html_res = html_snippet # no changes return html_res
def _generate_messaging_links(html_snippet, datamanager): """ Generates "new message" links for emails identified, provided they have had their @ escapes with a backslash (else they end up as standard mailto links because of docutils systems). ATM we also generate links for current user, but it's not a problem. """ if __debug__: datamanager.notify_event("GENERATE_MESSAGING_LINKS") def email_link_attr_generator(match): matched_str = match.group(0) link = game_view_url("pychronia_game.views.compose_message", datamanager=datamanager, neutral_link=True) link += "?recipient=%s" % urllib.parse.quote_plus( matched_str.encode("utf8"), safe=b"") return dict(href=link) regex = r"\b[-_\w.]+@\w+\.\w+\b" html_res = autolinker.generate_links( html_snippet, regex=regex, link_attr_generator=email_link_attr_generator) return html_res