From patchwork Thu Jan 30 11:57:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lukas Fleischer X-Patchwork-Id: 1471 Return-Path: Delivered-To: patchwork@archlinux.org Received: from apollo.archlinux.org (localhost [127.0.0.1]) by apollo.archlinux.org (Postfix) with ESMTP id 64DE516BB879F for ; Thu, 30 Jan 2020 11:57:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.3 (2019-12-06) on apollo.archlinux.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1,DKIM_VALID=-0.1,DKIM_VALID_AU=-0.1, MAILING_LIST_MULTI=-1,RCVD_IN_DNSWL_MED=-2.3,SPF_HELO_NONE=0.001, T_DMARC_POLICY_NONE=0.01 autolearn=ham autolearn_force=no version=3.4.3 X-Spam-BL-Results: [127.0.9.2] Received: from orion.archlinux.org (orion.archlinux.org [88.198.91.70]) by apollo.archlinux.org (Postfix) with ESMTPS for ; Thu, 30 Jan 2020 11:57:09 +0000 (UTC) Received: from orion.archlinux.org (localhost [127.0.0.1]) by orion.archlinux.org (Postfix) with ESMTP id 91D0A1884ABEC2; Thu, 30 Jan 2020 11:57:04 +0000 (UTC) Received: from luna.archlinux.org (luna.archlinux.org [IPv6:2a01:4f8:160:3033::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits)) (No client certificate requested) (Authenticated sender: luna) by orion.archlinux.org (Postfix) with ESMTPSA id 346BB1884ABEC0; Thu, 30 Jan 2020 11:57:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=archlinux.org; s=orion; t=1580385424; bh=6ktBTB05zn0biCrnzdLA8yho/wsoVYk7SK0I4PhZJeA=; h=From:To:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe; b=pDnugqu//rHOhJyQA6AmmqnuoJiY1qRUYxsaUjOvO7wTCTu7pAsXjFKT1RTRH1LKr zGg9MRG4wsS+J7z9Lv8XpgKDzodMP0ILzCYgPSBJR8w7RVXhuxpQjeq7+mecbqHFnf mS1CIZa8syP2ENNu/1UmA/MOuCyfFKMMVOBlMgUaZmvSTe78oV5IsAnNcLnbN/l11w EI2PAv9p2dBLEUwqFYNUW8dkHCNF0gwN67FmEE881bevFWGF4my9Ii5MsI8Nm829Ou E2RLG99EOcIRJabpZSd61O7fHNnxTImMEtDCuRb8SqBLH8HzjUPN76cF71YRdRi2Ww 5i4QWDWySVKjx1iQG4kwL8DwBXQxKkBEqm/oNNWWJ1yW4xTIfQAEzfYSi4nGg7yD8L ZIweh2/cZxqWiIPrIyo886Z1sxpDRzqGJ18R/c4BUaAiBr2LdhHSVJkA0dHm47E1dK kf0J2URzStoIvH9AGnWQJzld0aToKoJ0Hv5xE+bIDRtrYkJ+NT4X7lrBNVhLwdndQi QWqBgqsI9lVPCNa2ZjL54uOwMVuQsVYqlWYaKMcKE0cE6G4IgC2qkatn5MwyaT7rSI 9ZpcvkJ6aeacyz6pAEVD62AIOQDK/VcrL37ixcxDRUrTfcOtDW8c7eKKrRitLGwSw2 yKhHrH+N4w+1ikiA6oDGxQC0= Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 1B6AA2BE52; Thu, 30 Jan 2020 11:57:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=archlinux.org; s=luna2; t=1580385424; bh=6ktBTB05zn0biCrnzdLA8yho/wsoVYk7SK0I4PhZJeA=; h=From:To:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe; b=oyu3o/44gNeioEtY53lo92Ghs8w/YxPoQpf0finT5MJKFEFnVZlMvV2K90cphXxOH e8B7wOxVnIjd7Azw7zkH0OQ/+6fIfI6DZr8CyV1Vkl823ID/jwJxyj1nX8LMh6Kb+e KFU7pxT+dINYgf+PQER/Qpy5UJqQN0CsDF8Kf0wcucxt0+NlPTEBjtWP1yC6uD4f7H 3l1yHGdxgWZFBoxa8LSSPh7U6cQ0v7Pr3Kpakh1QYzmPJQfeddzw4oBj0/TuUTmmvc MoQbbVOBh4wrShQeaQTbl0VcQ1D7975p8Sz+RsepQySHC9S74PCaDzNuD09tNEaMUs BkeJa9gf496+t3NuOhyqqnXgHzqoge9hNzcMqUygKsm2Jd55i0HC++Xf0c+hRYZ3T/ 1m1tBX1d+Lp4bHwwZNQbytBl4ebwEgWLQ0XYVImFa7Y6Cp+kAWX0R2vvhHpPJKrJEZ NQyxs3gye9Z2+B4WRFWaHJIIc94ENR/7OV5CU7jV7B0GtsdR4Zim38LrbZDbTh3UCk Z37AB0b0ydxeqzryJhnbPNxTm3OJGJL2oVfXJ5FhEI2ylDeJKV4qoSDOqegD2lyMfM qM51UBXSCO3m6MSPcL2mHtnf1snwP2Mj32pGyHvHhECLZlA+cyjRGDsm2f6kkd5aMu +KVU6susRmMfX/NMPoTAtJPg= Received: from luna.archlinux.org (luna.archlinux.org [127.0.0.1]) by luna.archlinux.org (Postfix) with ESMTP id 25F332069D for ; Thu, 30 Jan 2020 11:57:02 +0000 (UTC) Received: from orion.archlinux.org (orion.archlinux.org [88.198.91.70]) by luna.archlinux.org (Postfix) with ESMTPS for ; Thu, 30 Jan 2020 11:57:02 +0000 (UTC) Received: from orion.archlinux.org (localhost [127.0.0.1]) by orion.archlinux.org (Postfix) with ESMTP id A5EA21884ABEB4; Thu, 30 Jan 2020 11:56:52 +0000 (UTC) Received: from localhost (unknown [IPv6:2a02:8070:24e4:b800:b66b:fcff:fe3e:6273]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: lfleischer) by orion.archlinux.org (Postfix) with ESMTPSA id 838441884ABEB3; Thu, 30 Jan 2020 11:56:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=archlinux.org; s=orion; t=1580385412; bh=6ktBTB05zn0biCrnzdLA8yho/wsoVYk7SK0I4PhZJeA=; h=From:To:Cc:Subject:Date; b=x6VPboLIlhIFU8ry3UaJV87LulTWqUJNdJ0yStJZmSvfL+7gN7M3LUMSCaU0k6CLB +wpMi7sZ8yFfOk8U1kYyiLSLrCGqvbmwANbYKKZbuen//sMDwtQNu9zh7duaprKEKW jyzNvaw2jNYMZSeIOQvwWNQ1kLyIOu7wnzdgSr8Fu5y43FMccmHYG0YT8sxqTGkow4 tzLiSS3LZL1On9nDj1bseeJ+GIriIIZuZ7C4zW+zjNruqdT6z9P25DRztTgOIco+te hKjA+YT3SAOAnKqUpqx+B/4ZN0Sb+puR/EPwW++idA18a+66vGPWKUF6VWf2pcebrm AoU8YuFg/1l0it+k+ZKsFocVU08sQjQV5zzvcmhBEiyzD/hDAWJybHNMP4rBcrn/Dc LgvJbf+TTtcuhOdF/ZgGcjSe3WAVfXT1kkImQ0biBDCgBtjc9l5ElP3U/NGtxbI9O+ /EpgSYqvXqdil7+WGuSEQ8Z8/++JIsiDgq2Gdu082WYMewOsLhDrqtN68kf8vU8g/g YtBstuTrqJm3r44d2Vv8BwnBpntP43WELKlOg9GE4+FtAneM4AMA59ZX1enlS/vek5 yAeuHCM2v2XXEjjBDUbOVTFoNsz7qo7A1VWeOqPgbObu+74F9LnCTBAGdp8/LTMuq4 bQuu4hLRDF6IZ7L3s30qIl5A= From: Lukas Fleischer To: aur-dev@archlinux.org Subject: [PATCH] rendercomment: respectful linkification of Git commits Date: Thu, 30 Jan 2020 12:57:00 +0100 Message-Id: <20200130115700.19185-1-lfleischer@archlinux.org> X-Mailer: git-send-email 2.25.0 MIME-Version: 1.0 X-BeenThere: aur-dev@archlinux.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Arch User Repository \(AUR\) Development" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: aur-dev-bounces@archlinux.org Sender: "aur-dev" From: Frédéric Mangano-Tarumi Turn the git-commits markdown processor into an inline processor, which is smart enough not to convert Git hashes contained in code blocks or links. Signed-off-by: Lukas Fleischer --- aurweb/scripts/rendercomment.py | 36 ++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/aurweb/scripts/rendercomment.py b/aurweb/scripts/rendercomment.py index 5e18fd5..5c59748 100755 --- a/aurweb/scripts/rendercomment.py +++ b/aurweb/scripts/rendercomment.py @@ -40,19 +40,26 @@ class FlysprayLinksExtension(markdown.extensions.Extension): md.preprocessors.add('flyspray-links', preprocessor, '_end') -class GitCommitsPreprocessor(markdown.preprocessors.Preprocessor): - _oidre = re.compile(r'(\b)([0-9a-f]{7,40})(\b)') +class GitCommitsInlineProcessor(markdown.inlinepatterns.InlineProcessor): + """ + Turn Git hashes like f7f5152be5ab into links to AUR's cgit. + + Only commit references that do exist are linkified. Hashes are shortened to + shorter non-ambiguous prefixes. Only hashes with at least 7 digits are + considered. + """ + _repo = pygit2.Repository(repo_path) - _head = None def __init__(self, md, head): self._head = head - super(markdown.preprocessors.Preprocessor, self).__init__(md) + super().__init__(r'\b([0-9a-f]{7,40})\b', md) - def handleMatch(self, m): - oid = m.group(2) + def handleMatch(self, m, data): + oid = m.group(1) if oid not in self._repo: - return oid + # Unkwown OID; preserve the orginal text. + return None, None, None prefixlen = 12 while prefixlen < 40: @@ -60,13 +67,10 @@ class GitCommitsPreprocessor(markdown.preprocessors.Preprocessor): break prefixlen += 1 - html = '[`' + oid[:prefixlen] + '`]' - html += '(' + commit_uri % (self._head, oid[:prefixlen]) + ')' - - return html - - def run(self, lines): - return [self._oidre.sub(self.handleMatch, line) for line in lines] + el = markdown.util.etree.Element('a') + el.set('href', commit_uri % (self._head, oid[:prefixlen])) + el.text = markdown.util.AtomicString(oid[:prefixlen]) + return el, m.start(0), m.end(0) class GitCommitsExtension(markdown.extensions.Extension): @@ -77,8 +81,8 @@ class GitCommitsExtension(markdown.extensions.Extension): super(markdown.extensions.Extension, self).__init__() def extendMarkdown(self, md, md_globals): - preprocessor = GitCommitsPreprocessor(md, self._head) - md.preprocessors.add('git-commits', preprocessor, '_end') + processor = GitCommitsInlineProcessor(md, self._head) + md.inlinePatterns.add('git-commits', processor, '_end') class HeadingTreeprocessor(markdown.treeprocessors.Treeprocessor):