Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions .github/workflows/JSON-LD-Build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
Install-Module -Name Pester -Repository PSGallery -Force -Scope CurrentUser -MaximumVersion $PesterMaxVersion -SkipPublisherCheck -AllowClobber
Import-Module Pester -Force -PassThru -MaximumVersion $PesterMaxVersion} @Parameters
- name: Check out repository
uses: actions/checkout@v4
uses: actions/checkout@v2
- name: RunPester
id: RunPester
shell: pwsh
Expand Down Expand Up @@ -92,6 +92,9 @@ jobs:
$result =
Invoke-Pester -PassThru -Verbose -OutputFile ".\$moduleName.TestResults.xml" -OutputFormat NUnitXml @codeCoverageParameters

"::set-output name=TotalCount::$($result.TotalCount)",
"::set-output name=PassedCount::$($result.PassedCount)",
"::set-output name=FailedCount::$($result.FailedCount)" | Out-Host
if ($result.FailedCount -gt 0) {
"::debug:: $($result.FailedCount) tests failed"
foreach ($r in $result.TestResult) {
Expand Down Expand Up @@ -491,20 +494,6 @@ jobs:
steps:
- name: Check out repository
uses: actions/checkout@v2
- name: GitLogger
uses: GitLogging/GitLoggerAction@main
id: GitLogger
- name: Use PSSVG Action
uses: StartAutomating/PSSVG@main
id: PSSVG
- name: Use PipeScript Action
uses: StartAutomating/PipeScript@main
id: PipeScript
- name: UseEZOut
uses: StartAutomating/EZOut@master
- name: UseHelpOut
uses: StartAutomating/HelpOut@master
- name: Use PSJekyll Action
uses: PowerShellWeb/PSJekyll@main
id: PSJekyll

23 changes: 1 addition & 22 deletions Build/GitHub/Jobs/JsonLD.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,7 @@
@{
name = 'Check out repository'
uses = 'actions/checkout@v2'
},
@{
name = 'GitLogger'
uses = 'GitLogging/GitLoggerAction@main'
id = 'GitLogger'
},
@{
name = 'Use PSSVG Action'
uses = 'StartAutomating/PSSVG@main'
id = 'PSSVG'
},
@{
name = 'Use PipeScript Action'
uses = 'StartAutomating/PipeScript@main'
id = 'PipeScript'
},
'RunEZOut',
'RunHelpOut',
@{
name = 'Use PSJekyll Action'
uses = 'PowerShellWeb/PSJekyll@main'
id = 'PSJekyll'
}
'RunEZOut'
)
}
39 changes: 39 additions & 0 deletions Build/JSON-LD.ezout.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#requires -Module EZOut
# Install-Module EZOut or https://github.com/StartAutomating/EZOut
$myFile = $MyInvocation.MyCommand.ScriptBlock.File
$myModuleName = $MyInvocation.MyCommand.Name -replace '\.ezout.ps1$'
$myRoot = $myFile | Split-Path | Split-Path
Push-Location $myRoot
$formatting = @(
# Add your own Write-FormatView here,
# or put them in a Formatting or Views directory
foreach ($potentialDirectory in 'Formatting','Views','Types') {
Join-Path $myRoot $potentialDirectory |
Get-ChildItem -ea ignore |
Import-FormatView -FilePath {$_.Fullname}
}
)

$destinationRoot = $myRoot

if ($formatting) {
$myFormatFilePath = Join-Path $destinationRoot "$myModuleName.format.ps1xml"
# You can also output to multiple paths by passing a hashtable to -OutputPath.
$formatting | Out-FormatData -Module $MyModuleName -OutputPath $myFormatFilePath
}

$types = @(
# Add your own Write-TypeView statements here
# or declare them in the 'Types' directory
Join-Path $myRoot Types |
Get-Item -ea ignore |
Import-TypeView

)

if ($types) {
$myTypesFilePath = Join-Path $destinationRoot "$myModuleName.types.ps1xml"
# You can also output to multiple paths by passing a hashtable to -OutputPath.
$types | Out-TypeData -OutputPath $myTypesFilePath
}
Pop-Location
26 changes: 24 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
> Like It? [Star It](https://github.com/PowerShellWeb/JSON-LD)
> Love It? [Support It](https://github.com/sponsors/StartAutomating)
Please:

* [Like, Share, and Subscribe](https://github.com/PowerShellWeb/JSON-LD)
* [Support Us](https://github.com/sponsors/StartAutomating)

---

## JSON-LD 0.1.1

* Updating Examples (#13)
* Simplfiying module scaffolding (#15)
* Building types with EZOut (#5)
* Supporting file input (#23)
* `Get-JSONLD -as`
* `Get-JSONLD -as json` (#16)
* `Get-JSONLD -as html` (#17)
* `Get-JSONLD -as script` (#18)
* `Get-JSONLD -as xml` (#19)
* Adding conversion to JsonSchema (#21)
* Adding conversion to At Protocol Lexicons (#22)

---

## JSON-LD 0.1

Caching JSON-LD requests

---

## JSON-LD 0.0.1

Get Linked Data from any page
Expand Down
170 changes: 149 additions & 21 deletions Commands/Get-JsonLD.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function Get-JsonLD {
This is a format used by many websites to provide structured data about their content.
.EXAMPLE
# Want to get information about a movie? Linked Data to the rescue!
Get-JsonLD -Url https://www.imdb.com/title/tt0211915/
Get-JsonLD -Url https://letterboxd.com/film/amelie/
.EXAMPLE
# Want information about an article? Lots of news sites use this format.
Get-JsonLD https://www.thebulwark.com/p/mahmoud-khalil-immigration-detention-first-amendment-free-speech-rights
Expand All @@ -21,9 +21,30 @@ function Get-JsonLD {
param(
# The URL that may contain JSON-LD data
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[Alias('href')]
[Uri]
$Url,

<#

If set, will the output as:

|as|is|
|-|-|
|html|the response as text|
|json|the match as json|
|*jsonld`|ld`|linkedData*|the match as linked data|'
|script|the script tag|
|xml|the script tag, as xml|

#>
[ValidateSet('html', 'json', 'jsonld', 'ld', 'linkedData', 'script', 'xml')]
[string]
$as = 'jsonld',

[switch]
$RawHtml,

# If set, will force the request to be made even if the URL has already been cached.
[switch]
$Force
Expand All @@ -46,39 +67,146 @@ application/ld\+json # The type that indicates linked d
'@, 'IgnoreCase,IgnorePatternWhitespace','00:00:00.1')

# Initialize the cache for JSON-LD requests
if (-not $script:JsonLDRequestCache) {
$script:JsonLDRequestCache = [Ordered]@{}
if (-not $script:Cache) {
$script:Cache = [Ordered]@{}
}

filter output {
$in = $_
$mySelf = $MyInvocation.MyCommand
if ($in.'@context' -is [string]) {
$context = $in.'@context'
}
if ($in.'@graph') {
if ($in.pstypenames -ne 'application/ld+json') {
$in.pstypenames.insert(0,'application/ld+json')
}
foreach ($graphObject in $in.'@graph') {
$null = $graphObject |
& $mySelf
}
}
elseif ($in.'@type') {

$typeName = if ($context) {
$context, $in.'@type' -join '/'
} else {
$in.'@type'
}

if ($in.pstypenames -ne 'application/ld+json') {
$in.pstypenames.insert(0,'application/ld+json')
}
if ($in.pstypenames -ne $typeName) {
$in.pstypenames.insert(0,$typeName)
}

foreach ($property in $in.psobject.properties) {
if ($property.value.'@type') {
$null = $property.value |
& $mySelf
}
}
}
$in
}

$foreachFile = {
$inFile = $_.FullName
try {

Get-Content -LiteralPath $_.FullName -Raw |
ConvertFrom-Json |
output
} catch {
Write-Verbose "$($inFile.FullName) : $_"
}
}
}

process {
if ($url.IsFile -or
-not $url.AbsoluteUri
) {
if (Test-Path $url.OriginalString) {
Get-ChildItem $url.OriginalString -File |
Foreach-Object $foreachFile
} elseif ($MyInvocation.MyCommand.Module -and
(Test-Path (
Join-Path (
$MyInvocation.MyCommand.Module | Split-Path
) $url.OriginalString
))
) {
Get-ChildItem -Path (
Join-Path (
$MyInvocation.MyCommand.Module | Split-Path
) $url.OriginalString
) -File |
Foreach-Object $foreachFile
}

return
}

$restResponse =
if ($Force -or -not $script:JsonLDRequestCache[$url]) {
$script:JsonLDRequestCache[$url] = Invoke-RestMethod -Uri $Url
$script:JsonLDRequestCache[$url]
if ($Force -or -not $script:Cache[$url]) {
$script:Cache[$url] = Invoke-RestMethod -Uri $Url
$script:Cache[$url]
} else {
$script:JsonLDRequestCache[$url]
$script:Cache[$url]
}

if ($as -eq 'html') {
return $restResponse
}

# Find all linked data tags within the response
foreach ($match in $linkedDataRegex.Matches("$restResponse")) {
# If we want the result as xml
if ($As -eq 'xml') {
# try to cast it
$matchXml ="$match" -as [xml]
if ($matchXml) {
# and output it if found.
$matchXml
continue
} else {
# otherwise, fall back to the `<script>` tag
$As = 'script'
}
}

# If we want the tag, that should be the whole match
if ($As -eq 'script') {
"$match"
continue
}

# If we want it as json, we have a match group.
if ($As -eq 'json') {
$match.Groups['JsonContent'].Value
continue
}
# Otherwise, we want it as linked data, so convert from the json
foreach ($jsonObject in
$match.Groups['JsonContent'].Value |
ConvertFrom-Json
) {
if ($jsonObject.'@type') {
$schemaType = $jsonObject.'@context',$jsonObject.'@type' -ne '' -join '/'
$jsonObject.pstypenames.insert(0, $schemaType)
# If there was a `@type` or `@graph` property
if (
$jsonObject.'@type' -or
$jsonObject.'@graph'
) {
# output the object as jsonld
$jsonObject | output
continue
}
# If there is neither a `@type` or a `@graph`
else {
# just output the object.
$jsonObject
} elseif ($jsonObject.'@graph') {
foreach ($graphObject in $jsonObject.'@graph') {
if ($graphObject.'@type') {
$graphObject.pstypenames.insert(0, $graphObject.'@type')
}
$graphObject
}
} else {
$jsonObject
}

}
}
}
}
Expand Down
26 changes: 0 additions & 26 deletions JSON-LD.ps.psm1

This file was deleted.

Loading
Loading