1+ # Filters are PowerShell functions designed to quickly process pipeline input.
2+
3+ # This file contains filters that are useful throughout the build.
4+
5+ # region PowerShell Specific Filters
6+ filter RequireModule {
7+ <#
8+ . SYNOPSIS
9+ Installs a module if it is not already loaded.
10+ . DESCRIPTION
11+ Installs a PowerShell module if it is not already loaded.
12+ #>
13+ $requirementName = $_
14+ if ($requirementName -is [Management.Automation.ExternalScriptInfo ]) {
15+ $requirementName.ScriptBlock.Ast.ScriptRequirements.RequiredModules.Name |
16+ RequireModule
17+ return
18+ }
19+ if (! $requirementName ) {
20+ return
21+ }
22+ $alreadyLoaded = Import-Module - Name $requirementName - PassThru - ErrorAction Ignore - Global
23+ # If they're not already loaded, we'll install them.
24+ if (-not $alreadyLoaded ) {
25+ Install-Module - AllowClobber - Force - Name $requirementName - Scope CurrentUser
26+ $alreadyLoaded = Import-Module - Name $requirementName - PassThru - ErrorAction Ignore - Global
27+ if ($file.FullName ) {
28+ Write-Host " Installed $ ( $alreadyLoaded.Name ) for $ ( $file.FullName ) "
29+ }
30+ } elseif ($file.FullName ) {
31+ Write-Host " Already loaded $ ( $alreadyLoaded.Name ) for $ ( $file.FullName ) "
32+ }
33+ }
34+ # endregion PowerShell Specific Filters
35+
36+ # region Special Filters
37+ filter content {$Content }
38+
39+ filter now {[DateTime ]::Now}
40+
41+ filter utc_now {[DateTime ]::UtcNow}
42+
43+ filter today {[DateTime ]::Today}
44+
45+ filter yaml_header_pattern {
46+ [Regex ]::new('
47+ (?<Markdown_YAMLHeader>
48+ (?m)\A\-{3,} # At least 3 dashes mark the start of the YAML header
49+ (?<YAML>(?:.|\s){0,}?(?=\z|\-{3,} # And anything until at least three dashes is the content
50+ ))\-{3,} # Include the dashes in the match, so that the pointer is correct.
51+ )
52+ ' , ' IgnoreCase, IgnorePatternWhitespace' )
53+ }
54+
55+ filter yaml_header {
56+ $in = $_
57+ if ($in -is [IO.FileInfo ]) {
58+ $in | Get-Content - Raw | yaml_header
59+ return
60+ }
61+ foreach ($match in (yaml_header_pattern).Matches(" $in " )) {
62+ $match.Groups [' YAML' ].Value | from_yaml
63+ }
64+ }
65+
66+ filter from_yaml {
67+ $in = $_
68+ " YaYaml" | RequireModule
69+ $in | ConvertFrom-Yaml
70+ }
71+
72+ filter to_yaml {
73+ $in = $_
74+ " YaYaml" | RequireModule
75+ $in | ConvertTo-Yaml
76+ }
77+
78+ filter to_json {
79+ $in = $_
80+ $in | ConvertTo-Json - Depth 10
81+ }
82+
83+ filter strip_yaml_header {
84+ $in = $_
85+ if ($in -is [IO.FileInfo ]) {
86+ $in | Get-Content - Raw | strip_yaml_header
87+ return
88+ }
89+ $in -replace (yaml_header_pattern)
90+ }
91+
92+ filter from_markdown {
93+ $in = $_
94+ if ($in -is [IO.FileInfo ]) {
95+ $in | Get-Content - Raw | from_markdown
96+ return
97+ }
98+ @ (
99+ $in |
100+ strip_yaml_header |
101+ ConvertFrom-Markdown |
102+ Select-Object - ExpandProperty Html
103+ ) -replace ' disabled="disabled"'
104+ }
105+
106+ # endregion Special Filters
107+
108+ # region Custom Filters
109+ <#
110+ Put any function or filter you want here.
111+
112+ (if you want to add more filters inline but don't want to modify any of the other filters)
113+ #>
114+ # endregion Custom Filters
0 commit comments