【问题标题】:Using PowerShell Core ConvertFrom-Markdown to parse values in a markdown table使用 PowerShell Core ConvertFrom-Markdown 解析降价表中的值
【发布时间】:2023-02-14 06:15:14
【问题描述】:

我对使用 ConvertFrom-Markdown cmdlet 来解析降价表中的值很感兴趣。该 cmdlet 使用 markdig markdown 处理器,它有一个 Abstract Syntax Tree 应该能够为此目的被遍历。

我们如何在以下 powershell sn-p 中搜索/枚举令牌以返回行和列?

(@'
# header1
## header2
| Column1 | Column2 |
| ------- | ------- |
| Row1Column1 | Row1Column2 |
| Row2Column1 | Ro2Column2 |
'@ | ConvertFrom-Markdown).Tokens

我在令牌中看到的值看起来很有希望,我可以在 Parent 字段中看到 Markdig.Extensions.Tables.TableCell,但这是我所能得到的。

【问题讨论】:

  • 使用这个ConvertFrom-SourceTable$YourMD | ConvertFrom-SourceTable
  • 为降价创建变量:$input = @' Your table '@.然后(没有标记): $table = $input | ConvertFrom-Markdown。现在你有了一个 Markdown 表格。代币只会让你得到代币。该 cmdlet 将返回一个 c# 类,因为 PS 是用 c# 编写的。因此,您应该能够调用 c# 类中的任何方法,例如 $table.Parse(sourceText, pipeline)。您可能需要将 PS 对象转换为它们的 c# 类型,例如 $table.Parse([string]sourceText, pipeline)

标签: powershell markdown


【解决方案1】:

这是一种方法。

注意我不确定,例如 Table 是否可以包含仅有的TableRows,所以可能不需要| where-object { ... }

# set up some sample data
$md = @"
# header1
## header2
| Column1 | Column2 |
| ------- | ------- |
| Row1Column1 | Row1Column2 |
| Row2Column1 | Ro2Column2 |
"@ | ConvertFrom-Markdown

# walk the syntax tree
$mdDoc = $md.Tokens;
$mdTables = @( $mdDoc | where-object { $_ -is [Markdig.Extensions.Tables.Table] } ); 
foreach( $mdTable in $mdTables )
{
    write-host "table";
    $mdRows = @( $mdTable | where-object { $_ -is [Markdig.Extensions.Tables.TableRow] } );
    foreach( $mdRow in $mdRows )
    {
        write-host "  row";
        write-host "    header = $($mdRow.IsHeader)";
        $mdCells = @( $mdRow | where-object { $_ -is [Markdig.Extensions.Tables.TableCell] } );
        foreach( $mdCell in $mdCells )
        {
            write-host "    cell";
            $mdInline = $mdCell.Inline;
            write-host "      inline - $($mdInline.Content)";
        }
    }
}

给出以下输出:

table
  row
    header = True
    cell
      inline - Column1
    cell
      inline - Column2
  row
    header = False
    cell
      inline - Row1Column1
    cell
      inline - Row1Column2
  row
    header = False
    cell
      inline - Row2Column1
    cell
      inline - Ro2Column2

希望这足以让你开始......

【讨论】:

    【解决方案2】:

    如果您想将 markdown 表导入 PowerShell 数组,您也可以在此过程中解析和构建 PsCustomObjects ...

    $MarkDown = @"
    # header1
    ## header2
    | Column1 | Column2 |
    | ------- | ------- |
    | Row1Column1 | Row1Column2 |
    | Row2Column1 | Ro2Column2 |
    
    | Table2 Column1 |
    | ------------- |
    | T2 Row1 |
    | T2 Row2 |
    | T2 Row3 |
    "@ | ConvertFrom-Markdown
    
    $mdDoc = $Markdown.Tokens
    [array]$tables = $null
    $mdTables = @($mdDoc | where {$_ -is [Markdig.Extensions.Tables.Table]})
    
    foreach ($mdTable in $mdTables) {
        [array]$table = $null
        $mdRows = @($mdTable | where {$_ -is [Markdig.Extensions.Tables.TableRow]})
    
        foreach ($mdRow in $mdRows) {
            $mdCells = @($mdRow | where-object { $_ -is [Markdig.Extensions.Tables.TableCell]})
            $mdCellsValues = @($mdCells.Inline.Content | foreach {$_.ToString()})
    
            if ($mdRow.IsHeader) {# don't use headers as values
                $CustomProperties = $mdCellsValues
            } else {# iterate throw the customobject and populate it
                $thisrow = New-Object PSCustomObject | select $CustomProperties
                
                foreach ($i in 0..($CustomProperties.Count -1)) {
                    $thisrow.($CustomProperties[$i]) = $mdCellsValues[$i]
                }
    
                $table += $thisrow
            }# endif
    
        }#end tablerows
    
        $tables += ,$table #add each table a sub arrays
        
    }#end tables
    
    $tables
    
    

    结果在两个子数组中可用

    C:> $tables[0]
    
    Column1     Column2
    -------     -------
    Row1Column1 Row1Column2
    Row2Column1 Ro2Column2
    
    C:> $tables[1]
    
    Table2 Column1
    -------------
    T2 Row1
    T2 Row2
    T2 Row3
    

    【讨论】:

      猜你喜欢
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-07
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多