首页 > 解决方案 > Powershell:将 StackOverflow 样式的编码 URL 转换为 HTML

问题描述

我有一个 CSV StackOverflow 样式的编码 URL,需要使用 Powershell 将它们转换为 HTML 并将它们保存回 CSV 的第三列。

CSV 样本:

ID,Comment,CommentConverted
1,Check [this out](https://stackoverflow.com),
2,To buy coffee [click here](https://google.com) or [here](https://bing.com),

我需要的

cID,Comment,CommentConverted
1,Check [this out](https://stackoverflow.com),Check <a href="https://stackoverflow.com">this out</a>    
2,To buy coffee [click here](https://google.com) or [here](https://bing.com),To buy coffee <a href="https://google.com">Click Here</a> or <a href="https://bing.com">here</a>

我在解析该Comment字段中有多个 URL 时遇到问题。

我的(丑陋的,非常冗长的)Powershell 代码

$comment_list = Import-Csv "c:\temp\Comments.csv"
$comment_list.foreach(
{
  $x = $_.Comment
  $linktextOUT = $x.Substring($x.IndexOf('[')+1,$x.IndexOf(']')-$x.IndexOf('[')-1)
  $linktextREPLACE = "[" + $linktextOUT + "]" 
  $URLOUT = $x.Substring($x.IndexOf('(')+1,$x.IndexOf(')')-$x.IndexOf('(')-1)
  $URLREPLACE = "(" + $URLOUT + ")" 
  $output = $x.Replace($URLREPLACE, "")
  $output = $output.Replace($linktextREPLACE, "")
  $output = $output + "<a href=""" + $URL + """>" + $linktext + "</a>"
  Write-Host $_.cID","$_.Comment","$output
})

它输出什么

1 , Check [this out](https://stackoverflow.com) , Check <a href="https://stackoverflow.com">this out</a>    
2 , To buy coffee [click here](https://google.com) or [here](https://bing.com) , To buy coffee  or [here](https://bing.com)<a href="https://google.com">click here</a>

您可以看到第一行很好 - Powershell 输出正确的第三列。但是,它看不到第 2 行中的第二个 URL,因此它完全跳过了“bing.com” URL。请注意,某些行的注释中最多可能有 5 个 URL。

提前感谢任何帮助。

标签: powershellcsv

解决方案


使用-replace运算符可以实现简洁的解决方案:

@'
ID,Comment,CommentConverted
1,Check [this out](https://stackoverflow.com),
2,To buy coffee [click here](https://google.com) or [here](https://bing.com),
'@ > Comments.csv

Import-Csv Comments.csv | ForEach-Object {
  $_.CommentConverted =
    $_.Comment -replace '\[(.+?)\]\((.+?)\)', '<a href="$2">$1</a>'
  $_ # output the modified object
}

正如JosefZ指出的那样,PowerShell (Core) 7+有一个ConvertFrom-Markdown实用程序,可以将 Markdown 输入转换为 HTML 文本(.Html输出对象上的属性)和带有嵌入式VT(虚拟终端)转义序列(属性.VT100EncodedString)的文本,以及 AST(抽象语法树) Markdown 输入文本的表示。

然而,在手头的情况下,它的好处是有限的,因为输入字符串总是包装在<p>HTML 元素中,这需要删除该包装器才能获得所需的结果;因此,在这种特殊情况下,上述基于正则表达式的解决方案不仅更简洁,而且性能更好。

为了完整起见,这是一个基于以下的解决方案ConvertFrom-Markdown

@'
ID,Comment,CommentConverted
1,Check [this out](https://stackoverflow.com),
2,To buy coffee [click here](https://google.com) or [here](https://bing.com),
'@ > Comments.csv

Import-Csv Comments.csv | ForEach-Object {
  $_.CommentConverted = ([xml] ($_.Comment | ConvertFrom-Markdown).Html).p.InnerXml
  $_
}

推荐阅读