【问题标题】:How to convert Github-style Wiki page link to Markdown-style link in Bash script如何在 Bash 脚本中将 Github 风格的 Wiki 页面链接转换为 Markdown 风格的链接
【发布时间】:2021-11-17 06:12:52
【问题描述】:

关于 Stack Overflow 的第一个问题。

我正在尝试编写一个 Bash 脚本来将为其他内部 Github Wiki 页面生成的 Github Wiki 链接类型转换为传统的 Markdown 样式链接。

Github Wiki 链接字符串如下所示:

[[An example of another page]]

我想把它变成这样:

[An example of another page](An-example-of-another-page.htm)

文档中有未知数量的这些链接,我不知道内容。

目前我一直在尝试解决其他问题的单行 sed 解决方案,比如这个:

https://askubuntu.com/questions/1283471/inserting-text-to-existing-text-within-brackets

...绝对没有成功。我什至不知道从哪里开始。

谢谢。

【问题讨论】:

  • 编辑:为了澄清,我需要保留属于同一字符串的正常 Markdown 链接(例如[link 1](link1.htm))。所以:[link 1](link1.htm) and [[link 2]] 变为 [link 1](link1.htm) and [link 2](link-2.htm)

标签: bash github sed markdown wiki


【解决方案1】:

你可以试试这个sed

$ sed -E 's/\[(.[^]]*)\]/\1/g;s/\[(.[^]]*)]/&(\1)/g;:jump s/(\([^ \)]*)[ ]/\1-/;tjump' input_file
[An example of another page](An-example-of-another-page)

s/\[(.[^]]*)\]/\1/g - 删除括号[]

s/\[(.[^]]*)]/&(\1)/g - 复制括号内的内容[],返回匹配&,然后操作匹配并添加括号(\1)

:jump s/(\([^ \)]*)[ ]/\1-/;tjump - 创建标签jump,匹配括号内的空格并替换为-

【讨论】:

  • 谢谢。这是一个有益的开始。您的解决方案的问题在于它去除了预先存在的 Markdown 链接。因此,如果您在一个流中有[link 1](link1.html) and [[link ]],它会正确地将后者更改为降价链接,但会从前者中去除格式:link 1(link1.htm) 我已经在原始问题中澄清了这一点。
【解决方案2】:

您可以使用 bash 的内部正则表达式支持来查找链接到 [[text]] 的 wiki 实例并将其替换为 [text](text.htm)。您要使用的模式是\[\[([^\]]*)\]\]

  • \[\] - 转义左右方括号,这样它们就不会被解释为让您匹配字符类的元字符

  • ([^\]]*) 捕获双括号内的所有文本,直到第一个右方括号

您可以从那里评估此正则表达式并使用$BASH_REMATCH 数组来提取和操作文本。您需要多次运行以匹配字符串中的所有实例,然后使用 /// 运算符替换内联字符串。

这是一个示例脚本:

#!/usr/bin/env bash

wiki_string="Now, this is [[a story]] all about how
My life [[got flipped-turned upside down]]
And I'd [[like to take a minute]]
Just [[sit]] right there
I'll [[tell you]] how I [[became the prince]] of a town called Bel-Air"

printf 'Original: %s\n' "$wiki_string"

# find each instance of [[text]] and capture the text inside 
# the square brackets
# if successful, BASH_REMATCH will contain the matched text and the
# captured value inside the parentheses
while [[ "$wiki_string" =~ \[\[([^\]]*)\]\] ]]; do
    # escape the [ and ] characters so we can replace [[text]]
    # with our modified value
    replace_text="${BASH_REMATCH[0]}"
    replace_text="${replace_text/\[\[/\\[\\[}"
    replace_text="${replace_text/\]\]/\\]\\]}"

    # Get the matched value inside the brackets
    link_text="${BASH_REMATCH[1]}"

    # store another copy of the text with the spaces replaced
    # with dashes and appending .htm
    link_target="${link_text// /-}.htm"

    # Finally, replace the matched [[text]] with [text](text.htm)
    wiki_string="${wiki_string//$replace_text/[$link_text]($link_target)}"
done

printf '\nUpdated: %s\n' "$wiki_string"

【讨论】:

  • 感谢您的回答。它非常有用。现在我希望 Hatless 的sed 答案可以通过,因为它更适合我目前拥有的简洁脚本。不过,我会回到你的那里研究它,因为那里有很多我不知道可以完成的事情。
【解决方案3】:

感谢 HatLess 为我改编的答案。下面的 sn-p 将 Github 样式的链接转换为 Markdown 样式的链接,没有 HatLess 的解决方案存在的两个问题。具体来说,这不会破坏预先存在的 Markdown 样式链接,也不会用括号内的连字符替换空格除非链接的一部分。

sed -E 's/\[\[(.[^]]*)]]/&(support\-\1\.htm)/g;:jump s/(]\([^ \)]*)[ ]/\1-/;tjump;s/\[\[/\[/g;s/]]\(/]\(/g' | pandoc -t html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    • 2014-10-24
    • 2016-01-06
    • 2014-04-03
    • 2015-12-02
    • 1970-01-01
    相关资源
    最近更新 更多