<# Компиляция html через hhc.exe Version: 1.04.1 Date: 2018-10-31 Copyright: (c) 2018 Smitis Использование: HtmlCompileSinglePage [-File] htmlfile [[-Out] chmfile] [-Title "help title" | -NoTitle] [-Del] [-File] htmlfile - исходный файл [-Out] chmfile - файл chm. По умолчанию будет создан в папке с исходным файлом с именем тем же, что и исходного файла с расширением chm. -Title "help title" - Задать заголовок для окна. Иначе заголовок будет взят из тега <title> -NoTitle - без заголовка -Del - удалить исходные файлы в случае успешной компиляции -Test - временный файл проекта не удаляется, создаётся log-файл Особенности работы: - Папка с исходным файлом будет сделана текущей. - Временный файл проекта создаётся в папке с исходным файлом. - Если есть папка с именем исходного файла и добавлением _files (типичный случай сохранения страницы их браузера), эта папка будет включена в проект. - Для работы нужен hhc.exe и hha.dll - hhc.exe и dll должены находиться в той же папке, что и скрипт (ищются в первую очередь) или в одной из папок PATH. #> <# TODO: - Только формировать hhp-файл (ключ -Test ???) - title может содержать кавычки? - удаление исходников в корзину - для успешности выполнения проверять Error log file=logfile - # Вид ошибок и предупреждений # HHC5003: Error: Compilation failed while compiling Как создать CHM файл_files\anchor.gif. # HHC4006: Warning: The file "Как создать CHM файл.htm" is already listed in the [FILES] section of the project file. #> param ( [Parameter(Position=0)] [Alias('Path','FilePath')] [ValidateNotNullOrEmpty()] [string] $File, [Parameter(Position=1)] [ValidateNotNullOrEmpty()] [string] $Out, [Parameter()] [Alias('T')] [string] $Title, [Parameter()] [Alias('NT')] [switch] $NoTitle, [Parameter()] [switch] $Test ) if ($PSBoundParameters.File -eq $Null) { Write-Host "Usage:" Write-Host ' HtmlCompileSinglePage htmlfile [[-Out] chmfile] [-Title "help title" | -NoTitle]' exit } if (Test-Path -Lit $File) { $File = [System.IO.Path]::GetFullPath($File) } else { $File = [System.IO.Path]::GetFileName($File) Write-Host "File `"$File`" not found" -for Red exit 1 } function Quote($s) { return "`"$s`"" } # заключить в кавычки $scriptpath = $MyInvocation.MyCommand.Path | Split-Path -Parent $hhc = Join-Path $scriptpath "hhc.exe" if (-not (Test-Path -Lit $hhc)) { $hhc = "hhc.exe" } $dir = [System.IO.Path]::GetDirectoryName($File) $name = [System.IO.Path]::GetFileNameWithoutExtension($File) $topic = [System.IO.Path]::GetFileName($File) if ($dir -ne "") { Set-Location -Lit $dir } if (-not $PSBoundParameters.ContainsKey('Out')) { $Out = [System.IO.Path]::GetFileNameWithoutExtension($File) + ".chm" } else { $Out = [System.IO.Path]::GetFileName($Out) } if ([System.IO.Path]::GetExtension($Out) -ne ".chm") { $Out += ".chm" } #$project = [System.IO.Path]::GetTempFileName() #New-TemporaryFile $project = if ($Test) { "${name}.hhp" } else { "~${name}.tmp" } $logfile = if ($Test) { "${name}.log" } else { "" } # Заголовок if ($NoTitle) { $Title = "" } else { if (-not $PSBoundParameters.ContainsKey('Title')) { # Определяем кодировку файла $charset = "Default" $content = Get-Content -Lit $File -Enc Default foreach ( $s in $content ) { if ($s -match '</head>') { break } if ($s -match '<meta http-equiv="?content-type"?[^<>]+charset=([a-z0-9\-"]+)') { $charset = $Matches[1] } if ($s -match '<meta charset=([a-z0-9\-"]+)') { $charset = $Matches[1] } } $charset = if($charset -match '"?UTF-?8"?') {'UTF8'} else {'Default'} # Ищем заголовок в теге <title> $content = Get-Content -Lit $File -Enc $charset foreach ($s in $content) { if ($s -match '</head>') { break } if ($s -match '<title>(.+)</title>') { $Title = $Matches[1] Write-Host "Title: $title" } } } $Title = $Title -replace '"', "'" # ??? #if ($Title -ne "") { $Title = Quote $Title } } # Секция [OPTIONS] $options = @( "Compatibility=1.1 or later" "Compiled file=$Out" "Default window=main" "Title=$Title" "Language=0x419 Русский (Россия)" "Default topic=$topic" "Error log file=$logfile" ) Set-Content -Lit $project -Force -Value "[OPTIONS]" Add-Content -Lit $project -Force -Value $options # Секция [FILES] Add-Content -Lit $project -Value "[FILES]" Add-Content -Lit $project -Value $topic if (Test-Path -Lit "${name}_files") { Get-ChildItem -Lit "${name}_files" -File | foreach { Add-Content -Lit $project -Value (Join-Path "${name}_files" $_.Name) } } # Секция [WINDOWS] $win = @( # 00: Чтобы согласовать нумерацию с 1, не используется "" # 01: Заголовок окна / The title bar text Quote $Title # 02: The TOC file (.hhc) "" # 03: The Index file (.hhk) "" # 04: The Default file Quote $topic # 05: The file shown when the Home button is pressed "" # 06: The file shown when the Jump 1 button is pressed "" # 07: The text of the Jump 1 button "" # 08: The file shown when the Jump 2 button is pressed "" # 09: The text of the Jump 2 button "" # 10: A bit feild of navigation pane styles "0" #"0x03520" # 11: Width of the navigation pane in pixels "300" # 12: A bit field of the buttons to show (количество и вид кнопок панели инструментов) "0" # 13: Initial position of the window on the screen: [left, top, right, bottom] "[50,50,800,600]" # 14: Style Flags. As set in the Win32 SetWindowLong & CreateWindow APIs (WS_...) "" # 15: Extended Style Flags. As set in the Win32 SetWindowLong & CreateWindowEx APIs (WS_EX_...) "" # 16: Window show state. As set in the Win32 ShowWindow API. 1=SW_NORMAL, 3=SW_MAXIMIZE "3" # 17: Whether or not the navigation pane is initially closed. 1=closed, 0=open "" # 18: The default navigation pane. 0=TOC, 1=Index, 2=Search, 3=Favorites, 4=History (not implemented by HH), 5=Author, 11..19=Custom panes "" # 19: Where the navigation pane tabs should be. 0=Top, 1=Left, 2=Bottom & anything else makes the tabs appear to be behind the pane "0" # 20: ID to send in WM_NOTIFY messages. "0" ) Add-Content -Lit $project -Value "[WINDOWS]" Add-Content -Lit $project -Value ("main=" + ($win[1..20] -join ",")) & $hhc $project Write-Host "Press any key to continue . . . " -NoNewLine while ($KeyInfo.VirtualKeyCode -eq $Null) { $KeyInfo = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown") } Write-Host "" if (-not $Test) { Remove-Item -Lit $project } |