docx_preview.ps1 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # docx_preview.ps1 — Windows mirror of docx_preview.sh
  2. # Preview DOCX content as plain text (pandoc preferred, falls back to raw word/document.xml).
  3. #Requires -Version 5.1
  4. [CmdletBinding()]
  5. param(
  6. [Parameter(Mandatory, Position = 0)]
  7. [string]$InputPath
  8. )
  9. $ErrorActionPreference = 'Stop'
  10. # Force UTF-8 console code page first (chcp 65001) so child native processes (pandoc, tar.exe)
  11. # see UTF-8 too. PS-only [Console]::OutputEncoding alone is not enough — child native processes
  12. # inherit the chcp value, not PS's view of stdout.
  13. try { & chcp.com 65001 *> $null } catch { }
  14. [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
  15. [Console]::InputEncoding = [System.Text.Encoding]::UTF8
  16. $OutputEncoding = [System.Text.UTF8Encoding]::new()
  17. if (-not (Test-Path -LiteralPath $InputPath -PathType Leaf)) {
  18. Write-Host "Error: File not found: $InputPath" -ForegroundColor Red
  19. exit 1
  20. }
  21. $resolved = (Resolve-Path -LiteralPath $InputPath).Path
  22. $item = Get-Item -LiteralPath $resolved
  23. $sizeKb = [math]::Round($item.Length / 1KB, 1)
  24. $base = Split-Path -Leaf $resolved
  25. Write-Host "=== DOCX Preview: $base ==="
  26. Write-Host "File size: ${sizeKb} KB"
  27. if (Get-Command pandoc -ErrorAction SilentlyContinue) {
  28. # 2>$null discards pandoc's stderr; capture stdout only.
  29. $content = & pandoc -f docx -t plain $resolved 2>$null
  30. # pandoc exit 0 = success even when the body is empty (cover-only DOCX). Don't truthy-check $content.
  31. if ($LASTEXITCODE -eq 0) {
  32. $joined = ($content -join "`n")
  33. # Word count: split on whitespace and filter empties
  34. $words = ($joined -split '\s+' | Where-Object { $_ }).Count
  35. $pages = [math]::Ceiling($words / 250)
  36. Write-Host "Word count: $words"
  37. Write-Host "Estimated pages: $pages"
  38. Write-Host "---"
  39. Write-Host $joined
  40. exit 0
  41. }
  42. Write-Host "(pandoc invocation failed (exit=$LASTEXITCODE), falling back to raw XML extract)" -ForegroundColor Yellow
  43. } else {
  44. Write-Host "(pandoc not available, falling back to raw XML extract)" -ForegroundColor Yellow
  45. }
  46. Write-Host "---"
  47. # Fallback: open the .docx (zip) and dump word/document.xml first 100 lines.
  48. $tmpDir = Join-Path $env:TEMP ("docx-preview-" + [System.IO.Path]::GetRandomFileName())
  49. New-Item -ItemType Directory -Path $tmpDir -Force | Out-Null
  50. try {
  51. # Prefer tar.exe (Windows 10+) — handles .zip natively, faster than Expand-Archive.
  52. if (Get-Command tar.exe -ErrorAction SilentlyContinue) {
  53. & tar.exe -xf $resolved -C $tmpDir 'word/document.xml' 2>$null
  54. } else {
  55. # Expand-Archive needs .zip extension; copy with that extension first.
  56. $zipCopy = Join-Path $tmpDir 'doc.zip'
  57. Copy-Item -LiteralPath $resolved -Destination $zipCopy
  58. Expand-Archive -LiteralPath $zipCopy -DestinationPath $tmpDir -Force
  59. }
  60. $docXml = Join-Path $tmpDir 'word\document.xml'
  61. if (Test-Path -LiteralPath $docXml) {
  62. Get-Content -LiteralPath $docXml -TotalCount 100 -Encoding UTF8
  63. } else {
  64. Write-Host "Could not locate word/document.xml inside the package." -ForegroundColor Red
  65. }
  66. } finally {
  67. Remove-Item -LiteralPath $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
  68. }