#4905 Port pagure's markdown extension to the new API and improve logging
Merged 3 years ago by pingou. Opened 3 years ago by pingou.

file modified
+115 -24
@@ -21,6 +21,8 @@ 

  

  from __future__ import unicode_literals, absolute_import

  

+ import logging

+ 

  import flask

  

  import markdown.inlinepatterns
@@ -44,6 +46,8 @@ 

  

      MK_VERSION = 3

  

+ _log = logging.getLogger(__name__)

+ 

  

  # the (?<!\w) (and variants) we use a lot in all these regexes is a

  # negative lookbehind assertion. It means 'match when the preceding
@@ -90,6 +94,8 @@ 

  

      def handleMatch(self, m):

          """ When the pattern matches, update the text. """

+         _log.debug("MentionPattern: %s", m.groups())

+ 

          name = markdown.util.AtomicString(m.group(2))

          text = "@%s" % name

          user = pagure.lib.query.search_user(flask.g.session, username=name)
@@ -111,6 +117,8 @@ 

  

      def handleMatch(self, m):

          """ When the pattern matches, update the text. """

+         _log.debug("ExplicitLinkPattern: %s", m.groups())

+ 

          is_fork = m.group(2)

          user = m.group(3)

          namespace = m.group(4)
@@ -150,6 +158,8 @@ 

  

      def handleMatch(self, m):

          """ When the pattern matches, update the text. """

+         _log.debug("CommitLinkPattern: %s", m.groups())

+ 

          is_fork = m.group(2)

          user = m.group(3)

          namespace = m.group(4)
@@ -194,6 +204,7 @@ 

          parsing the line as a header. ImplicitIssuePattern will catch

          and parse the text later. Otherwise, we change nothing.

          """

+         _log.debug("ImplicitIssuePreprocessor")

          # match a # character, then any number of digits

          regex = re.compile(r"#([0-9]+)")

          new_lines = []
@@ -231,26 +242,40 @@ 

  

      def handleMatch(self, m):

          """ When the pattern matches, update the text. """

+         _log.debug("ImplicitIssuePattern: %s", m.groups())

          idx = markdown.util.AtomicString(m.group(2))

          text = "#%s" % idx

          try:

              idx = int(idx)

          except (ValueError, TypeError):

+             _log.debug("Invalid integer for %s, bailing", idx)

              return text

  

          try:

              namespace, repo, user = _get_ns_repo_user()

          except RuntimeError:

+             _log.debug("No repo found associated with this context, bailing")

              return text

  

+         _log.debug(

+             "Checking ns: %s, name: %s, user:%s for id: %s",

+             namespace,

+             repo,

+             user,

+             idx,

+         )

+ 

          issue = _issue_exists(user, namespace, repo, idx)

          if issue:

+             _log.debug("Linking to an issue")

              return _obj_anchor_tag(user, namespace, repo, issue, text)

  

          request = _pr_exists(user, namespace, repo, idx)

          if request:

+             _log.debug("Linking to an PR")

              return _obj_anchor_tag(user, namespace, repo, request, text)

  

+         _log.debug("Bailing, return text as is")

          return text

  

  
@@ -259,6 +284,7 @@ 

  

      def handleMatch(self, m):

          """ When the pattern matches, update the text. """

+         _log.debug("ImplicitPRPattern: %s", m.groups())

          idx = markdown.util.AtomicString(m.group(2))

          text = "PR#%s" % idx

          try:
@@ -287,6 +313,7 @@ 

  

      def handleMatch(self, m):

          """ When the pattern matches, update the text. """

+         _log.debug("ImplicitCommitPattern: %s", m.groups())

  

          githash = markdown.util.AtomicString(m.group(2))

          text = "%s" % githash
@@ -309,6 +336,8 @@ 

  

      def handleMatch(self, m):

          """ When the pattern matches, update the text. """

+         _log.debug("StrikeThroughPattern: %s", m.groups())

+ 

          text = markdown.util.AtomicString(m.group(2))

  

          element = markdown.util.etree.Element("del")
@@ -325,6 +354,8 @@ 

          :arg m: the matched object

  

          """

+         _log.debug("AutolinkPattern2: %s", m.groups())

+ 

          url = m.group(2)

          if url.startswith("<"):

              url = url[1:]
@@ -376,7 +407,7 @@ 

  

  

  class PagureExtension(markdown.extensions.Extension):

-     def extendMarkdown(self, md, md_globals):

+     def extendMarkdown(self, md, *args):

          # First, make it so that bare links get automatically linkified.

          AUTOLINK_RE = "(%s)" % "|".join(

              [
@@ -386,37 +417,96 @@ 

                  r"\b[Ii][Rr][Cc][Ss]?://[^)<>\s]+[^.,)<>\s]",

              ]

          )

-         markdown.inlinepatterns.AUTOLINK_RE = AUTOLINK_RE

  

-         md.preprocessors["implicit_issue"] = ImplicitIssuePreprocessor()

- 

-         md.inlinePatterns["mention"] = MentionPattern(MENTION_RE)

+         def _old_mardkown_way():

+             markdown.inlinepatterns.AUTOLINK_RE = AUTOLINK_RE

+             md.preprocessors["implicit_issue"] = ImplicitIssuePreprocessor()

+             md.inlinePatterns["mention"] = MentionPattern(MENTION_RE)

+             # Customize the image linking to support lazy loading

+             md.inlinePatterns["image_link"] = ImagePatternLazyLoad(

+                 markdown.inlinepatterns.IMAGE_LINK_RE, md

+             )

+             md.inlinePatterns["implicit_commit"] = ImplicitCommitPattern(

+                 IMPLICIT_COMMIT_RE

+             )

+             md.inlinePatterns["commit_links"] = CommitLinkPattern(

+                 COMMIT_LINK_RE

+             )

+             md.inlinePatterns["autolink"] = AutolinkPattern2(AUTOLINK_RE, md)

+             if pagure_config.get("ENABLE_TICKETS", True):

+                 md.inlinePatterns["implicit_pr"] = ImplicitPRPattern(

+                     IMPLICIT_PR_RE

+                 )

+                 md.inlinePatterns["explicit_fork_issue"] = ExplicitLinkPattern(

+                     EXPLICIT_LINK_RE

+                 )

+                 md.inlinePatterns["implicit_issue"] = ImplicitIssuePattern(

+                     IMPLICIT_ISSUE_RE

+                 )

+             md.inlinePatterns["striked"] = StrikeThroughPattern(

+                 STRIKE_THROUGH_RE

+             )

+             md.postprocessors[

+                 "encapsulate"

+             ] = EncapsulateMarkdownPostprocessor()

  

-         # Customize the image linking to support lazy loading

-         md.inlinePatterns["image_link"] = ImagePatternLazyLoad(

-             markdown.inlinepatterns.IMAGE_LINK_RE, md

-         )

+         def _new_markdown_way():

+             idx = md.inlinePatterns.get_index_for_name("autolink")

+             md.inlinePatterns[idx].AUTOLINK_RE = AUTOLINK_RE

  

-         md.inlinePatterns["implicit_commit"] = ImplicitCommitPattern(

-             IMPLICIT_COMMIT_RE

-         )

-         md.inlinePatterns["commit_links"] = CommitLinkPattern(COMMIT_LINK_RE)

-         md.inlinePatterns["autolink"] = AutolinkPattern2(AUTOLINK_RE, md)

+             # The number at the end is the priority, the highest priorities are

+             # processed first.

  

-         if pagure_config.get("ENABLE_TICKETS", True):

-             md.inlinePatterns["implicit_pr"] = ImplicitPRPattern(

-                 IMPLICIT_PR_RE

+             md.preprocessors.register(

+                 ImplicitIssuePreprocessor(), "implicit_issue", 100

              )

-             md.inlinePatterns["explicit_fork_issue"] = ExplicitLinkPattern(

-                 EXPLICIT_LINK_RE

+             md.inlinePatterns.register(

+                 MentionPattern(MENTION_RE), "mention", 95

              )

-             md.inlinePatterns["implicit_issue"] = ImplicitIssuePattern(

-                 IMPLICIT_ISSUE_RE

+             # Customize the image linking to support lazy loading

+             md.inlinePatterns.register(

+                 ImagePatternLazyLoad(

+                     markdown.inlinepatterns.IMAGE_LINK_RE, md

+                 ),

+                 "image_link",

+                 90,

+             )

+             md.inlinePatterns.register(

+                 ImplicitCommitPattern(IMPLICIT_COMMIT_RE),

+                 "implicit_commit",

+                 85,

+             )

+             md.inlinePatterns.register(

+                 CommitLinkPattern(COMMIT_LINK_RE), "autolink2", 80

+             )

+             md.inlinePatterns.register(

+                 AutolinkPattern2(AUTOLINK_RE, md), "commit_links", 75

+             )

+             if pagure_config.get("ENABLE_TICKETS", True):

+                 md.inlinePatterns.register(

+                     ImplicitPRPattern(IMPLICIT_PR_RE), "implicit_pr", 70

+                 )

+                 md.inlinePatterns.register(

+                     ExplicitLinkPattern(EXPLICIT_LINK_RE),

+                     "explicit_fork_issue",

+                     65,

+                 )

+                 md.inlinePatterns.register(

+                     ImplicitIssuePattern(IMPLICIT_ISSUE_RE),

+                     "implicit_issue",

+                     60,

+                 )

+             md.inlinePatterns.register(

+                 StrikeThroughPattern(STRIKE_THROUGH_RE), "striked", 50

+             )

+             md.postprocessors.register(

+                 EncapsulateMarkdownPostprocessor(), "encapsulate", 100

              )

  

-         md.inlinePatterns["striked"] = StrikeThroughPattern(STRIKE_THROUGH_RE)

- 

-         md.postprocessors["encapsulate"] = EncapsulateMarkdownPostprocessor()

+         if hasattr(md.inlinePatterns, "get_index_for_name"):

+             _new_markdown_way()

+         else:

+             _old_mardkown_way()

  

          md.registerExtension(self)

  
@@ -541,6 +631,7 @@ 

      repo = flask.request.args.get("repo") or None

  

      if not user and not repo:

+         _log.debug("Extracting repo info from url: %s", url)

          if "fork/" in url:

              user, ext = url.split("fork/")[1].split("/", 1)

          else:

It seems that markdown has now a new way to register extensions and
it throws a number of deprecation warnings on recent version to
invite program to migrate. So this commit ports pagure to this new
API.

It also increase the logging in the module quite a bit to help
debugging any future issue that we face every so often in this
module.

Signed-off-by: Pierre-Yves Chibon pingou@pingoured.fr

rebased onto f96a4ffbc99110856e9277859e78298f1d2aed7c

3 years ago

rebased onto 20790e4125b859ccccc1214235fe6f924ff0cd49

3 years ago

rebased onto e8569a8483b3e07e23fcc9ce9d7229a3e1dd4499

3 years ago

rebased onto f375125bf50b90d9891ab6b671dca07f37ade776

3 years ago

rebased onto 30ee75eeea72ad80828a2fb05e7b950a8d1be042

3 years ago

rebased onto 36d243cdeef9f5f4496eded1671c8812baae9ec0

3 years ago

rebased onto 07306b186fa972af07b50f19e2d40b51bd09053f

3 years ago

rebased onto d897d3c

3 years ago

Pull-Request has been merged by pingou

3 years ago