├── .github ├── FUNDING.yml ├── dependabot.yml └── workflows │ ├── build.yml │ └── publish.yml ├── .gitignore ├── .vscode ├── launch.json └── tasks.json ├── Common.props ├── LICENSE ├── README.md ├── Shortcodes.sln ├── src ├── Directory.Build.props └── Shortcodes │ ├── Arguments.cs │ ├── Context.cs │ ├── IShortcodeProvider.cs │ ├── NamedShortcodeProvider.cs │ ├── Node.cs │ ├── RawText.cs │ ├── Shortcode.cs │ ├── ShortcodeStyle.cs │ ├── Shortcodes.csproj │ ├── Shortcodes.snk │ ├── ShortcodesParser.cs │ ├── ShortcodesProcessor.cs │ ├── ShortcodesScanner.cs │ └── StringBuilderPool.cs └── tests ├── Shortcodes.Benchmarks ├── Program.cs ├── RenderBenchmarks.cs └── Shortcodes.Benchmarks.csproj └── Shortcodes.Tests ├── Shortcodes.Tests.csproj ├── ShortcodesParserTests.cs └── ShortcodesProcessorTests.cs /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [sebastienros] 2 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: "nuget" # See documentation for possible values 9 | directory: "/" # Location of package manifests 10 | schedule: 11 | interval: "weekly" 12 | groups: 13 | # Grouped version updates configuration 14 | all-dependencies: 15 | patterns: 16 | - "*" 17 | exclude-patterns: 18 | - "*Abstractions" # Don't update abstractions packages 19 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | paths-ignore: 7 | - 'doc/**' 8 | - 'readme.md' 9 | 10 | pull_request: 11 | branches: [ main ] 12 | 13 | jobs: 14 | build: 15 | 16 | runs-on: ubuntu-latest 17 | 18 | steps: 19 | - uses: actions/checkout@v4 20 | - name: Test 21 | run: dotnet test --configuration Release 22 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish 2 | 3 | on: 4 | push: 5 | tags: 6 | - 'v*.*.*' 7 | 8 | jobs: 9 | build: 10 | 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v4 15 | - name: Install dependencies 16 | run: dotnet restore 17 | - name: Test 18 | run: dotnet test -c Release 19 | - name: Pack with dotnet 20 | run: | 21 | arrTag=(${GITHUB_REF//\// }) 22 | VERSION="${arrTag[2]}" 23 | VERSION="${VERSION//v}" 24 | echo "$VERSION" 25 | dotnet pack --output artifacts --configuration Release -p:Version=$VERSION 26 | - name: Push with dotnet 27 | run: dotnet nuget push artifacts/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | [Ll]ogs/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | # Uncomment if you have tasks that create the project's static files in wwwroot 37 | #wwwroot/ 38 | 39 | # Visual Studio 2017 auto generated files 40 | Generated\ Files/ 41 | 42 | # MSTest test Results 43 | [Tt]est[Rr]esult*/ 44 | [Bb]uild[Ll]og.* 45 | 46 | # NUnit 47 | *.VisualState.xml 48 | TestResult.xml 49 | nunit-*.xml 50 | 51 | # Build Results of an ATL Project 52 | [Dd]ebugPS/ 53 | [Rr]eleasePS/ 54 | dlldata.c 55 | 56 | # Benchmark Results 57 | BenchmarkDotNet.Artifacts/ 58 | 59 | # .NET Core 60 | project.lock.json 61 | project.fragment.lock.json 62 | artifacts/ 63 | 64 | # StyleCop 65 | StyleCopReport.xml 66 | 67 | # Files built by Visual Studio 68 | *_i.c 69 | *_p.c 70 | *_h.h 71 | *.ilk 72 | *.meta 73 | *.obj 74 | *.iobj 75 | *.pch 76 | *.pdb 77 | *.ipdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *_wpftmp.csproj 88 | *.log 89 | *.vspscc 90 | *.vssscc 91 | .builds 92 | *.pidb 93 | *.svclog 94 | *.scc 95 | 96 | # Chutzpah Test files 97 | _Chutzpah* 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opendb 104 | *.opensdf 105 | *.sdf 106 | *.cachefile 107 | *.VC.db 108 | *.VC.VC.opendb 109 | 110 | # Visual Studio profiler 111 | *.psess 112 | *.vsp 113 | *.vspx 114 | *.sap 115 | 116 | # Visual Studio Trace Files 117 | *.e2e 118 | 119 | # TFS 2012 Local Workspace 120 | $tf/ 121 | 122 | # Guidance Automation Toolkit 123 | *.gpState 124 | 125 | # ReSharper is a .NET coding add-in 126 | _ReSharper*/ 127 | *.[Rr]e[Ss]harper 128 | *.DotSettings.user 129 | 130 | # TeamCity is a build add-in 131 | _TeamCity* 132 | 133 | # DotCover is a Code Coverage Tool 134 | *.dotCover 135 | 136 | # AxoCover is a Code Coverage Tool 137 | .axoCover/* 138 | !.axoCover/settings.json 139 | 140 | # Visual Studio code coverage results 141 | *.coverage 142 | *.coveragexml 143 | 144 | # NCrunch 145 | _NCrunch_* 146 | .*crunch*.local.xml 147 | nCrunchTemp_* 148 | 149 | # MightyMoose 150 | *.mm.* 151 | AutoTest.Net/ 152 | 153 | # Web workbench (sass) 154 | .sass-cache/ 155 | 156 | # Installshield output folder 157 | [Ee]xpress/ 158 | 159 | # DocProject is a documentation generator add-in 160 | DocProject/buildhelp/ 161 | DocProject/Help/*.HxT 162 | DocProject/Help/*.HxC 163 | DocProject/Help/*.hhc 164 | DocProject/Help/*.hhk 165 | DocProject/Help/*.hhp 166 | DocProject/Help/Html2 167 | DocProject/Help/html 168 | 169 | # Click-Once directory 170 | publish/ 171 | 172 | # Publish Web Output 173 | *.[Pp]ublish.xml 174 | *.azurePubxml 175 | # Note: Comment the next line if you want to checkin your web deploy settings, 176 | # but database connection strings (with potential passwords) will be unencrypted 177 | *.pubxml 178 | *.publishproj 179 | 180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 181 | # checkin your Azure Web App publish settings, but sensitive information contained 182 | # in these scripts will be unencrypted 183 | PublishScripts/ 184 | 185 | # NuGet Packages 186 | *.nupkg 187 | # NuGet Symbol Packages 188 | *.snupkg 189 | # The packages folder can be ignored because of Package Restore 190 | **/[Pp]ackages/* 191 | # except build/, which is used as an MSBuild target. 192 | !**/[Pp]ackages/build/ 193 | # Uncomment if necessary however generally it will be regenerated when needed 194 | #!**/[Pp]ackages/repositories.config 195 | # NuGet v3's project.json files produces more ignorable files 196 | *.nuget.props 197 | *.nuget.targets 198 | 199 | # Microsoft Azure Build Output 200 | csx/ 201 | *.build.csdef 202 | 203 | # Microsoft Azure Emulator 204 | ecf/ 205 | rcf/ 206 | 207 | # Windows Store app package directories and files 208 | AppPackages/ 209 | BundleArtifacts/ 210 | Package.StoreAssociation.xml 211 | _pkginfo.txt 212 | *.appx 213 | *.appxbundle 214 | *.appxupload 215 | 216 | # Visual Studio cache files 217 | # files ending in .cache can be ignored 218 | *.[Cc]ache 219 | # but keep track of directories ending in .cache 220 | !?*.[Cc]ache/ 221 | 222 | # Others 223 | ClientBin/ 224 | ~$* 225 | *~ 226 | *.dbmdl 227 | *.dbproj.schemaview 228 | *.jfm 229 | *.pfx 230 | *.publishsettings 231 | orleans.codegen.cs 232 | 233 | # Including strong name files can present a security risk 234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 235 | #*.snk 236 | 237 | # Since there are multiple workflows, uncomment next line to ignore bower_components 238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 239 | #bower_components/ 240 | 241 | # RIA/Silverlight projects 242 | Generated_Code/ 243 | 244 | # Backup & report files from converting an old project file 245 | # to a newer Visual Studio version. Backup files are not needed, 246 | # because we have git ;-) 247 | _UpgradeReport_Files/ 248 | Backup*/ 249 | UpgradeLog*.XML 250 | UpgradeLog*.htm 251 | ServiceFabricBackup/ 252 | *.rptproj.bak 253 | 254 | # SQL Server files 255 | *.mdf 256 | *.ldf 257 | *.ndf 258 | 259 | # Business Intelligence projects 260 | *.rdl.data 261 | *.bim.layout 262 | *.bim_*.settings 263 | *.rptproj.rsuser 264 | *- [Bb]ackup.rdl 265 | *- [Bb]ackup ([0-9]).rdl 266 | *- [Bb]ackup ([0-9][0-9]).rdl 267 | 268 | # Microsoft Fakes 269 | FakesAssemblies/ 270 | 271 | # GhostDoc plugin setting file 272 | *.GhostDoc.xml 273 | 274 | # Node.js Tools for Visual Studio 275 | .ntvs_analysis.dat 276 | node_modules/ 277 | 278 | # Visual Studio 6 build log 279 | *.plg 280 | 281 | # Visual Studio 6 workspace options file 282 | *.opt 283 | 284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 285 | *.vbw 286 | 287 | # Visual Studio LightSwitch build output 288 | **/*.HTMLClient/GeneratedArtifacts 289 | **/*.DesktopClient/GeneratedArtifacts 290 | **/*.DesktopClient/ModelManifest.xml 291 | **/*.Server/GeneratedArtifacts 292 | **/*.Server/ModelManifest.xml 293 | _Pvt_Extensions 294 | 295 | # Paket dependency manager 296 | .paket/paket.exe 297 | paket-files/ 298 | 299 | # FAKE - F# Make 300 | .fake/ 301 | 302 | # CodeRush personal settings 303 | .cr/personal 304 | 305 | # Python Tools for Visual Studio (PTVS) 306 | __pycache__/ 307 | *.pyc 308 | 309 | # Cake - Uncomment if you are using it 310 | # tools/** 311 | # !tools/packages.config 312 | 313 | # Tabs Studio 314 | *.tss 315 | 316 | # Telerik's JustMock configuration file 317 | *.jmconfig 318 | 319 | # BizTalk build output 320 | *.btp.cs 321 | *.btm.cs 322 | *.odx.cs 323 | *.xsd.cs 324 | 325 | # OpenCover UI analysis results 326 | OpenCover/ 327 | 328 | # Azure Stream Analytics local run output 329 | ASALocalRun/ 330 | 331 | # MSBuild Binary and Structured Log 332 | *.binlog 333 | 334 | # NVidia Nsight GPU debugger configuration file 335 | *.nvuser 336 | 337 | # MFractors (Xamarin productivity tool) working folder 338 | .mfractor/ 339 | 340 | # Local History for Visual Studio 341 | .localhistory/ 342 | 343 | # BeatPulse healthcheck temp database 344 | healthchecksdb 345 | 346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 347 | MigrationBackup/ 348 | 349 | # Ionide (cross platform F# VS Code tools) working folder 350 | .ionide/ 351 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to find out which attributes exist for C# debugging 3 | // Use hover for the description of the existing attributes 4 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": ".NET Core Launch (console)", 9 | "type": "coreclr", 10 | "request": "launch", 11 | "preLaunchTask": "build", 12 | // If you have changed target frameworks, make sure to update the program path. 13 | "program": "${workspaceFolder}/tests/Shortcodes.Tests/bin/Debug/netcoreapp3.1/Shortcodes.Tests.dll", 14 | "args": [], 15 | "cwd": "${workspaceFolder}/tests/Shortcodes.Tests", 16 | // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console 17 | "console": "internalConsole", 18 | "stopAtEntry": false 19 | }, 20 | { 21 | "name": ".NET Core Attach", 22 | "type": "coreclr", 23 | "request": "attach", 24 | "processId": "${command:pickProcess}" 25 | } 26 | ] 27 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "label": "build", 6 | "command": "dotnet", 7 | "type": "process", 8 | "args": [ 9 | "build", 10 | "${workspaceFolder}/tests/Shortcodes.Tests/Shortcodes.Tests.csproj", 11 | "/property:GenerateFullPaths=true", 12 | "/consoleloggerparameters:NoSummary" 13 | ], 14 | "problemMatcher": "$msCompile" 15 | }, 16 | { 17 | "label": "publish", 18 | "command": "dotnet", 19 | "type": "process", 20 | "args": [ 21 | "publish", 22 | "${workspaceFolder}/tests/Shortcodes.Tests/Shortcodes.Tests.csproj", 23 | "/property:GenerateFullPaths=true", 24 | "/consoleloggerparameters:NoSummary" 25 | ], 26 | "problemMatcher": "$msCompile" 27 | }, 28 | { 29 | "label": "watch", 30 | "command": "dotnet", 31 | "type": "process", 32 | "args": [ 33 | "watch", 34 | "run", 35 | "${workspaceFolder}/tests/Shortcodes.Tests/Shortcodes.Tests.csproj", 36 | "/property:GenerateFullPaths=true", 37 | "/consoleloggerparameters:NoSummary" 38 | ], 39 | "problemMatcher": "$msCompile" 40 | } 41 | ] 42 | } -------------------------------------------------------------------------------- /Common.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Sebastien Ros 5 | Sebastien Ros 6 | https://github.com/sebastienros/shortcodes 7 | MIT 8 | 0.0.0-preview-$(GITHUB_RUN_ID) 9 | true 10 | netstandard2.1 11 | Latest 12 | portable 13 | false 14 | false 15 | false 16 | false 17 | false 18 | false 19 | false 20 | false 21 | 22 | 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Sébastien Ros 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Basic Overview 2 | 3 | Shortcodes processor for .NET with focus on performance and simplicity. 4 | 5 |
6 | 7 | ## Features 8 | 9 | - Parses and renders shortcodes. 10 | - Supports **async** shortcode to execute database queries and async operations more efficiently under load. 11 | - Named and positioned arguments. 12 | 13 |
14 | 15 | ## Contents 16 | - [Sample usage](#sample-usage) 17 | - [Used by](#used-by) 18 | 19 |
20 | 21 | ## Sample usage 22 | 23 | Don't forget to include the __using__ statement: 24 | 25 | ```c# 26 | using Shortcodes; 27 | ``` 28 | 29 | ### Predefined shortcodes 30 | 31 | ```c# 32 | var processor = new ShortcodesProcessor(new NamedShortcodeProvider 33 | { 34 | ["hello"] = (args, content, ctx) => new ValueTask("Hello world!") 35 | }); 36 | 37 | Console.WriteLine(await processor.EvaluateAsync("This is an [hello]")); 38 | ``` 39 | 40 | Which results in 41 | 42 | ``` 43 | This is an Hello world! 44 | ``` 45 | 46 | ### Named arguments 47 | 48 | Arguments can contain any character, but need to be quoted either with `'` or `"` if they contain spaces. 49 | Strings can use standard string escape sequences like `\u03A9` and `\n`. 50 | 51 | ```c# 52 | var processor = new ShortcodesProcessor(new NamedShortcodeProvider 53 | { 54 | ["bold"] = (args, content, ctx) => 55 | { 56 | var text = args.Named("text"); 57 | 58 | return new ValueTask($"{text}"); 59 | } 60 | }); 61 | 62 | Console.WriteLine(await processor.EvaluateAsync("[bold text='bold text' 1234]")); 63 | ``` 64 | 65 | ### Content arguments 66 | 67 | Shortcodes using opening and closing tags can access their inner content. 68 | 69 | ```c# 70 | var processor = new ShortcodesProcessor(new NamedShortcodeProvider 71 | { 72 | ["bold"] = (args, content, ctx) => 73 | { 74 | return new ValueTask($"{content}"); 75 | } 76 | }); 77 | 78 | Console.WriteLine(await processor.EvaluateAsync("[bold]bold text[/bold]")); 79 | ``` 80 | 81 | For single tags, the content is `null`. It means that you can detect if a shortcode was 82 | used with a closing tag, even if the inner content is empty. 83 | 84 | ### Positional arguments 85 | 86 | If an argument doesn't have a name, an default index can be used. 87 | 88 | ```c# 89 | var processor = new ShortcodesProcessor(new NamedShortcodeProvider 90 | { 91 | ["bold"] = (args, content, ctx) => 92 | { 93 | var text = args.NamedOrDefault("text"); 94 | 95 | return new ValueTask($"{text}"); 96 | } 97 | }); 98 | 99 | Console.WriteLine(await processor.EvaluateAsync("[bold 'bold text']")); 100 | ``` 101 | 102 | ``` 103 | bold text 104 | ``` 105 | 106 | Named and positional arguments can be mixed together. Each time an argument doesn't 107 | have a name, the index is incremented. 108 | 109 | 110 | ```c# 111 | var processor = new ShortcodesProcessor(new NamedShortcodeProvider 112 | { 113 | ["bold"] = (args, content, ctx) => 114 | { 115 | var text = args.At(0); 116 | 117 | return new ValueTask($"{text}"); 118 | } 119 | }); 120 | 121 | Console.WriteLine(await processor.EvaluateAsync("[bold id='a' 'some text']")); 122 | ``` 123 | 124 | ``` 125 | some text 126 | ``` 127 | 128 | ### Escaping tags 129 | 130 | In case you want to render a shortcode instead of evaluating it, you can double the 131 | opening and closing braces. 132 | 133 | ``` 134 | [[bold] some text to show [/bold]] 135 | ``` 136 | 137 | Will then be rendered as 138 | 139 | ``` 140 | [bold] some text to show [/bold] 141 | ``` 142 | 143 | And for single tags: 144 | 145 | ``` 146 | [[bold 'text']] 147 | ``` 148 | 149 | Will be rendered as 150 | 151 | ``` 152 | [bold 'text'] 153 | ``` 154 | 155 | In case several braces are used, and they are balanced, a single one will be escaped. 156 | 157 | ``` 158 | [[[bold 'text']]] 159 | ``` 160 | 161 | Will be rendered as: 162 | 163 | ``` 164 | [[bold 'text']] 165 | ``` 166 | 167 | Not that unbalanced braces won't be escaped. 168 | 169 | ``` 170 | [[[[bold 'text']] 171 | ``` 172 | 173 | Will be rendered as 174 | 175 | ``` 176 | [[[[bold 'text']] 177 | ``` 178 | 179 | ### Context object 180 | 181 | A __Context__ object can be passed when evaluating a template. This object 182 | is shared across all shortcodes 183 | 184 | A common usage is to pass custom data that might be used by some shortcodes, like 185 | the current `HttpContext` if a template is running in a web application and needs 186 | to access the current request. 187 | 188 | Another usage is to use it as a bag of values that can be shared across shortcodes. 189 | 190 | ```c# 191 | // From a Startup.cs class 192 | 193 | var processor = new ShortcodesProcessor(new NamedShortcodeProvider 194 | { 195 | ["username"] = (args, content, ctx) => 196 | { 197 | var httpContext = (HttpContext)ctx["HttpContext"]; 198 | 199 | return new ValueTask(httpContext.User.Identity.Name); 200 | } 201 | }); 202 | 203 | app.Run((httpContext) => 204 | { 205 | var context = new Context(){ ["HttpContext"] = httpContext }; 206 | var result = await processor.EvaluateAsync("The current user is [username]", context); 207 | return context.Response.WriteAsync(result); 208 | }); 209 | ``` 210 | 211 | ``` 212 | The current user is admin 213 | ``` 214 | -------------------------------------------------------------------------------- /Shortcodes.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.11.35111.106 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shortcodes.Tests", "tests\Shortcodes.Tests\Shortcodes.Tests.csproj", "{7ECF5D62-FDDF-4A6E-A6DD-8B4EDFB5CB4E}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shortcodes", "src\Shortcodes\Shortcodes.csproj", "{EDD2509B-3D55-4EA4-83B3-017462DC090F}" 9 | EndProject 10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{46FB729C-77E2-4C63-A799-566E6DBF6064}" 11 | ProjectSection(SolutionItems) = preProject 12 | .github\workflows\build.yml = .github\workflows\build.yml 13 | Common.props = Common.props 14 | src\Directory.Build.props = src\Directory.Build.props 15 | .github\workflows\publish.yml = .github\workflows\publish.yml 16 | EndProjectSection 17 | EndProject 18 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shortcodes.Benchmarks", "tests\Shortcodes.Benchmarks\Shortcodes.Benchmarks.csproj", "{D7B1F710-04EC-49F1-BE79-FC4A83749E69}" 19 | EndProject 20 | Global 21 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 22 | Debug|Any CPU = Debug|Any CPU 23 | Release|Any CPU = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 26 | {7ECF5D62-FDDF-4A6E-A6DD-8B4EDFB5CB4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {7ECF5D62-FDDF-4A6E-A6DD-8B4EDFB5CB4E}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {7ECF5D62-FDDF-4A6E-A6DD-8B4EDFB5CB4E}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {7ECF5D62-FDDF-4A6E-A6DD-8B4EDFB5CB4E}.Release|Any CPU.Build.0 = Release|Any CPU 30 | {EDD2509B-3D55-4EA4-83B3-017462DC090F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 31 | {EDD2509B-3D55-4EA4-83B3-017462DC090F}.Debug|Any CPU.Build.0 = Debug|Any CPU 32 | {EDD2509B-3D55-4EA4-83B3-017462DC090F}.Release|Any CPU.ActiveCfg = Release|Any CPU 33 | {EDD2509B-3D55-4EA4-83B3-017462DC090F}.Release|Any CPU.Build.0 = Release|Any CPU 34 | {D7B1F710-04EC-49F1-BE79-FC4A83749E69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 35 | {D7B1F710-04EC-49F1-BE79-FC4A83749E69}.Debug|Any CPU.Build.0 = Debug|Any CPU 36 | {D7B1F710-04EC-49F1-BE79-FC4A83749E69}.Release|Any CPU.ActiveCfg = Release|Any CPU 37 | {D7B1F710-04EC-49F1-BE79-FC4A83749E69}.Release|Any CPU.Build.0 = Release|Any CPU 38 | EndGlobalSection 39 | GlobalSection(SolutionProperties) = preSolution 40 | HideSolutionNode = FALSE 41 | EndGlobalSection 42 | GlobalSection(ExtensibilityGlobals) = postSolution 43 | SolutionGuid = {CC14EE4A-5B52-4057-9F63-F045DE329C8D} 44 | EndGlobalSection 45 | EndGlobal 46 | -------------------------------------------------------------------------------- /src/Directory.Build.props: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Shortcodes 5 | Shortcodes processor for .NET 6 | Shortcodes processor for .NET with focus on performance and simplicity. 7 | shortcode;template; 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/Shortcodes/Arguments.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace Shortcodes 6 | { 7 | public struct Arguments : IEnumerable> 8 | { 9 | public static Arguments Empty = new Arguments(new Dictionary()); 10 | 11 | private readonly Dictionary _arguments; 12 | 13 | public Arguments(Dictionary arguments) 14 | { 15 | _arguments = arguments; 16 | } 17 | 18 | public IEnumerator> GetEnumerator() 19 | { 20 | if (_arguments == null) 21 | { 22 | return Enumerable.Empty>().GetEnumerator(); 23 | } 24 | 25 | return ((IEnumerable>)_arguments).GetEnumerator(); 26 | } 27 | 28 | public ICollection Keys => _arguments.Keys; 29 | 30 | public string Named(string index) 31 | { 32 | if (_arguments == null) 33 | { 34 | return null; 35 | } 36 | 37 | if (_arguments.TryGetValue(index, out string result)) 38 | { 39 | return result; 40 | } 41 | 42 | return null; 43 | } 44 | 45 | public string NamedOrDefault(string name) 46 | { 47 | return Named(name) ?? Named("0"); 48 | } 49 | 50 | public string NamedOrAt(string name, int index) 51 | { 52 | return Named(name) ?? Named(index.ToString()); 53 | } 54 | 55 | public string At(int index) 56 | { 57 | return Named(index.ToString()); 58 | } 59 | 60 | public int Count => _arguments.Count; 61 | 62 | IEnumerator IEnumerable.GetEnumerator() 63 | { 64 | if (_arguments == null) 65 | { 66 | return Enumerable.Empty>().GetEnumerator(); 67 | } 68 | 69 | return ((IEnumerable>)_arguments).GetEnumerator(); 70 | } 71 | 72 | public bool Any() 73 | { 74 | return _arguments != null && _arguments.Count > 0; 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/Shortcodes/Context.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | 4 | namespace Shortcodes 5 | { 6 | public class Context : IEnumerable> 7 | { 8 | private Dictionary _items; 9 | 10 | private Dictionary Items => _items ??= new Dictionary(); 11 | 12 | public object this[string key] 13 | { 14 | get 15 | { 16 | return Items[key]; 17 | } 18 | set 19 | { 20 | Items[key] = value; 21 | } 22 | } 23 | 24 | public ICollection Keys => Items.Keys; 25 | 26 | public int Count => Items.Count; 27 | 28 | public void Clear() 29 | { 30 | Items.Clear(); 31 | } 32 | 33 | public bool ContainsKey(string key) 34 | { 35 | return Items.ContainsKey(key); 36 | } 37 | 38 | public IEnumerator> GetEnumerator() 39 | { 40 | return Items.GetEnumerator(); 41 | } 42 | 43 | public bool Remove(string key) 44 | { 45 | return Items.Remove(key); 46 | } 47 | 48 | public bool TryGetValue(string key, out object value) 49 | { 50 | return Items.TryGetValue(key, out value); 51 | } 52 | 53 | public T GetOrSetValue(string key, T value) 54 | { 55 | if (Items.TryGetValue(key, out var result)) 56 | { 57 | if (result is T t) 58 | { 59 | return t; 60 | } 61 | else 62 | { 63 | return default(T); 64 | } 65 | } 66 | else 67 | { 68 | Items[key] = value; 69 | 70 | return value; 71 | } 72 | } 73 | 74 | IEnumerator IEnumerable.GetEnumerator() 75 | { 76 | return Items.GetEnumerator(); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/Shortcodes/IShortcodeProvider.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace Shortcodes 4 | { 5 | /// 6 | /// Delegate evaluated when a shortcode has matched. 7 | /// 8 | /// The arguments passed with the shortcode. 9 | /// The inner content of the shortcode.null for self-closing shortcodes. 10 | /// Custom properties used to evaluate a template. 11 | /// The string to substitue the shortcode with. 12 | public delegate ValueTask ShortcodeDelegate(Arguments arguments, string content, Context context); 13 | 14 | public interface IShortcodeProvider 15 | { 16 | /// 17 | /// Evaluates a named shortcode. 18 | /// 19 | /// The name of the shortcode to evaluate. 20 | /// The arguments passed with the shortcode. 21 | /// The inner content of the shortcode.null for self-closing shortcodes. 22 | /// Custom properties used to evaluate a template. 23 | /// The string to substitue the shortcode with. 24 | ValueTask EvaluateAsync(string identifier, Arguments arguments, string content, Context context); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Shortcodes/NamedShortcodeProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Threading.Tasks; 5 | 6 | namespace Shortcodes 7 | { 8 | public class NamedShortcodeProvider : IShortcodeProvider, IEnumerable 9 | { 10 | private static ValueTask Null => new ValueTask((string)null); 11 | 12 | private Dictionary Shortcodes { get; } 13 | 14 | public NamedShortcodeProvider() 15 | { 16 | Shortcodes = new Dictionary(StringComparer.OrdinalIgnoreCase); 17 | } 18 | 19 | public NamedShortcodeProvider(Dictionary shortcodes) 20 | { 21 | Shortcodes = new Dictionary(shortcodes, StringComparer.OrdinalIgnoreCase); 22 | } 23 | 24 | public bool Contains(string shortcode) 25 | { 26 | return Shortcodes.ContainsKey(shortcode); 27 | } 28 | 29 | public ShortcodeDelegate this[string shortcode] 30 | { 31 | get { return Shortcodes[shortcode]; } 32 | set { Shortcodes[shortcode] = value; } 33 | } 34 | 35 | public ValueTask EvaluateAsync(string identifier, Arguments arguments, string content, Context context) 36 | { 37 | if (Shortcodes.TryGetValue(identifier, out var shortcode)) 38 | { 39 | if (shortcode == null) 40 | { 41 | return Null; 42 | } 43 | 44 | return shortcode.Invoke(arguments, content, context); 45 | } 46 | 47 | return Null; 48 | } 49 | 50 | public IEnumerator GetEnumerator() 51 | { 52 | return ((IEnumerable)Shortcodes).GetEnumerator(); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/Shortcodes/Node.cs: -------------------------------------------------------------------------------- 1 | namespace Shortcodes 2 | { 3 | public abstract class Node 4 | { 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /src/Shortcodes/RawText.cs: -------------------------------------------------------------------------------- 1 | using Parlot; 2 | using System; 3 | 4 | namespace Shortcodes 5 | { 6 | public class RawText : Node 7 | { 8 | private readonly TextSpan _textSpan; 9 | 10 | public RawText(TextSpan textSpan) 11 | { 12 | _textSpan = textSpan; 13 | } 14 | 15 | public ReadOnlySpan Span => _textSpan.Span; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/Shortcodes/Shortcode.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Shortcodes 4 | { 5 | public class Shortcode : Node 6 | { 7 | public Shortcode(string identifier, ShortcodeStyle style, int openBraces, int closeBraces, int sourceIndex, int sourceLength, Arguments arguments) 8 | { 9 | Identifier = identifier ?? throw new ArgumentNullException(nameof(identifier)); 10 | Style = style; 11 | Content = null; 12 | OpenBraces = openBraces; 13 | CloseBraces = closeBraces; 14 | SourceIndex = sourceIndex; 15 | SourceLength = sourceLength; 16 | Arguments = arguments; 17 | } 18 | 19 | public string Identifier { get; } 20 | public ShortcodeStyle Style { get; } 21 | public string Content { get; set; } 22 | public Arguments Arguments { get; } 23 | public int SourceIndex { get; set; } 24 | public int SourceLength { get; set; } 25 | public int OpenBraces { get; set; } = 1; 26 | public int CloseBraces { get; set; } = 1; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Shortcodes/ShortcodeStyle.cs: -------------------------------------------------------------------------------- 1 | namespace Shortcodes 2 | { 3 | public enum ShortcodeStyle 4 | { 5 | Open, 6 | Close, 7 | SelfClosing 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/Shortcodes/Shortcodes.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8.0 4 | Shortcodes.snk 5 | true 6 | true 7 | pdbonly 8 | true 9 | $(NoWarn);1591 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/Shortcodes/Shortcodes.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sebastienros/shortcodes/f337cedb6abca5efb8ecba4436faae77ba0d082e/src/Shortcodes/Shortcodes.snk -------------------------------------------------------------------------------- /src/Shortcodes/ShortcodesParser.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Parlot; 4 | 5 | namespace Shortcodes 6 | { 7 | /* 8 | * nodes => ( shortcode | TEXT )* 9 | * shortcode => '['+ identifier (arguments)* ']'+ 10 | * | '['+ '/' identifier ']'+ 11 | * arguments => identifer '=' literal 12 | * literal => STRING | NUMBER 13 | */ 14 | public class ShortcodesParser 15 | { 16 | private ShortcodesScanner _scanner; 17 | 18 | public List Parse(string input) 19 | { 20 | _scanner = new ShortcodesScanner(input); 21 | return ParseNodes(); 22 | } 23 | 24 | private List ParseNodes() 25 | { 26 | var nodes = new List(8); 27 | 28 | while (!_scanner.Cursor.Eof) 29 | { 30 | var shortcode = ParseShortcode(); 31 | 32 | if (shortcode != null) 33 | { 34 | nodes.Add(shortcode); 35 | } 36 | else 37 | { 38 | var rawText = ParseRawText(); 39 | 40 | if (rawText == null) 41 | { 42 | throw new ParseException("Text didn't match any expected sequence.", _scanner.Cursor.Position); 43 | } 44 | 45 | nodes.Add(rawText); 46 | } 47 | } 48 | 49 | return nodes; 50 | } 51 | 52 | private RawText ParseRawText() 53 | { 54 | if (_scanner.ReadRawText(out var result)) 55 | { 56 | return new RawText(result); 57 | } 58 | 59 | return null; 60 | } 61 | 62 | private Shortcode ParseShortcode() 63 | { 64 | // Number of opening braces 65 | var openBraces = 0; 66 | 67 | // Number of closing braces 68 | var closeBraces = 0; 69 | 70 | Shortcode shortcode; 71 | 72 | var style = ShortcodeStyle.Open; 73 | 74 | // Start position of the shortcode 75 | var start = _scanner.Cursor.Position; 76 | 77 | if (!_scanner.ReadChar('[')) 78 | { 79 | return null; 80 | } 81 | 82 | openBraces += 1; 83 | 84 | // Read all '[' so we can detect escaped tags 85 | while (_scanner.ReadChar('[')) 86 | { 87 | openBraces += 1; 88 | } 89 | 90 | // Is it a closing tag? 91 | if (_scanner.ReadChar('/')) 92 | { 93 | style = ShortcodeStyle.Close; 94 | } 95 | 96 | // Reach Eof before end of shortcode 97 | if (_scanner.Cursor.Eof) 98 | { 99 | _scanner.Cursor.ResetPosition(start); 100 | 101 | return null; 102 | } 103 | 104 | _scanner.SkipWhiteSpace(); 105 | 106 | if (!_scanner.ReadIdentifier(out var _result)) 107 | { 108 | _scanner.Cursor.ResetPosition(start); 109 | 110 | return null; 111 | } 112 | 113 | var identifier = _result.ToString(); 114 | 115 | _scanner.SkipWhiteSpace(); 116 | 117 | Dictionary arguments = null; 118 | 119 | int argumentIndex = 0; 120 | 121 | // Arguments? 122 | while (!_scanner.Cursor.Eof) 123 | { 124 | // Record location in case it doesn't have a value 125 | var argumentStart = _scanner.Cursor.Position; 126 | 127 | if (_scanner.ReadQuotedString(out _result)) 128 | { 129 | arguments ??= CreateArgumentsDictionary(); 130 | 131 | arguments[argumentIndex.ToString()] = Character.DecodeString(_result)[1..^1].ToString(); 132 | 133 | argumentIndex += 1; 134 | } 135 | else if (_scanner.ReadIdentifier(out _result)) 136 | { 137 | _scanner.SkipWhiteSpace(); 138 | 139 | var argumentName = _result.ToString(); 140 | var valueStart = _scanner.Cursor.Offset; 141 | 142 | // It might just be a value 143 | if (_scanner.ReadChar('=')) 144 | { 145 | _scanner.SkipWhiteSpace(); 146 | 147 | if (_scanner.ReadQuotedString(out _result)) 148 | { 149 | arguments ??= CreateArgumentsDictionary(); 150 | 151 | arguments[argumentName] = Character.DecodeString(_result)[1..^1].ToString(); 152 | } 153 | else if (_scanner.ReadValue(out var textSpan)) 154 | { 155 | arguments ??= CreateArgumentsDictionary(); 156 | 157 | arguments[argumentName] = textSpan.ToString(); 158 | } 159 | else 160 | { 161 | _scanner.Cursor.ResetPosition(start); 162 | 163 | return null; 164 | } 165 | } 166 | else 167 | { 168 | // Positional argument that looks like an identifier 169 | 170 | _scanner.Cursor.ResetPosition(argumentStart); 171 | 172 | if (_scanner.ReadValue(out var textSpan)) 173 | { 174 | arguments ??= CreateArgumentsDictionary(); 175 | 176 | arguments[argumentIndex.ToString()] = textSpan.ToString(); 177 | 178 | argumentIndex += 1; 179 | } 180 | else 181 | { 182 | _scanner.Cursor.ResetPosition(start); 183 | 184 | break; 185 | } 186 | } 187 | } 188 | else if (_scanner.ReadValue(out var textSpan)) 189 | { 190 | arguments ??= CreateArgumentsDictionary(); 191 | 192 | arguments[argumentIndex.ToString()] = textSpan.ToString(); 193 | 194 | argumentIndex += 1; 195 | } 196 | else if (_scanner.Cursor.Match("/]")) 197 | { 198 | style = ShortcodeStyle.SelfClosing; 199 | _scanner.Cursor.Advance(); 200 | break; 201 | } 202 | else if (_scanner.Cursor.Match(']')) 203 | { 204 | break; 205 | } 206 | else 207 | { 208 | _scanner.Cursor.ResetPosition(start); 209 | return null; 210 | } 211 | 212 | _scanner.SkipWhiteSpace(); 213 | } 214 | 215 | // If we exited the loop due to EOF, exit 216 | if (_scanner.Cursor.Eof || !_scanner.ReadChar(']')) 217 | { 218 | _scanner.Cursor.ResetPosition(start); 219 | return null; 220 | } 221 | 222 | closeBraces += 1; 223 | 224 | // Read all ']' so we can detect escaped tags 225 | while (_scanner.ReadChar(']')) 226 | { 227 | closeBraces += 1; 228 | } 229 | 230 | shortcode = new Shortcode( 231 | identifier, 232 | style, 233 | openBraces, 234 | closeBraces, 235 | start.Offset, 236 | _scanner.Cursor.Position - start - 1, 237 | new Arguments(arguments) 238 | ); 239 | 240 | return shortcode; 241 | 242 | // Local function to use the same logic to create the arguments dictionary 243 | static Dictionary CreateArgumentsDictionary() 244 | { 245 | return new Dictionary(StringComparer.OrdinalIgnoreCase); 246 | } 247 | } 248 | } 249 | } 250 | -------------------------------------------------------------------------------- /src/Shortcodes/ShortcodesProcessor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using Parlot; 7 | 8 | namespace Shortcodes 9 | { 10 | public class ShortcodesProcessor 11 | { 12 | private readonly ShortcodesParser _parser = new ShortcodesParser(); 13 | 14 | public List Providers { get; } 15 | 16 | public ShortcodesProcessor() 17 | { 18 | Providers = new List(); 19 | } 20 | 21 | public ShortcodesProcessor(params IShortcodeProvider[] providers) 22 | { 23 | Providers = new List(providers); 24 | } 25 | 26 | public ShortcodesProcessor(IEnumerable providers) 27 | { 28 | Providers = new List(providers); 29 | } 30 | 31 | public ShortcodesProcessor(Dictionary shortcodes) : this (new NamedShortcodeProvider(shortcodes)) 32 | { 33 | } 34 | 35 | /// 36 | /// Evaluates a template with an optional context. 37 | /// 38 | /// The template to evaluate. 39 | /// An optional Context instance. 40 | /// A string with all shortcodes evaluated. 41 | public ValueTask EvaluateAsync(string input, Context context = null) 42 | { 43 | if (String.IsNullOrEmpty(input)) 44 | { 45 | return new ValueTask(input); 46 | } 47 | 48 | // Don't do anything if brackets can't be found in the input text 49 | var openIndex = input.IndexOf('['); 50 | var closeIndex = input.IndexOf(']'); 51 | 52 | if (openIndex < 0 || closeIndex < 0 || closeIndex < openIndex) 53 | { 54 | return new ValueTask(input); 55 | } 56 | 57 | if (context == null) 58 | { 59 | context = new Context(); 60 | } 61 | 62 | // Parse nodes 63 | var nodes = _parser.Parse(input); 64 | 65 | return FoldClosingTagsAsync(input, nodes, 0, nodes.Count, context); 66 | } 67 | 68 | private async ValueTask FoldClosingTagsAsync(string input, List nodes, int index, int length, Context context) 69 | { 70 | // This method should not be called when nodes has a single RawText element. 71 | // It's implementation assumes at least two nodes are provided. 72 | 73 | using var sb = StringBuilderPool.GetInstance(); 74 | 75 | // The index of the next shortcode opening node 76 | var cursor = index; 77 | 78 | // Process the list 79 | while (cursor <= index + length - 1) 80 | { 81 | Shortcode start = null; 82 | var head = 0; 83 | var tail = 0; 84 | 85 | // Find the next opening tag 86 | while (cursor <= index + length - 1 && start == null) 87 | { 88 | var node = nodes[cursor]; 89 | 90 | if (node is Shortcode shortCode) 91 | { 92 | if (shortCode.Style == ShortcodeStyle.Open) 93 | { 94 | head = cursor; 95 | start = shortCode; 96 | } 97 | else 98 | { 99 | // These closing tags need to be rendered 100 | sb.Builder.Append(input, shortCode.SourceIndex, shortCode.SourceLength + 1); 101 | } 102 | } 103 | else 104 | { 105 | var text = node as RawText; 106 | 107 | sb.Builder.Append(text.Span); 108 | } 109 | 110 | cursor += 1; 111 | } 112 | 113 | // if start is null, then there is nothing to fold 114 | if (start == null) 115 | { 116 | return sb.Builder.ToString(); 117 | } 118 | 119 | Shortcode end = null; 120 | 121 | var depth = 1; 122 | 123 | // Find a matching closing tag 124 | while (cursor <= index + length - 1 && end == null) 125 | { 126 | if (nodes[cursor] is Shortcode shortCode) 127 | { 128 | if (String.Equals(start.Identifier, shortCode.Identifier, StringComparison.OrdinalIgnoreCase)) 129 | { 130 | if (shortCode.Style == ShortcodeStyle.Open) 131 | { 132 | // We need to count all opening shortcodes matching the start to account for: 133 | // [a] [a] [/a] [/a] 134 | 135 | depth += 1; 136 | } 137 | else 138 | { 139 | depth -= 1; 140 | 141 | if (depth == 0) 142 | { 143 | tail = cursor; 144 | end = shortCode; 145 | } 146 | } 147 | } 148 | } 149 | 150 | cursor += 1; 151 | } 152 | 153 | // Is it a single tag? 154 | if (end == null) 155 | { 156 | cursor = head + 1; 157 | 158 | // If there are more than one open/close brace we don't evaluate the shortcode 159 | if (start.OpenBraces > 1 || start.CloseBraces > 1) 160 | { 161 | // We need to escape the braces if counts match 162 | var bracesToSkip = start.OpenBraces == start.CloseBraces ? 1 : 0; 163 | 164 | sb.Builder.Append('[', start.OpenBraces - bracesToSkip); 165 | sb.Builder.Append(input, start.SourceIndex + start.OpenBraces, start.SourceLength - start.CloseBraces - start.OpenBraces + 1); 166 | sb.Builder.Append(']', start.CloseBraces - bracesToSkip); 167 | } 168 | else 169 | { 170 | await AppendAsync(sb.Builder, input, start, null, context); 171 | } 172 | } 173 | else 174 | { 175 | // Standard braces are made of 1 brace on each edge 176 | var standardBraces = start.OpenBraces == 1 && start.CloseBraces == 1 && end.OpenBraces == 1 && end.CloseBraces == 1; 177 | var balancedBraces = start.OpenBraces == end.CloseBraces && start.CloseBraces == end.OpenBraces; 178 | 179 | if (standardBraces) 180 | { 181 | // Are the tags adjacent? 182 | if (tail - head == 1) 183 | { 184 | start.Content = ""; 185 | await AppendAsync(sb.Builder, input, start, end, context); 186 | } 187 | // Is there a single node between the tags? 188 | else if (tail - head == 2) 189 | { 190 | // Render the inner node (raw or shortcode) 191 | var content = nodes[head + 1]; 192 | 193 | // Set it to the start shortcode 194 | using (var sbContent = StringBuilderPool.GetInstance()) 195 | { 196 | await AppendAsync(sbContent.Builder, input, content, null, context); 197 | start.Content = sbContent.ToString(); 198 | } 199 | 200 | // Render the start shortcode 201 | await AppendAsync(sb.Builder, input, start, end, context); 202 | } 203 | // Fold the inner nodes 204 | else 205 | { 206 | start.Content = await FoldClosingTagsAsync(input, nodes, head + 1, tail - head - 1, context); 207 | await AppendAsync(sb.Builder, input, start, end, context); 208 | } 209 | } 210 | else 211 | { 212 | // Balanced braces represent an escape sequence, e.g. [[upper]foo[/upper]] -> [upper]foo[/upper] 213 | if (balancedBraces) 214 | { 215 | var bracesToSkip = start.OpenBraces == end.CloseBraces ? 1 : 0; 216 | 217 | sb.Builder.Append('[', start.OpenBraces - bracesToSkip); 218 | sb.Builder.Append(input, start.SourceIndex + start.OpenBraces, end.SourceIndex + end.SourceLength - end.CloseBraces - start.SourceIndex - start.OpenBraces + 1); 219 | sb.Builder.Append(']', end.CloseBraces - bracesToSkip); 220 | } 221 | // Unbalanced braces only evaluate inner content, e.g. [upper]foo[/upper]] 222 | else 223 | { 224 | // Are the tags adjacent? 225 | if (tail - head == 1) 226 | { 227 | AppendRawNode(sb.Builder, input, start); 228 | AppendRawNode(sb.Builder, input, end); 229 | } 230 | // Is there a single node between the tags? 231 | else if (tail - head == 2) 232 | { 233 | // Render the inner node (raw or shortcode) 234 | var content = nodes[head + 1]; 235 | 236 | AppendRawNode(sb.Builder, input, start); 237 | await AppendAsync(sb.Builder, input, content, null, context); 238 | AppendRawNode(sb.Builder, input, end); 239 | } 240 | // Fold the inner nodes 241 | else 242 | { 243 | var content = await FoldClosingTagsAsync(input, nodes, head + 1, tail - head - 1, context); 244 | 245 | AppendRawNode(sb.Builder, input, start); 246 | sb.Builder.Append(content); 247 | AppendRawNode(sb.Builder, input, end); 248 | } 249 | } 250 | } 251 | } 252 | } 253 | 254 | return sb.Builder.ToString(); 255 | } 256 | 257 | private void AppendRawNode(StringBuilder builder, string source, Shortcode node) 258 | { 259 | if (node.OpenBraces == node.CloseBraces) 260 | { 261 | builder.Append(source, node.SourceIndex, node.SourceLength + node.CloseBraces); 262 | } 263 | else 264 | { 265 | builder.Append(source, node.SourceIndex, node.SourceLength + 1); 266 | } 267 | } 268 | 269 | private async Task AppendAsync(StringBuilder builder, string source, Node start, Shortcode end, Context context) 270 | { 271 | switch (start) 272 | { 273 | case RawText raw: 274 | builder.Append(raw.Span); 275 | return; 276 | 277 | case Shortcode code: 278 | foreach (var provider in Providers) 279 | { 280 | var result = await provider.EvaluateAsync(code.Identifier, code.Arguments, code.Content, context); 281 | 282 | if (result != null) 283 | { 284 | builder.Append(result); 285 | return; 286 | } 287 | } 288 | 289 | // Return original content if no handler is found 290 | if (end == null) 291 | { 292 | // No closing tag 293 | builder.Append(source, code.SourceIndex, code.SourceLength + code.CloseBraces); 294 | } 295 | else 296 | { 297 | builder 298 | .Append(source, code.SourceIndex, code.SourceLength + code.CloseBraces) 299 | .Append(code.Content) 300 | .Append(source, end.SourceIndex, end.SourceLength + end.CloseBraces) 301 | ; 302 | } 303 | 304 | break; 305 | 306 | default: 307 | throw new NotSupportedException(); 308 | } 309 | } 310 | } 311 | } 312 | -------------------------------------------------------------------------------- /src/Shortcodes/ShortcodesScanner.cs: -------------------------------------------------------------------------------- 1 | using Parlot; 2 | using System; 3 | 4 | namespace Shortcodes 5 | { 6 | public class ShortcodesScanner : Scanner 7 | { 8 | public ShortcodesScanner(string buffer) : base(buffer) 9 | { 10 | 11 | } 12 | 13 | public bool ReadRawText(out TextSpan result) 14 | { 15 | var start = Cursor.Offset; 16 | 17 | while (Cursor.Match('[')) 18 | { 19 | Cursor.Advance(); 20 | } 21 | 22 | while (!Cursor.Match('[') && !Cursor.Eof) 23 | { 24 | Cursor.Advance(); 25 | } 26 | 27 | var length = Cursor.Offset - start; 28 | 29 | if (length == 0) 30 | { 31 | result = null; 32 | return false; 33 | } 34 | 35 | result = new TextSpan(Buffer, start, length); 36 | 37 | return true; 38 | } 39 | 40 | public bool ReadValue(out TextSpan result) 41 | { 42 | if (Cursor.Match(']') || Cursor.Match('\'') || Cursor.Match('"') || Character.IsWhiteSpaceOrNewLine(Cursor.Current)) 43 | { 44 | result = null; 45 | return false; 46 | } 47 | 48 | if (Cursor.Match("/]")) 49 | { 50 | result = null; 51 | return false; 52 | } 53 | 54 | var start = Cursor.Offset; 55 | 56 | while (!Character.IsWhiteSpaceOrNewLine(Cursor.Current) && !Cursor.Match(']')) 57 | { 58 | if (Cursor.Eof) 59 | { 60 | result = null; 61 | return false; 62 | } 63 | 64 | Cursor.Advance(); 65 | } 66 | 67 | result = new TextSpan(Buffer, start, Cursor.Offset - start); 68 | 69 | return true; 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/Shortcodes/StringBuilderPool.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. 2 | 3 | // define TRACE_LEAKS to get additional diagnostics that can lead to the leak sources. note: it will 4 | // make everything about 2-3x slower 5 | // 6 | // #define TRACE_LEAKS 7 | 8 | // define DETECT_LEAKS to detect possible leaks 9 | // #if DEBUG 10 | // #define DETECT_LEAKS //for now always enable DETECT_LEAKS in debug. 11 | // #endif 12 | 13 | using System; 14 | using System.Diagnostics; 15 | using System.Text; 16 | using System.Threading; 17 | 18 | #if DETECT_LEAKS 19 | using System.Runtime.CompilerServices; 20 | #endif 21 | 22 | namespace Shortcodes 23 | { 24 | /// 25 | /// Generic implementation of object pooling pattern with predefined pool size limit. The main 26 | /// purpose is that limited number of frequently used objects can be kept in the pool for 27 | /// further recycling. 28 | /// 29 | /// Notes: 30 | /// 1) it is not the goal to keep all returned objects. Pool is not meant for storage. If there 31 | /// is no space in the pool, extra returned objects will be dropped. 32 | /// 33 | /// 2) it is implied that if object was obtained from a pool, the caller will return it back in 34 | /// a relatively short time. Keeping checked out objects for long durations is ok, but 35 | /// reduces usefulness of pooling. Just new up your own. 36 | /// 37 | /// Not returning objects to the pool in not detrimental to the pool's work, but is a bad practice. 38 | /// Rationale: 39 | /// If there is no intent for reusing the object, do not use pool - just use "new". 40 | /// 41 | internal class ObjectPool where T : class 42 | { 43 | [DebuggerDisplay("{Value,nq}")] 44 | private struct Element 45 | { 46 | internal T Value; 47 | } 48 | 49 | /// 50 | /// Not using System.Func{T} because this file is linked into the (debugger) Formatter, 51 | /// which does not have that type (since it compiles against .NET 2.0). 52 | /// 53 | internal delegate T Factory(); 54 | 55 | // Storage for the pool objects. The first item is stored in a dedicated field because we 56 | // expect to be able to satisfy most requests from it. 57 | private T _firstItem; 58 | private readonly Element[] _items; 59 | 60 | // factory is stored for the lifetime of the pool. We will call this only when pool needs to 61 | // expand. compared to "new T()", Func gives more flexibility to implementers and faster 62 | // than "new T()". 63 | private readonly Factory _factory; 64 | 65 | #if DETECT_LEAKS 66 | private static readonly ConditionalWeakTable leakTrackers = new ConditionalWeakTable(); 67 | 68 | private class LeakTracker : IDisposable 69 | { 70 | private volatile bool disposed; 71 | 72 | #if TRACE_LEAKS 73 | internal volatile object Trace = null; 74 | #endif 75 | 76 | public void Dispose() 77 | { 78 | disposed = true; 79 | GC.SuppressFinalize(this); 80 | } 81 | 82 | private string GetTrace() 83 | { 84 | #if TRACE_LEAKS 85 | return Trace == null ? "" : Trace.ToString(); 86 | #else 87 | return "Leak tracing information is disabled. Define TRACE_LEAKS on ObjectPool`1.cs to get more info \n"; 88 | #endif 89 | } 90 | 91 | ~LeakTracker() 92 | { 93 | if (!this.disposed && !Environment.HasShutdownStarted) 94 | { 95 | var trace = GetTrace(); 96 | 97 | // If you are seeing this message it means that object has been allocated from the pool 98 | // and has not been returned back. This is not critical, but turns pool into rather 99 | // inefficient kind of "new". 100 | Debug.WriteLine($"TRACEOBJECTPOOLLEAKS_BEGIN\nPool detected potential leaking of {typeof(T)}. \n Location of the leak: \n {GetTrace()} TRACEOBJECTPOOLLEAKS_END"); 101 | } 102 | } 103 | } 104 | #endif 105 | 106 | internal ObjectPool(Factory factory) 107 | : this(factory, Environment.ProcessorCount * 2) 108 | { } 109 | 110 | internal ObjectPool(Factory factory, int size) 111 | { 112 | Debug.Assert(size >= 1); 113 | _factory = factory; 114 | _items = new Element[size - 1]; 115 | } 116 | 117 | private T CreateInstance() 118 | { 119 | var inst = _factory(); 120 | return inst; 121 | } 122 | 123 | /// 124 | /// Produces an instance. 125 | /// 126 | /// 127 | /// Search strategy is a simple linear probing which is chosen for it cache-friendliness. 128 | /// Note that Free will try to store recycled objects close to the start thus statistically 129 | /// reducing how far we will typically search. 130 | /// 131 | internal T Allocate() 132 | { 133 | // PERF: Examine the first element. If that fails, AllocateSlow will look at the remaining elements. 134 | // Note that the initial read is optimistically not synchronized. That is intentional. 135 | // We will interlock only when we have a candidate. in a worst case we may miss some 136 | // recently returned objects. Not a big deal. 137 | T inst = _firstItem; 138 | if (inst == null || inst != Interlocked.CompareExchange(ref _firstItem, null, inst)) 139 | { 140 | inst = AllocateSlow(); 141 | } 142 | 143 | #if DETECT_LEAKS 144 | var tracker = new LeakTracker(); 145 | leakTrackers.Add(inst, tracker); 146 | 147 | #if TRACE_LEAKS 148 | var frame = CaptureStackTrace(); 149 | tracker.Trace = frame; 150 | #endif 151 | #endif 152 | return inst; 153 | } 154 | 155 | private T AllocateSlow() 156 | { 157 | var items = _items; 158 | 159 | for (int i = 0; i < items.Length; i++) 160 | { 161 | // Note that the initial read is optimistically not synchronized. That is intentional. 162 | // We will interlock only when we have a candidate. in a worst case we may miss some 163 | // recently returned objects. Not a big deal. 164 | T inst = items[i].Value; 165 | if (inst != null) 166 | { 167 | if (inst == Interlocked.CompareExchange(ref items[i].Value, null, inst)) 168 | { 169 | return inst; 170 | } 171 | } 172 | } 173 | 174 | return CreateInstance(); 175 | } 176 | 177 | /// 178 | /// Returns objects to the pool. 179 | /// 180 | /// 181 | /// Search strategy is a simple linear probing which is chosen for it cache-friendliness. 182 | /// Note that Free will try to store recycled objects close to the start thus statistically 183 | /// reducing how far we will typically search in Allocate. 184 | /// 185 | internal void Free(T obj) 186 | { 187 | Validate(obj); 188 | ForgetTrackedObject(obj); 189 | 190 | if (_firstItem == null) 191 | { 192 | // Intentionally not using interlocked here. 193 | // In a worst case scenario two objects may be stored into same slot. 194 | // It is very unlikely to happen and will only mean that one of the objects will get collected. 195 | _firstItem = obj; 196 | } 197 | else 198 | { 199 | FreeSlow(obj); 200 | } 201 | } 202 | 203 | private void FreeSlow(T obj) 204 | { 205 | var items = _items; 206 | for (int i = 0; i < items.Length; i++) 207 | { 208 | if (items[i].Value == null) 209 | { 210 | // Intentionally not using interlocked here. 211 | // In a worst case scenario two objects may be stored into same slot. 212 | // It is very unlikely to happen and will only mean that one of the objects will get collected. 213 | items[i].Value = obj; 214 | break; 215 | } 216 | } 217 | } 218 | 219 | /// 220 | /// Removes an object from leak tracking. 221 | /// 222 | /// This is called when an object is returned to the pool. It may also be explicitly 223 | /// called if an object allocated from the pool is intentionally not being returned 224 | /// to the pool. This can be of use with pooled arrays if the consumer wants to 225 | /// return a larger array to the pool than was originally allocated. 226 | /// 227 | [Conditional("DEBUG")] 228 | internal void ForgetTrackedObject(T old, T replacement = null) 229 | { 230 | #if DETECT_LEAKS 231 | LeakTracker tracker; 232 | if (leakTrackers.TryGetValue(old, out tracker)) 233 | { 234 | tracker.Dispose(); 235 | leakTrackers.Remove(old); 236 | } 237 | else 238 | { 239 | var trace = CaptureStackTrace(); 240 | Debug.WriteLine($"TRACEOBJECTPOOLLEAKS_BEGIN\nObject of type {typeof(T)} was freed, but was not from pool. \n Callstack: \n {trace} TRACEOBJECTPOOLLEAKS_END"); 241 | } 242 | 243 | if (replacement != null) 244 | { 245 | tracker = new LeakTracker(); 246 | leakTrackers.Add(replacement, tracker); 247 | } 248 | #endif 249 | } 250 | 251 | #if DETECT_LEAKS 252 | private static Lazy _stackTraceType = new Lazy(() => Type.GetType("System.Diagnostics.StackTrace")); 253 | 254 | private static object CaptureStackTrace() 255 | { 256 | return Activator.CreateInstance(_stackTraceType.Value); 257 | } 258 | #endif 259 | 260 | [Conditional("DEBUG")] 261 | private void Validate(object obj) 262 | { 263 | Debug.Assert(obj != null, "freeing null?"); 264 | 265 | Debug.Assert(_firstItem != obj, "freeing twice?"); 266 | 267 | var items = _items; 268 | for (int i = 0; i < items.Length; i++) 269 | { 270 | var value = items[i].Value; 271 | if (value == null) 272 | { 273 | return; 274 | } 275 | 276 | Debug.Assert(value != obj, "freeing twice?"); 277 | } 278 | } 279 | } 280 | 281 | /// 282 | /// The usage is: 283 | /// var inst = PooledStringBuilder.GetInstance(); 284 | /// var sb = inst.builder; 285 | /// ... Do Stuff... 286 | /// ... sb.ToString() ... 287 | /// inst.Free(); 288 | /// 289 | internal sealed class StringBuilderPool : IDisposable 290 | { 291 | private const int DefaultCapacity = 50 * 1024; 292 | 293 | // global pool 294 | private static readonly ObjectPool s_poolInstance = CreatePool(); 295 | 296 | public readonly StringBuilder Builder = new StringBuilder(DefaultCapacity); 297 | private readonly ObjectPool _pool; 298 | 299 | private StringBuilderPool(ObjectPool pool) 300 | { 301 | Debug.Assert(pool != null); 302 | _pool = pool; 303 | } 304 | 305 | public int Length => Builder.Length; 306 | 307 | // if someone needs to create a private pool; 308 | /// 309 | /// If someone need to create a private pool 310 | /// 311 | /// The size of the pool. 312 | /// 313 | internal static ObjectPool CreatePool(int size = 16) 314 | { 315 | ObjectPool pool = null; 316 | pool = new ObjectPool(() => new StringBuilderPool(pool), size); 317 | return pool; 318 | } 319 | 320 | public static StringBuilderPool GetInstance() 321 | { 322 | var builder = s_poolInstance.Allocate(); 323 | Debug.Assert(builder.Builder.Length == 0); 324 | return builder; 325 | } 326 | 327 | public override string ToString() 328 | { 329 | return Builder.ToString(); 330 | } 331 | 332 | public void Dispose() 333 | { 334 | var builder = Builder; 335 | 336 | // Do not store builders that are too large. 337 | 338 | if (builder.Capacity == DefaultCapacity) 339 | { 340 | builder.Clear(); 341 | _pool.Free(this); 342 | } 343 | else 344 | { 345 | _pool.ForgetTrackedObject(this); 346 | } 347 | } 348 | } 349 | } -------------------------------------------------------------------------------- /tests/Shortcodes.Benchmarks/Program.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Running; 2 | 3 | namespace Shortcodes.Benchmarks 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | BenchmarkRunner.Run(); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/Shortcodes.Benchmarks/RenderBenchmarks.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Attributes; 2 | using System.Threading.Tasks; 3 | 4 | namespace Shortcodes.Benchmarks 5 | { 6 | [MemoryDiagnoser] 7 | [ShortRunJob] 8 | public class RenderBenchmarks 9 | { 10 | private readonly NamedShortcodeProvider _provider; 11 | 12 | private readonly ShortcodesProcessor _processor; 13 | 14 | public RenderBenchmarks() 15 | { 16 | _provider = new NamedShortcodeProvider 17 | { 18 | ["upper"] = (args, content, ctx) => new ValueTask(content.ToUpperInvariant()), 19 | }; 20 | 21 | _processor = new ShortcodesProcessor(_provider); 22 | } 23 | 24 | [Benchmark] 25 | public ValueTask Nop() => _processor.EvaluateAsync("Lorem ipsum dolor est"); 26 | 27 | [Benchmark] 28 | public ValueTask Upper() => _processor.EvaluateAsync("Lorem [upper]ipsum[/upper] dolor est"); 29 | 30 | [Benchmark] 31 | public ValueTask Unkown() => _processor.EvaluateAsync("Lorem [lower]ipsum[/lower] dolor est"); 32 | 33 | [Benchmark] 34 | public ValueTask Big() => _processor.EvaluateAsync("Lorem [upper]ipsum[/upper] dolor est Lorem [upper] Lorem [upper]ipsum[/upper] dolor est [/upper] dolor est Lorem [upper]ipsum[/upper] dolor est Lorem [upper] Lorem [upper]ipsum[/upper] dolor est [/upper] dolor est Lorem ipsum dolor est Lorem ipsum dolor est Lorem ipsum dolor est Lorem ipsum dolor est Lorem ipsum dolor est Lorem ipsum dolor est Lorem ipsum dolor est Lorem ipsum dolor est "); 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/Shortcodes.Benchmarks/Shortcodes.Benchmarks.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net8.0 6 | false 7 | 8 | true 9 | pdbonly 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /tests/Shortcodes.Tests/Shortcodes.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | false 6 | 7 | true 8 | pdbonly 9 | 10 | 11 | 12 | 13 | 14 | 15 | all 16 | runtime; build; native; contentfiles; analyzers; buildtransitive 17 | 18 | 19 | all 20 | runtime; build; native; contentfiles; analyzers; buildtransitive 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /tests/Shortcodes.Tests/ShortcodesParserTests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Text; 3 | using Xunit; 4 | 5 | namespace Shortcodes.Tests 6 | { 7 | public class ShortcodesParserTests 8 | { 9 | private readonly StringBuilder _builder = new(); 10 | 11 | private string EncodeNodes(IEnumerable nodes) 12 | { 13 | _builder.Clear(); 14 | 15 | foreach (var node in nodes) 16 | { 17 | switch (node) 18 | { 19 | case Shortcode shortcode: 20 | _builder.Append('['); 21 | 22 | if (shortcode.Style == ShortcodeStyle.Close) 23 | { 24 | _builder.Append('/'); 25 | } 26 | 27 | _builder.Append(shortcode.Identifier); 28 | 29 | if (shortcode.Arguments.Any()) 30 | { 31 | foreach (var argument in shortcode.Arguments) 32 | { 33 | _builder.Append(' ').Append(argument.Key).Append('=').Append(argument.Value); 34 | } 35 | } 36 | 37 | if (shortcode.Style == ShortcodeStyle.SelfClosing) 38 | { 39 | _builder.Append(" /"); 40 | } 41 | 42 | _builder.Append(']'); 43 | break; 44 | 45 | case RawText raw: 46 | _builder.Append($"R({raw.Span.Length})"); 47 | break; 48 | } 49 | } 50 | 51 | return _builder.ToString(); 52 | } 53 | 54 | [Theory] 55 | [InlineData("[hello/]", "[hello /]")] 56 | [InlineData("[hello /]", "[hello /]")] 57 | [InlineData("[ hello /]", "[hello /]")] 58 | [InlineData(" [hello /] ", "R(1)[hello /]R(1)")] 59 | public void ShouldScanSelfClosingTags(string input, string encoded) 60 | { 61 | var nodes = new ShortcodesParser().Parse(input); 62 | var result = EncodeNodes(nodes); 63 | 64 | Assert.Equal(encoded, result); 65 | } 66 | 67 | [Theory] 68 | [InlineData("[/hello]", "[/hello]")] 69 | [InlineData("[/hello ]", "[/hello]")] 70 | [InlineData("[/ hello]", "[/hello]")] 71 | [InlineData(" [/hello] ", "R(1)[/hello]R(1)")] 72 | public void ShouldScanCloseTags(string input, string encoded) 73 | { 74 | var nodes = new ShortcodesParser().Parse(input); 75 | var result = EncodeNodes(nodes); 76 | 77 | Assert.Equal(encoded, result); 78 | } 79 | 80 | [Theory] 81 | [InlineData("[/hello", "R(7)")] 82 | [InlineData("[ /hello ]", "R(10)")] 83 | [InlineData("[/ hello[", "R(8)R(1)")] 84 | public void ShouldIgnoreMalformedTags(string input, string encoded) 85 | { 86 | var nodes = new ShortcodesParser().Parse(input); 87 | var result = EncodeNodes(nodes); 88 | 89 | Assert.Equal(encoded, result); 90 | } 91 | 92 | [Theory] 93 | [InlineData("[hello][/hello]", "[hello][/hello]")] 94 | [InlineData("[hello] [/hello]", "[hello]R(1)[/hello]")] 95 | [InlineData("a[hello]b[/hello]c", "R(1)[hello]R(1)[/hello]R(1)")] 96 | [InlineData("a[hello]b[/hello]c[hello]d[/hello]e", "R(1)[hello]R(1)[/hello]R(1)[hello]R(1)[/hello]R(1)")] 97 | public void ShouldScanMixedTags(string input, string encoded) 98 | { 99 | var nodes = new ShortcodesParser().Parse(input); 100 | var result = EncodeNodes(nodes); 101 | 102 | Assert.Equal(encoded, result); 103 | } 104 | 105 | [Theory] 106 | [InlineData("[hello a='b']", "[hello a=b]")] 107 | [InlineData("[hello a='b' c=\"d\"]", "[hello a=b c=d]")] 108 | [InlineData("[hello 'a']", "[hello 0=a]")] 109 | [InlineData("[hello 'a' b='c' 'd']", "[hello 0=a b=c 1=d]")] 110 | [InlineData("[hello 123]", "[hello 0=123]")] 111 | public void ShouldScanArguments(string input, string encoded) 112 | { 113 | var nodes = new ShortcodesParser().Parse(input); 114 | var result = EncodeNodes(nodes); 115 | 116 | Assert.Equal(encoded, result); 117 | } 118 | 119 | [Theory] 120 | [InlineData("[hello a='b]", "R(12)")] 121 | // \z is not a valid escape sequence 122 | [InlineData("[hello '\\z']", "R(12)")] 123 | public void ShouldIgnoreMalformedArguments(string input, string encoded) 124 | { 125 | var nodes = new ShortcodesParser().Parse(input); 126 | var result = EncodeNodes(nodes); 127 | 128 | Assert.Equal(encoded, result); 129 | } 130 | 131 | [Theory] 132 | [InlineData("[h a='\\u03A9']", "[h a=Ω]")] 133 | [InlineData("[h a='\\xe9']", "[h a=é]")] 134 | [InlineData("[h a='\\xE9']", "[h a=é]")] 135 | // This is not a valid string (invalid escape sequence), and not a valid value as it starts with ' 136 | [InlineData("[h a='\\z']", "R(10)")] 137 | [InlineData("[h a='\\\\']", "[h a=\\]")] 138 | [InlineData("[h a='\\\"']", "[h a=\"]")] 139 | [InlineData("[h a='\\\'']", "[h a=']")] 140 | [InlineData("[h a='\\b']", "[h a=\b]")] 141 | [InlineData("[h a='\\f']", "[h a=\f]")] 142 | [InlineData("[h a='\\n']", "[h a=\n]")] 143 | [InlineData("[h a='\\r']", "[h a=\r]")] 144 | [InlineData("[h a='\\t']", "[h a=\t]")] 145 | [InlineData("[h a='\\v']", "[h a=\v]")] 146 | public void ShouldEscapeStrings(string input, string encoded) 147 | { 148 | var nodes = new ShortcodesParser().Parse(input); 149 | var result = EncodeNodes(nodes); 150 | 151 | Assert.Equal(encoded, result); 152 | } 153 | 154 | [Theory] 155 | [InlineData("[h a='\\u0']", "R(11)")] 156 | [InlineData("[h a='\\xe']", "[h a=\xe]")] 157 | public void ShouldNotParseInvalidEscapeSequence(string input, string encoded) 158 | { 159 | var nodes = new ShortcodesParser().Parse(input); 160 | var result = EncodeNodes(nodes); 161 | 162 | Assert.Equal(encoded, result); 163 | } 164 | 165 | [Theory] 166 | [InlineData("[h a='\"']", "[h a=\"]")] 167 | [InlineData("[h a=\"'\"]", "[h a=']")] 168 | public void ShouldStringsWitBothQuotes(string input, string encoded) 169 | { 170 | var nodes = new ShortcodesParser().Parse(input); 171 | var result = EncodeNodes(nodes); 172 | 173 | Assert.Equal(encoded, result); 174 | } 175 | 176 | [Theory] 177 | [InlineData("[hello a='b']", "[hello a=b]")] 178 | [InlineData("[[hello a='b']]", "[hello a=b]")] 179 | [InlineData("[[[hello a='b']]]", "[hello a=b]")] 180 | [InlineData("[hello a='b']]", "[hello a=b]")] 181 | [InlineData("[hello a='b']]]", "[hello a=b]")] 182 | [InlineData("[hello a='b']]]]", "[hello a=b]")] 183 | [InlineData("[[hello a='b']", "[hello a=b]")] 184 | [InlineData("[[[hello a='b']", "[hello a=b]")] 185 | [InlineData("[[[[hello a='b']", "[hello a=b]")] 186 | public void ShouldIncludeOpenAndCloseBraces(string input, string encoded) 187 | { 188 | var nodes = new ShortcodesParser().Parse(input); 189 | var result = EncodeNodes(nodes); 190 | 191 | Assert.Equal(encoded, result); 192 | } 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /tests/Shortcodes.Tests/ShortcodesProcessorTests.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using Xunit; 3 | 4 | namespace Shortcodes.Tests 5 | { 6 | public class ShortcodesProcessorTests 7 | { 8 | private readonly NamedShortcodeProvider _provider; 9 | 10 | public ShortcodesProcessorTests() 11 | { 12 | _provider = new NamedShortcodeProvider 13 | { 14 | ["hello"] = (args, content, ctx) => new ValueTask("Hello world!"), 15 | ["named_or_default"] = (args, content, ctx) => new ValueTask("Hello " + args.NamedOrDefault("name")), 16 | ["upper"] = (args, content, ctx) => new ValueTask(content.ToUpperInvariant()), 17 | ["positional"] = (args, content, ctx) => 18 | { 19 | string result = ""; 20 | 21 | for (var i=0; i(result); 27 | } 28 | }; 29 | } 30 | 31 | [Theory] 32 | [InlineData(null)] 33 | [InlineData("")] 34 | [InlineData(" ")] 35 | [InlineData(" ")] 36 | [InlineData("a")] 37 | [InlineData("a b c")] 38 | [InlineData("Hello World!")] 39 | [InlineData("Hello [ World!")] 40 | [InlineData("Hello ] World!")] 41 | [InlineData("Hello ] [ World!")] 42 | public async Task DoesntProcessInputsWithoutBrackets(string input) 43 | { 44 | var parser = new ShortcodesProcessor(); 45 | 46 | Assert.Same(input, await parser.EvaluateAsync(input)); 47 | } 48 | 49 | [Theory] 50 | [InlineData("[hello]", "Hello world!")] 51 | [InlineData(" [hello] ", " Hello world! ")] 52 | [InlineData("a [hello] b", "a Hello world! b")] 53 | public async Task ProcessShortcodes(string input, string expected) 54 | { 55 | var parser = new ShortcodesProcessor(_provider); 56 | Assert.Equal(expected, await parser.EvaluateAsync(input)); 57 | } 58 | 59 | [Theory] 60 | [InlineData("[[hello]", "[[hello]")] 61 | [InlineData("[[[hello]", "[[[hello]")] 62 | [InlineData("[hello]]", "[hello]]")] 63 | [InlineData("[hello]]]", "[hello]]]")] 64 | [InlineData("[[upper]a[/upper]", "[[upper]a[/upper]")] 65 | [InlineData("[upper]a[/upper]]", "[upper]a[/upper]]")] 66 | public async Task IgnoresIncompleteShortcodes(string input, string expected) 67 | { 68 | var parser = new ShortcodesProcessor(_provider); 69 | 70 | Assert.Equal(expected, await parser.EvaluateAsync(input)); 71 | } 72 | 73 | [Theory] 74 | [InlineData("[hello][foo]", "Hello world![foo]")] 75 | [InlineData("[hello ][foo]", "Hello world![foo]")] 76 | [InlineData("[foo][hello]", "[foo]Hello world!")] 77 | [InlineData("[upper][foo][/upper]", "[FOO]")] 78 | [InlineData("[upper] [ foo ] [/upper]", " [ FOO ] ")] 79 | [InlineData("[[upper] [[] foo []] [/upper]]", "[upper] [[] foo []] [/upper]")] 80 | [InlineData("[[[upper] [[] foo []] [/upper]]]", "[[upper] [[] foo []] [/upper]]")] 81 | [InlineData("[upper] [hello] [/upper]", " HELLO WORLD! ")] 82 | [InlineData("[foo arg1=blah] ", "[foo arg1=blah] ")] 83 | [InlineData("[ foo arg1=blah ]", "[ foo arg1=blah ]")] 84 | [InlineData("[ foo arg1=blah ] [/foo]", "[ foo arg1=blah ] [/foo]")] 85 | [InlineData("[/foo]", "[/foo]")] 86 | [InlineData(" [/ foo ] ", " [/ foo ] ")] 87 | [InlineData(" [a] [/a] ", " [a] [/a] ")] 88 | [InlineData("[a][hello][/a]", "[a]Hello world![/a]")] 89 | [InlineData("[/a][a][hello][a][/a]", "[/a][a]Hello world![a][/a]")] 90 | [InlineData(" [a][hello][/a] ", " [a]Hello world![/a] ")] 91 | [InlineData(" [a] [hello] [/a] ", " [a] Hello world! [/a] ")] 92 | [InlineData(" [a] [hello] [/a][/a] ", " [a] Hello world! [/a][/a] ")] 93 | [InlineData(" [a]]] [hello] [/a][[/a] ", " [a]]] Hello world! [/a][[/a] ")] 94 | [InlineData(" [a]]] [/a]"," [a]]] [/a]")] 95 | [InlineData("[a]]][/a]","[a]]][/a]")] 96 | [InlineData(" [a]]] [hello]"," [a]]] Hello world!")] 97 | public async Task IngoreUnknownShortcodes(string input, string expected) 98 | { 99 | var parser = new ShortcodesProcessor(_provider); 100 | 101 | Assert.Equal(expected, await parser.EvaluateAsync(input)); 102 | } 103 | 104 | [Theory] 105 | [InlineData("[[hello]]", "[hello]")] 106 | [InlineData("[[[hello]]]", "[[hello]]")] 107 | [InlineData("[[[[hello]]]]", "[[[hello]]]")] 108 | public async Task EscapeSingleShortcodes(string input, string expected) 109 | { 110 | var parser = new ShortcodesProcessor(_provider); 111 | 112 | Assert.Equal(expected, await parser.EvaluateAsync(input)); 113 | } 114 | 115 | [Theory] 116 | [InlineData("[[upper]a[/upper]]", "[upper]a[/upper]")] 117 | [InlineData("[[[upper]a[/upper]]]", "[[upper]a[/upper]]")] 118 | [InlineData("[[[[upper]a[/upper]]]]", "[[[upper]a[/upper]]]")] 119 | public async Task EscapeEnclosedShortcodes(string input, string expected) 120 | { 121 | var parser = new ShortcodesProcessor(_provider); 122 | 123 | Assert.Equal(expected, await parser.EvaluateAsync(input)); 124 | } 125 | 126 | [Theory] 127 | [InlineData("[hello]a[/hello]", "Hello world!")] 128 | public async Task ProcessClosingShortcodes(string input, string expected) 129 | { 130 | var parser = new ShortcodesProcessor(_provider); 131 | 132 | Assert.Equal(expected, await parser.EvaluateAsync(input)); 133 | } 134 | 135 | [Theory] 136 | [InlineData("[named_or_default name='world!']", "Hello world!")] 137 | [InlineData("[named_or_default 'world!']", "Hello world!")] 138 | public async Task NamedOrDefaultArguments(string input, string expected) 139 | { 140 | var parser = new ShortcodesProcessor(_provider); 141 | 142 | Assert.Equal(expected, await parser.EvaluateAsync(input)); 143 | } 144 | 145 | [Theory] 146 | [InlineData("[named_or_default Name='world!']", "Hello world!")] 147 | [InlineData("[named_or_default NAME='world!']", "Hello world!")] 148 | public async Task ArgumentsAreCaseInsensitive(string input, string expected) 149 | { 150 | var parser = new ShortcodesProcessor(_provider); 151 | 152 | Assert.Equal(expected, await parser.EvaluateAsync(input)); 153 | } 154 | 155 | [Theory] 156 | [InlineData("[upper]lorem[/upper]", "LOREM")] 157 | [InlineData("[upper]lorem[/upper] [upper]ipsum[/upper]", "LOREM IPSUM")] 158 | [InlineData("[upper]lorem [upper]ipsum[/upper][/upper]", "LOREM IPSUM")] 159 | [InlineData("[upper]lorem [hello][/upper]", "LOREM HELLO WORLD!")] 160 | [InlineData("[upper]lorem [upper]ipsum[/upper] [upper]dolor[/upper][/upper]", "LOREM IPSUM DOLOR")] 161 | [InlineData("[upper][/upper]", "")] 162 | public async Task FoldsRecursiceShortcodes(string input, string expected) 163 | { 164 | var parser = new ShortcodesProcessor(_provider); 165 | 166 | Assert.Equal(expected, await parser.EvaluateAsync(input)); 167 | } 168 | 169 | [Fact] 170 | public async Task PositionalArgumentMixedWithNamedArguments() 171 | { 172 | var provider = new NamedShortcodeProvider 173 | { 174 | ["hello"] = (args, content, ctx) => 175 | { 176 | Assert.Equal("1", args.At(0)); 177 | Assert.Equal("b", args.At(1)); 178 | Assert.Equal("d", args.Named("c")); 179 | Assert.Equal("123", args.At(2)); 180 | 181 | return new ValueTask(""); 182 | } 183 | }; 184 | 185 | var parser = new ShortcodesProcessor(_provider); 186 | await parser.EvaluateAsync("[hello 1 b c=d 123]"); 187 | } 188 | 189 | [Theory] 190 | [InlineData("1234")] 191 | [InlineData("true")] 192 | [InlineData("123_456")] 193 | [InlineData("http://github.com")] 194 | [InlineData("http://github.com?foo=bar")] 195 | public async Task ValuesAreParsed(string input) 196 | { 197 | var parser = new ShortcodesProcessor(_provider); 198 | 199 | Assert.Equal($"0:{input};", await parser.EvaluateAsync($"[positional {input}]")); 200 | } 201 | 202 | [Fact] 203 | public async Task ContextIsShareAcrossShortcodes() 204 | { 205 | var parser = new ShortcodesProcessor(new NamedShortcodeProvider 206 | { 207 | ["inc"] = (args, content, ctx) => { ctx["x"] = ctx.GetOrSetValue("x", 0) + 1; return new ValueTask(ctx["x"].ToString()); }, 208 | ["val"] = (args, content, ctx) => new ValueTask(ctx["x"].ToString()) 209 | }); 210 | 211 | Assert.Equal("122", await parser.EvaluateAsync($"[inc][inc][val]")); 212 | } 213 | 214 | [Fact] 215 | public async Task ShouldUseContextValue() 216 | { 217 | var parser = new ShortcodesProcessor(new NamedShortcodeProvider 218 | { 219 | ["hello"] = (args, content, ctx) => { return new ValueTask("message: " + ctx["message"].ToString()); } 220 | }); 221 | 222 | Assert.Equal("message: Hello World!", await parser.EvaluateAsync($"[hello]", new Context { ["message"] = "Hello World!" })); 223 | } 224 | } 225 | } 226 | --------------------------------------------------------------------------------