Skip to content

Commit

Permalink
Adds NoPageLayoutStyle to HTML output options
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
iainbrighton committed May 25, 2016
1 parent db29320 commit ed0abb9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 13 deletions.
17 changes: 17 additions & 0 deletions Examples/Example30.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Import-Module PScribo -Force;

$example30 = Document -Name 'PScribo Example 30' {
<#
When sending PScribo output as a HTML email body, Outlook does not necessarily
display the page styling/look-and-feel correctly. This can be suppressed by
passing an -Options hashtable with the 'NoPageLayoutStyle' key to the
Export-Document function.
#>
GlobalOption -Margin 18
Style -Name StoppedService -Color White -BackgroundColor Firebrick

$services = Get-Service
$services | Where-Object { $_.Status -ne 'Running' } | Set-Style -Style 'StoppedService'
Table -InputObject $services -Columns Name,DisplayName,Status -Headers 'Name','Display Name','State'
}
$example30 | Export-Document -Format Html -Path ~\Desktop -Options @{ NoPageLayoutStyle = $true }
7 changes: 6 additions & 1 deletion Functions/Export-Document.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@
#try {
## Dynamically generate the output format function name
$outputFormat = 'Out{0}' -f $f;
& $outputFormat -Document $Document -Path $Path; # -ErrorAction Stop;
if ($PSBoundParameters.ContainsKey('Options')) {
& $outputFormat -Document $Document -Path $Path -Options $Options; # -ErrorAction Stop;
}
else {
& $outputFormat -Document $Document -Path $Path; # -ErrorAction Stop;
}
#}
#catch [System.Management.Automation.CommandNotFoundException] {
# Write-Warning ('Output format "{0}" is unsupported.' -f $f);
Expand Down
20 changes: 12 additions & 8 deletions Plugins/OutHtml.Internal.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,23 @@
[OutputType([System.String])]
param (
## PScribo document styles
[Parameter(Mandatory, ValueFromPipeline)] [System.Collections.Hashtable] $Styles,
[Parameter(Mandatory, ValueFromPipelineByPropertyName)] [System.Collections.Hashtable] $Styles,
## PScribo document tables styles
[Parameter(Mandatory, ValueFromPipeline)] [System.Collections.Hashtable] $TableStyles
[Parameter(Mandatory, ValueFromPipelineByPropertyName)] [System.Collections.Hashtable] $TableStyles,
## Suppress page layout styling
[Parameter(ValueFromPipelineByPropertyName)] [System.Management.Automation.SwitchParameter] $NoPageLayoutStyle
)
process {
$stylesBuilder = New-Object -TypeName 'System.Text.StringBuilder';
[ref] $null = $stylesBuilder.AppendLine('<style type="text/css">');
## Add HTML page layout styling options
[ref] $null = $stylesBuilder.AppendLine('html { height: 100%; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; background: #f8f8f8; }');
[ref] $null = $stylesBuilder.Append("page { background: white; width: $($Document.Options['PageWidth'])mm; display: block; margin-top: 1em; margin-left: auto; margin-right: auto; margin-bottom: 1em; ");
[ref] $null = $stylesBuilder.AppendLine('border-style: solid; border-width: 1px; border-color: #c6c6c6; }');
[ref] $null = $stylesBuilder.AppendLine('@media print { body, page { margin: 0; box-shadow: 0; } }');
[ref] $null = $stylesBuilder.AppendLine('hr { margin-top: 1.0em; }');
if (-not $NoPageLayoutStyle) {
## Add HTML page layout styling options, e.g. when emailing HTML documents
[ref] $null = $stylesBuilder.AppendLine('html { height: 100%; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; background: #f8f8f8; }');
[ref] $null = $stylesBuilder.Append("page { background: white; width: $($Document.Options['PageWidth'])mm; display: block; margin-top: 1em; margin-left: auto; margin-right: auto; margin-bottom: 1em; ");
[ref] $null = $stylesBuilder.AppendLine('border-style: solid; border-width: 1px; border-color: #c6c6c6; }');
[ref] $null = $stylesBuilder.AppendLine('@media print { body, page { margin: 0; box-shadow: 0; } }');
[ref] $null = $stylesBuilder.AppendLine('hr { margin-top: 1.0em; }');
}
foreach ($style in $Styles.Keys) {
## Build style
$htmlStyle = GetHtmlStyle -Style $Styles[$style];
Expand Down
24 changes: 21 additions & 3 deletions Plugins/OutHtml.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,32 @@ InModuleScope 'PScribo' {
}

Describe 'OutHtmlStyle' {
$Document = Document -Name 'Test' -ScriptBlock { };
$text = OutHtmlStyle -Styles $Document.Styles -TableStyles $Document.TableStyles;
## Predominately calls GetHtmlStyle and GetHtmlTableStyle

It 'creates <style> tag.' {
$Document = Document -Name 'Test' -ScriptBlock { };
$text = OutHtmlStyle -Styles $Document.Styles -TableStyles $Document.TableStyles;

$text -match '<style type="text/css">' | Should Be $true;
$text -match '</style>' | Should Be $true;
}

It 'creates page layout style by default' {
$Document = Document -Name 'Test' -ScriptBlock { };
$text = OutHtmlStyle -Styles $Document.Styles -TableStyles $Document.TableStyles;

$text -match 'html {' | Should Be $true;
$text -match 'page {' | Should Be $true;
$text -match '@media print {' | Should Be $true;
}

It "suppresses page layout style when 'Options.NoPageLayoutSyle' specified" {
$Document = Document -Name 'Test' -ScriptBlock { };
$text = OutHtmlStyle -Styles $Document.Styles -TableStyles $Document.TableStyles -NoPageLayoutStyle;

$text -match 'html {' | Should Be $false;
$text -match 'page {' | Should Be $false;
$text -match '@media print {' | Should Be $false;
}
}

Describe 'OutHtmlTable' {
Expand Down
6 changes: 5 additions & 1 deletion Plugins/OutHtml.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ function OutHtml {
process {
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew();
WriteLog -Message ($localized.DocumentProcessingStarted -f $Document.Name);
$noPageLayoutStyle = $false;
if ($Options -and ($Options['NoPageLayoutStyle'])) {
$noPageLayoutStyle = $Options['NoPageLayoutStyle'];
}
[System.Text.StringBuilder] $htmlBuilder = New-Object System.Text.StringBuilder;
[ref] $null = $htmlBuilder.AppendLine('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
[ref] $null = $htmlBuilder.AppendLine('<html xmlns="http://www.w3.org/1999/xhtml">');
[ref] $null = $htmlBuilder.AppendLine('<head><title>{0}</title>' -f $Document.Name);
[ref] $null = $htmlBuilder.AppendLine('{0}</head><body><page>' -f (OutHtmlStyle -Styles $Document.Styles -TableStyles $Document.TableStyles));
[ref] $null = $htmlBuilder.AppendLine('{0}</head><body><page>' -f (OutHtmlStyle -Styles $Document.Styles -TableStyles $Document.TableStyles -NoPageLayoutStyle:$noPageLayoutStyle));
$topMargin = ConvertMmToEm $Document.Options['MarginTop'];
$leftMargin = (ConvertMmToEm $Document.Options['MarginLeft']);
$bottomMargin = (ConvertMmToEm $Document.Options['MarginBottom']);
Expand Down
1 change: 1 addition & 0 deletions en-US/about_Plugins.help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@

# Plugin-specific options
# In their own about file?
HTML: NoPageLayoutStyle [bool]: Suppresses the page layout/look-and-feel style

# Writing a plugin

0 comments on commit ed0abb9

Please sign in to comment.