From ad277394b6e87ad0472df1755161040a27e977b6 Mon Sep 17 00:00:00 2001 From: "Mr.k" Date: Sun, 21 Apr 2024 00:12:23 +0300 Subject: [PATCH] Better JSON Parsing for Compile Script!! (#1850) * Better JSON Parsing for Compile Script!! Rendering Special XML Character should be possible when parsing json files that have some of these special characters, and without the need to worry about them. Try these changes to see what will be affected, and what won't be. * Undo #1844 PR Changes The problem that #1844 PR Tried to resolve temporarly, should now be fixed permanently with the previous changes to 'Compile.ps1' Script. --- Compile.ps1 | 28 +++++++++++++++++++++++++++- config/applications.json | 2 +- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Compile.ps1 b/Compile.ps1 index 68031a44..330c226a 100644 --- a/Compile.ps1 +++ b/Compile.ps1 @@ -26,6 +26,32 @@ Get-ChildItem .\functions -Recurse -File | ForEach-Object { Get-ChildItem .\config | Where-Object {$psitem.extension -eq ".json"} | ForEach-Object { $json = (Get-Content $psitem.FullName).replace("'","''") + + # Replace every XML Special Character so it'll render correctly in final build + # Only do so if json files has content to be displayed (for example the applications, tweaks, features json files) + # Some Type Convertion using Casting and Cleaning Up of the convertion result using 'Replace' Method + $jsonAsObject = $json | convertfrom-json + $firstLevelJsonList = ([System.String]$jsonAsObject).split('=;') | ForEach-Object { + $_.Replace('=}','').Replace('@{','').Replace(' ','') + } + + for ($i = 0; $i -lt $firstLevelJsonList.Count; $i += 1) { + $firstLevelName = $firstLevelJsonList[$i] + # Note: Avoid using HTML Entity Codes (for example '”' (stands for "Right Double Quotation Mark")), and use HTML decimal/hex codes instead. + # as using HTML Entity Codes will result in XML parse Error when running the compiled script. + if ($jsonAsObject.$firstLevelName.content -ne $null) { + $jsonAsObject.$firstLevelName.content = $jsonAsObject.$firstLevelName.content.replace('&','&').replace('“','“').replace('”','”').replace("'",''').replace('<','<').replace('>','>') + $jsonAsObject.$firstLevelName.content = $jsonAsObject.$firstLevelName.content.replace('''',"'") # resolves the Double Apostrophe caused by the first replace function in the main loop + } + if ($jsonAsObject.$firstLevelName.description -ne $null) { + $jsonAsObject.$firstLevelName.description = $jsonAsObject.$firstLevelName.description.replace('&','&').replace('“','“').replace('”','”').replace("'",''').replace('<','<').replace('>','>') + $jsonAsObject.$firstLevelName.description = $jsonAsObject.$firstLevelName.description.replace('''',"'") # resolves the Double Apostrophe caused by the first replace function in the main loop + } + } + # The replace at the end is required, as without it the output of converto-json will be somewhat weird for Multiline String + # Most Notably is the scripts in json files, make=ing it harder for users who want to review these scripts that are found in the final compiled script + $json = ($jsonAsObject | convertto-json -Depth 3).replace('\r\n',"`r`n") + $sync.configs.$($psitem.BaseName) = $json | convertfrom-json Write-output "`$sync.configs.$($psitem.BaseName) = '$json' `| convertfrom-json" | Out-File ./$scriptname -Append -Encoding ascii } @@ -60,4 +86,4 @@ $xaml = $xaml -replace "{{InstallPanel_features}}", $featuresXamlContent Write-output "`$inputXML = '$xaml'" | Out-File ./$scriptname -Append -Encoding ascii -Get-Content .\scripts\main.ps1 | Out-File ./$scriptname -Append -Encoding ascii \ No newline at end of file +Get-Content .\scripts\main.ps1 | Out-File ./$scriptname -Append -Encoding ascii diff --git a/config/applications.json b/config/applications.json index e4a70bd9..c65f1906 100644 --- a/config/applications.json +++ b/config/applications.json @@ -1955,7 +1955,7 @@ "category": "Development", "choco": "na", "content": "Swift toolchain", - "description": "Swift is a general-purpose programming language that is approachable for newcomers and powerful for experts.", + "description": "Swift is a general-purpose programming language that's approachable for newcomers and powerful for experts.", "link": "https://www.swift.org/", "winget": "Swift.Toolchain" },