当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 星期五有趣 文本转 HTML

作者:精品下载站 日期:2024-12-14 07:43:23 浏览:11 分类:玩电脑

星期五有趣 文本转 HTML


[玩转系统] 星期五有趣 文本转 HTML

我的想法是获取一个文本文件(例如脚本),并使用交替阴影行的样式将其转换为 HTML 文档。我已经从其他一些脚本中获得了样式标头,因此该部分很简单。我的第一次尝试是简单地获取文件内容并转换为 HTML。

get-content .\XMLDemo.txt | convertto-html

但如果你在家尝试的话,你会发现一个问题。 ConvertTo-HTML 正在寻找一组对象属性,而文件的内容没有任何属性。或者换句话说,我需要将文件的内容变成一个对象。因为我知道我想要包含行号,所以我想我可以使用 PowerShell 3.0 中引入的 [pscustomobject] 类型。

get-content .\XMLDemo.txt | foreach -begin {$i=0} -process { 
#create a custom object out of each line of text
$i++
[pscustomobject]@{Line=$i;" "=$_}
} 

运行时,每行都会变成一个自定义对象,其中包含行号属性和每行值属性。通常,您需要为您的属性命名,但在本例中我不需要属性名称,因此我使用空白。你最终会明白为什么。现在我可以将其通过管道传输到 ConvertTo-HTML 并获得我期望的输出。这就是剧本的关键。这是完整的函数,ConvertTo-HTMLListing

#requires -version 3.0

Function ConvertTo-HTMLListing {

<#
.Synopsis
Convert text file to HTML listing
.Description
This command will take the contents of a text file and create an HTML document complete with line numbers.

There are options to suppress the line numbers and to skip any blank lines. The command is intended to convert one file at a time although you can pipe a file name to the command.
.Example
PS C:\> ConvertTo-HTMLListing -path c:\scripts\myscript.ps1 | out-file d:\MyScript.htm
.Example
PS C:\> dir c:\work\myfile.ps1 | ConvertTo-HTMLListing | Out-file d:\myfile.htm
.Example
PS C:\> foreach ($file in (dir c:\work\*.txt)) { ConvertTo-HTMLListing $file.fullname | Out-File D:$($File.basename).htm }

Create an HTML file for each text file in C:\work.
.Notes
Last Updated: 8/22/2014
Version     : 0.9

Learn more:
 PowerShell in Depth: An Administrator's Guide (http://www.manning.com/jones6/)
 PowerShell Deep Dives (http://manning.com/hicks/)
 Learn PowerShell in a Month of Lunches (http://manning.com/jones3/)
 Learn PowerShell Toolmaking in a Month of Lunches (http://manning.com/jones4/)
 
  ****************************************************************
  * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
  * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.  IF   *
  * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
  * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.             *
  ****************************************************************

.Link
https://jdhitsolutions.com/blog/2014/08/friday-fun-text-to-html

.Link
ConvertTo-HTML
#>
[cmdletbinding()]
Param(
[Parameter(Position=0,Mandatory,HelpMessage="Enter the path to the file",
ValueFromPipeline,ValueFromPipelineByPropertyName)]
[alias("PSPath")]
[ValidateScript({Test-Path $_})]
[string]$Path,
[switch]$SkipBlankLines,
[switch]$NoLineNumber
)

Begin {
    Write-Verbose -Message "Starting $($MyInvocation.Mycommand)"  

    $head = @'
<Title>Script Listing</Title>
<style>
body { background-color:#FFFFFF;
       font-family:Consolas;
       font-size:12pt; }
td, th { border:0px solid black; 
         border-collapse:collapse; }
th { color:white;
     background-color:black; }
table, tr, td, th { padding: 0px; margin: 0px }
tr:nth-child(odd) {background-color: lightgray}
table { margin-left:25px; }
h2 {
 font-family:Tahoma;
}
.footer 
{ color:green; 
  margin-left:25px; 
  font-family:Tahoma
}

</style>
'@

} #begin

Process {
    $file = Resolve-Path -Path $Path
    Write-Verbose "Processing $($file.providerpath)"
    $body = "<H2>$($file.providerpath)</H2>"
    $content = Get-Content -Path $file

    if ($SkipBlankLines) {
        #filter out blank lines
        Write-Verbose "Skipping blanks"
        $content = $content | where {$_ -AND $_ -match "\w"}
    }

    Write-Verbose "Converting text to objects"
    $processed = $content | foreach -begin {$i=0} -process { 
    #create a custom object out of each line of text
    $i++
    [pscustomobject]@{Line=$i;" "=$_}
    } 

    if ($NoLineNumber) {
      $processed = $processed | Select " "
    }

    Write-Verbose "Creating HTML"
    $body+= $processed | ConvertTo-Html -Fragment | foreach {
    #convert spaces to HTML spaces
    $_.replace(" ","&nbsp;")
    }

    $post = "<br><div class='footer'>$(Get-Date)</div>"
    #create the HTML output and write to the pipeline
    ConvertTo-HTML -Head $head -Body $body -PostContent $post 
} #process

End {    
    Write-Verbose -Message "Ending $($MyInvocation.Mycommand)"
} #end

} #end function

该函数采用文件名并将文本内容转换为 HTML 文档。

[玩转系统] 星期五有趣 文本转 HTML

该功能包括一个嵌入的样式表,它允许我为不同的部分以及交替的行使用不同的字体。唯一的另一个“技巧”是我用 HTML 等效内容替换原始文本中的空格。

 $body+= $processed | ConvertTo-Html -Fragment | foreach {
    #convert spaces to HTML spaces
    $_.replace(" ","&nbsp;")
    }

这就是为什么 PowerShell cmdlet 只做一件事很有帮助。如果 ConvertTo-HTML 自动写入文件,我就必须跳过更多的环节。但事实并非如此。该函数也仅将 HTML 写入管道。您仍然需要将其通过管道传输到 Out-File 进行保存。基于注释的帮助中有一些示例。

您会在输出中注意到该表有一个行号标题,但没有任何文本行。这就是为什么我在 pscustomobject 中使用空格作为属性名称。

我唯一拥有的其他功能是跳过空白行和行号的选项。

现在,在您开始说“是的,但是……怎么样”之前,我知道还有其他技术和 ISE 插件可以将脚本转换为完整的彩色 HTML 文件。您也可以简单地在浏览器中打开该文件并将其显示为文本。我的功能实际上是作为一种教学工具,尽管拥有一个基于 HTML 的 PowerShell 脚本库可能会很酷。

这有趣吗?你学到新东西了吗?如果没有的话,下周总是有的。周末愉快。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯