Private/Compare-GhostPostFile.ps1
|
function Compare-GhostPostFile { <# .SYNOPSIS Says what an export has to do with the file of a post or a page: write it, save its images or neither. .DESCRIPTION Export-GhostPost and Export-GhostPage use this for every post or page, before they change anything. The file of a post can be written in two forms, see ConvertTo-GhostPostFile: with every image as its address, or with an uploaded image as the path of a file in the folder images next to the post file. A file in either form is the post. So is a file in between, with a path for the images that have a file and an address for the others, which is what an export leaves behind when an image could not be saved. A post that came from a file has the addresses that Sync-GhostPost uploaded the image files of that file to. Those addresses are written as the paths the file has, so that such a file is the post too. A file can also have a link to a page of the same site without the address of the site, where the post has that link with the address. Test-GhostSiteLinkMatch says whether that is all the difference. Such a file is the post as long as nothing has to be done for it. It is never written again without -Force, because that would put the address in its links. So when -IncludeImage would have to write it or save an image for it, the file counts as one that differs. The answer has the action for the file as it is now: - Created: there is no file. It is to be written. - Unchanged: the file is the post, in one of the forms. With -IncludeImage the images that have no file yet are to be saved, and the file is to be written again when it has an address where a path belongs. - Updated: the file differs and -Force says to write it. - Skipped: the file differs and is left alone. Save-GhostPostFile does what the answer says. .EXAMPLE Compare-GhostPostFile -Post $post -Path $file -Fields $fields -IncludeImage Returns the action, the text to write and the images to save for that post and that file. #> [CmdletBinding()] [OutputType([pscustomobject])] param( # The post or the page, read with the formats lexical and html and with its tags. [Parameter(Mandatory)] [object] $Post, # Full path of the file of the post. [Parameter(Mandatory)] [string] $Path, # The fields the front matter may set, as Read-GhostPostFile takes them, to read the file that is there. [Parameter(Mandatory)] [System.Collections.IDictionary] $Fields, # Keep the uploaded images as files next to the post file. [switch] $IncludeImage, # Write a file that differs. [switch] $Force, # The addresses of the site, as Get-GhostSiteUrl returns them, for a link the file has without them. [string[]] $SiteUrl = @() ) $directory = [System.IO.Path]::GetDirectoryName($Path) $existing = $null $known = [System.Collections.Generic.Dictionary[string, string]]::new([System.StringComparer]::Ordinal) if ([System.IO.File]::Exists($Path)) { $existing = [System.IO.File]::ReadAllText($Path, [System.Text.Encoding]::UTF8) -replace '\r\n?', "`n" # The image files of this file that the post on the site already has, under the name they were # uploaded with. try { $source = Read-GhostPostFile -Path $Path -Fields $Fields foreach ($image in Get-GhostPostImage -Source $source -Post $Post) { if ($image.Url -and -not $known.ContainsKey($image.Url)) { $known[$image.Url] = $image.Destination } } } catch { Write-Debug -Message "The images of $Path are not compared: $($_.Exception.Message)" } } $plain = ConvertTo-GhostPostFile -Post $Post $full = ConvertTo-GhostPostFile -Post $Post -IncludeImage -KnownImage $known $missing = @( foreach ($image in $full.Images) { $file = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($directory, $image.Path)) if (-not [System.IO.File]::Exists($file)) { [pscustomobject]@{ Url = $image.Url; Path = $file } } } ) # The form in between: a path for the images that have a file, an address for the others. $partial = $full if ($missing.Count -gt 0) { $addresses = @($missing | ForEach-Object { $_.Url }) $partial = ConvertTo-GhostPostFile -Post $Post -IncludeImage -KnownImage $known -ExcludeImage $addresses } $text = $plain.Text $images = @() if ($IncludeImage) { $text = $full.Text $images = $missing } # A file that only differs in the address of the site in a link, from a form that leaves nothing to do. $forms = @($plain.Text, $full.Text, $partial.Text) $alike = $false if ($null -ne $existing -and $forms -cnotcontains $existing -and $images.Count -eq 0) { $settled = $forms if ($IncludeImage) { $settled = @($full.Text) } foreach ($form in $settled) { if (Test-GhostSiteLinkMatch -Text $existing -Stored $form -SiteUrl $SiteUrl) { $alike = $true break } } } $write = $false if ($null -eq $existing) { $action = 'Created' $write = $true } elseif ($forms -ccontains $existing) { # The post, but with -IncludeImage perhaps not with a path for every image yet. $action = 'Unchanged' $write = $IncludeImage -and $existing -cne $full.Text } elseif ($alike) { $action = 'Unchanged' } elseif ($Force) { $action = 'Updated' $write = $true } else { $action = 'Skipped' $images = @() } [pscustomobject]@{ Action = $action Write = $write Path = $Path Existing = $existing Text = $text Content = $plain.Content Images = @($images) Known = $known } } |