Skip to content

Instantly share code, notes, and snippets.

@dylanlangston
Last active April 24, 2020 20:45
Show Gist options
  • Save dylanlangston/3874c0b943d2b9ddff9346f7b78ba9fa to your computer and use it in GitHub Desktop.
Save dylanlangston/3874c0b943d2b9ddff9346f7b78ba9fa to your computer and use it in GitHub Desktop.
Powershell script that takes the API guide and converts it into an api.add file. r86.3 version
# Powershell script that takes the API guide and converts it into an api.add file.
# Thanks to Kai for the idea.
# A lot of this was figured out via trial and error. It's possible that changes will be needed to get this to work with future admin guides.
# r86.3 version
# Path to PDF
$PDFFILE = "C:\Users\dlangston\Desktop\api-xtractor\testr86.3.pdf"
# Uses ITextSharp to get the PDF Text. Lots of examples online and it's free.
# Based on an answer here https://www.reddit.com/r/PowerShell/comments/7yee0i/how_do_i_parse_pdf_text_with_powershell/
# DLL found here https://github.com/itext/itextsharp
if (-not ([System.Management.Automation.PSTypeName]'iTextSharp.Text.Pdf.PdfReader').Type) {
Add-Type -Path "$PSScriptRoot\itextsharp.dll"
}
$Reader = New-Object 'iTextSharp.Text.Pdf.PdfReader' -ArgumentList $PDFFILE
$PdfText = New-Object 'System.Text.StringBuilder'
for ($Page = 1; $Page -le $Reader.NumberOfPages; $Page++) {
$Strategy = New-Object 'iTextSharp.Text.Pdf.Parser.SimpleTextExtractionStrategy'
$CurrentText = [iTextSharp.Text.Pdf.Parser.PdfTextExtractor]::GetTextFromPage($Reader, $Page, $Strategy)
if ($CurrentText -match "^Chapter 3 – ") { # Change to Chapter 6 for the r71 Guide.
$PdfText.AppendLine($CurrentText) | Out-Null
$PdfText.AppendLine("`r`n*******************************`r`n") | Out-Null
}
}
$Reader.Close()
# Create a 2D array from the PDF text.
$arr=@{}
foreach ($line in $($PdfText.ToString() -split "`r`n\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*`r`n")) {
if ($line -match "(?<Name>\w+) Method \n") {
$name = $Matches.Name -replace "\s", ""
}
if ($line -match "Description (?<text>((((\w+ |.)+)\n))+?)(?=(Syntax))") {
$text = $Matches.text -replace "`n", ""
$text = $text -replace "\s{2,}", ""
}
if ($line -match "Return Value (?<text>(.*`n)+)") {
$returns = $Matches.text -replace "`n", ""
$returns = $returns -replace "\s{2,}", ""
}
if ($line -match "Syntax \n \n\w+.(\w+\((?<Syntax>(\w+, \n*)+\w+)\))") {
$syntax = $Matches.Syntax -replace "`n", ""
$syntax = $syntax -replace " ", ""
}
if ($line -match "Part Description \nobject An expression that evaluates to an object of type: PVAction \n(?<Description>(.+\n)*)") {
$description = $Matches.Description -replace "Return Value `n", "`nReturn Value:`n"
$description = $description -replace "(?<=\w+)( `n)(?=\S{2,})", " "
$description = $description -replace "(`n +){2,}", ""
$description = $description -replace "`n`$", ""
$description = $description -replace "`"", "\`""
$description = "Description: "+$text+"`n`n"+$description
foreach ($parameter in $($syntax.Split(","))) {
if ([array]::IndexOf($($syntax.Split(",")), $parameter) -eq 0) {
$description = $description -replace $($parameter.ToString() + "\s"), $("Parameters:`n"+$parameter.ToString()+" - ")
} else {
$description = $description -replace $($parameter.ToString() + " "), $($parameter.ToString()+" - ")
}
}
}
$arr[$name] = @{}
$arr[$name]["name"] = $name
$arr[$name]["text"] = $returns
$arr[$name]["syntax"] = $syntax
$arr[$name]["description"] = $description
}
# Convert Array into api.add file string
$apiadd = " ListModel {
id: newModel`n"
foreach ($api in $($arr.GetEnumerator().name| Sort-Object)) {
$apiadd += "`n ListElement {`n"
$apiadd += " name: `""+$arr[$api].name+"`"`n"
$apiadd += " query: `"<"+$arr[$api].name+"_REQUEST>`n<FUNCTION>`n<NAME>"+$arr[$api].name.ToUpper()+"</NAME>`n<PARAMETERS>`n"
foreach ($parameter in $($arr[$api].syntax.ToUpper().Split(","))) {
$apiadd += "<"+$parameter+">"+"</"+$parameter+">`n"
}
$apiadd += "</PARAMETERS>`n</FUNCTION>`n</"+$arr[$api].name+"_REQUEST>`";`n inputValues: [`n"
foreach ($parameter in $($arr[$api].syntax.Split(","))) {
$apiadd += " ListElement { inputs: `""+$parameter+"`" },`n"
}
$apiadd += " ListElement { returns: `""+$arr[$api].text+"`"}`n ]`n"
$apiadd += " definition: `""+$arr[$api].description+"`"`n }`n"
}
$apiadd += " }`n"
# Write String to File
$apiadd | Add-Content -Path "api.add"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment