generate-views-doc.ps1 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # Script to generate views.md from API documentation
  2. param(
  3. [string]$ApiPath = "api",
  4. [string]$OutputPath = "docs/views.md",
  5. [switch]$Debug
  6. )
  7. # Ensure we're in the correct directory (docfx root)
  8. $scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
  9. $rootPath = Split-Path -Parent $scriptPath
  10. Set-Location $rootPath
  11. Write-Host "Working directory: $(Get-Location)"
  12. Write-Host "Looking for view files in: $ApiPath"
  13. # Get all .yml files in the API directory that are Views
  14. if ($Debug) {
  15. $viewFiles = Get-ChildItem -Path $ApiPath -Filter "Terminal.Gui.Views.HexView.yml"
  16. Write-Host "DEBUG MODE: Only processing" $viewFiles -ForegroundColor Cyan
  17. } else {
  18. $viewFiles = Get-ChildItem -Path $ApiPath -Filter "Terminal.Gui.Views.*.yml"
  19. }
  20. Write-Host "Found $($viewFiles.Count) view files"
  21. # Start building the markdown content
  22. $content = @"
  23. <!--
  24. This file is automatically generated from the API documentation and the `OutputView` tool found in the
  25. `.docfx/scripts/OutputView` folder.
  26. `OutputView` is a simple tool that takes a view name and outputs the view's rendered output to a file,
  27. using `Application.ToString` to capture the view's rendered output.
  28. -->
  29. # Terminal Gui's Built-in Views
  30. *Terminal.Gui* provides the following set of built-in views and controls for building terminal user interfaces:
  31. "@
  32. # Process each view file
  33. $views = @()
  34. foreach ($file in $viewFiles) {
  35. try {
  36. $yml = Get-Content $file.FullName -Raw | ConvertFrom-Yaml
  37. # Check if this is actually a View type
  38. $isView = $false
  39. if ($yml.items[0].inheritance) {
  40. $isView = $yml.items[0].inheritance -contains "Terminal.Gui.ViewBase.View"
  41. }
  42. if (-not $isView) {
  43. continue
  44. }
  45. # Extract the view name and description
  46. $name = $file.BaseName -replace "^Terminal\.Gui\.Views\.", ""
  47. # Handle generic types
  48. if ($name -match "(.+)-(\d+)$") {
  49. $name = "$($matches[1])\<T\>"
  50. }
  51. $description = $yml.items[0].summary
  52. # Clean up the description
  53. $description = $description -replace "`r`n", " " # Replace newlines with spaces
  54. $description = $description -replace "\s+", " " # Replace multiple spaces with single space
  55. $description = $description.Trim() # Trim leading/trailing whitespace
  56. # Remove duplicate content (only for repeated phrases, not characters)
  57. $description = $description -replace '([^a-zA-Z0-9]+)\1+', '$1'
  58. # Clean up HTML tags
  59. $description = $description -replace '<p>|</p>', '' # Remove paragraph tags
  60. $description = $description -replace '<a href="[^"]+">([^<]+)</a>', '$1' # Remove links but keep text
  61. # Convert ALL xref tags to markdown links
  62. $description = $description -replace '<xref href="([^"]+)"[^>]*>([^<]+)</xref>', '[$2](~/api/$1.yml)'
  63. $description = $description -replace '<see cref="([^"]+)"/>', '[$1](~/api/$1.yml)'
  64. $description = $description -replace '<c>([^<]+)</c>', '`$1`'
  65. # Convert code tags to backticks
  66. $description = $description -replace '<code>([^<]*)</code>', '`$1`'
  67. # Fix any remaining xref tags
  68. $description = $description -replace '<xref href="([^"]+)"[^>]*></xref>', '[$1](~/api/$1.yml)'
  69. # Extract just the class name from full type names
  70. $description = $description -replace '\[Terminal\.Gui\.Views\.([^\]]+)\]', '[$1]'
  71. $description = $description -replace '\[Terminal\.Gui\.ViewBase\.([^\]]+)\]', '[$1]'
  72. $description = $description -replace '\[Terminal\.Gui\.Drawing\.([^\]]+)\]', '[$1]'
  73. $description = $description -replace '\[Terminal\.Gui\.Input\.([^\]]+)\]', '[$1]'
  74. $description = $description -replace '\[System\.([^\]]+)\]', '[$1]'
  75. # Get the view output
  76. $viewOutput = ""
  77. try {
  78. $viewName = $file.BaseName -replace "^Terminal\.Gui\.Views\.", ""
  79. $tempFile = [System.IO.Path]::GetTempFileName()
  80. Write-Host "Running: dotnet run --project scripts/OutputView --view=$viewName --output=$tempFile" -ForegroundColor Cyan
  81. dotnet run --project scripts/OutputView --view=$viewName --output=$tempFile
  82. if (Test-Path $tempFile) {
  83. $output = Get-Content $tempFile -Raw
  84. if ($output -and $output.Trim()) {
  85. $lines = $output.Trim() -split "`n"
  86. $trimmedLines = $lines | ForEach-Object { $_.TrimEnd() }
  87. $viewOutput = "``````text" + "`n" + $($trimmedLines -join "`n") + "`n" + "``````"
  88. }
  89. Write-Host "View output: $viewOutput" -ForegroundColor Blue
  90. } else {
  91. Write-Host "Temp file was not created!" -ForegroundColor Red
  92. }
  93. if (-not $viewOutput) {
  94. Write-Host " No output generated for $($file.Name)" -ForegroundColor Yellow
  95. }
  96. }
  97. catch {
  98. Write-Host " Error running OutputView for $($file.Name): $_" -ForegroundColor Red
  99. }
  100. Write-Host "Found view: $name"
  101. $views += "## [$name](~/api/$($file.BaseName).yml)`n`n$description`n`n$viewOutput`n"
  102. }
  103. catch {
  104. Write-Host " Error processing $($file.Name): $_" -ForegroundColor Red
  105. Write-Host " YAML content:"
  106. Write-Host (Get-Content $file.FullName -Raw)
  107. }
  108. }
  109. Write-Host "Sorting views..."
  110. # Sort the views alphabetically
  111. $views = $views | Sort-Object
  112. Write-Host "Generating markdown..."
  113. # Add the views to the content
  114. $content += "`n" + ($views -join "`n")
  115. Write-Host "Writing to $OutputPath..."
  116. # Write the content to the output file
  117. $content | Set-Content -Path $OutputPath -NoNewline
  118. Write-Host "Generated $OutputPath successfully" -ForegroundColor Green