Skip to content

Commit da76764

Browse files
feat: Get-JSONLD file support ( Fixes #23 )
1 parent 22d8c6b commit da76764

1 file changed

Lines changed: 88 additions & 27 deletions

File tree

Commands/Get-JsonLD.ps1

Lines changed: 88 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function Get-JsonLD {
88
This is a format used by many websites to provide structured data about their content.
99
.EXAMPLE
1010
# Want to get information about a movie? Linked Data to the rescue!
11-
Get-JsonLD -Url https://letterboxd.com/film/amelie/
11+
Get-JsonLD -Url https://letterboxd.com/film/amelie/
1212
.EXAMPLE
1313
# Want information about an article? Lots of news sites use this format.
1414
Get-JsonLD https://www.thebulwark.com/p/mahmoud-khalil-immigration-detention-first-amendment-free-speech-rights
@@ -37,8 +37,7 @@ function Get-JsonLD {
3737
|script|the script tag|
3838
|xml|the script tag, as xml|
3939
40-
#>
41-
40+
#>
4241
[ValidateSet('html', 'json', 'jsonld', 'ld', 'linkedData', 'script', 'xml')]
4342
[string]
4443
$as = 'jsonld',
@@ -71,9 +70,85 @@ application/ld\+json # The type that indicates linked d
7170
if (-not $script:Cache) {
7271
$script:Cache = [Ordered]@{}
7372
}
73+
74+
filter output {
75+
$in = $_
76+
$mySelf = $MyInvocation.MyCommand
77+
if ($in.'@context' -is [string]) {
78+
$context = $in.'@context'
79+
}
80+
if ($in.'@graph') {
81+
if ($in.pstypenames -ne 'application/ld+json') {
82+
$in.pstypenames.insert(0,'application/ld+json')
83+
}
84+
foreach ($graphObject in $in.'@graph') {
85+
$null = $graphObject |
86+
& $mySelf
87+
}
88+
}
89+
elseif ($in.'@type') {
90+
91+
$typeName = if ($context) {
92+
$context, $in.'@type' -join '/'
93+
} else {
94+
$in.'@type'
95+
}
96+
97+
if ($in.pstypenames -ne 'application/ld+json') {
98+
$in.pstypenames.insert(0,'application/ld+json')
99+
}
100+
if ($in.pstypenames -ne $typeName) {
101+
$in.pstypenames.insert(0,$typeName)
102+
}
103+
104+
foreach ($property in $in.psobject.properties) {
105+
if ($property.value.'@type') {
106+
$null = $property.value |
107+
& $mySelf
108+
}
109+
}
110+
}
111+
$in
112+
}
113+
114+
$foreachFile = {
115+
$inFile = $_.FullName
116+
try {
117+
118+
Get-Content -LiteralPath $_.FullName -Raw |
119+
ConvertFrom-Json |
120+
output
121+
} catch {
122+
Write-Verbose "$($inFile.FullName) : $_"
123+
}
124+
}
74125
}
75126

76127
process {
128+
if ($url.IsFile -or
129+
-not $url.AbsoluteUri
130+
) {
131+
if (Test-Path $url.OriginalString) {
132+
Get-ChildItem $url.OriginalString -File |
133+
Foreach-Object $foreachFile
134+
} elseif ($MyInvocation.MyCommand.Module -and
135+
(Test-Path (
136+
Join-Path (
137+
$MyInvocation.MyCommand.Module | Split-Path
138+
) $url.OriginalString
139+
))
140+
) {
141+
Get-ChildItem -Path (
142+
Join-Path (
143+
$MyInvocation.MyCommand.Module | Split-Path
144+
) $url.OriginalString
145+
) -File |
146+
Foreach-Object $foreachFile
147+
}
148+
149+
return
150+
}
151+
77152
$restResponse =
78153
if ($Force -or -not $script:Cache[$url]) {
79154
$script:Cache[$url] = Invoke-RestMethod -Uri $Url
@@ -84,8 +159,7 @@ application/ld\+json # The type that indicates linked d
84159

85160
if ($as -eq 'html') {
86161
return $restResponse
87-
}
88-
162+
}
89163

90164
# Find all linked data tags within the response
91165
foreach ($match in $linkedDataRegex.Matches("$restResponse")) {
@@ -119,28 +193,15 @@ application/ld\+json # The type that indicates linked d
119193
$match.Groups['JsonContent'].Value |
120194
ConvertFrom-Json
121195
) {
122-
# If there was a `@type` property
123-
if ($jsonObject.'@type') {
124-
# all we need to do is decorate the object
125-
# If we combine the `@context` and `@type` property, we should have a schema url
126-
$schemaType = $jsonObject.'@context',$jsonObject.'@type' -ne '' -join '/'
127-
# and we can make that the typename
128-
$jsonObject.pstypenames.insert(0, $schemaType)
129-
# and show the object.
130-
$jsonObject
131-
}
132-
# If there was a `@graph` property
133-
elseif ($jsonObject.'@graph') {
134-
# we can display all items in the graph
135-
foreach ($graphObject in $jsonObject.'@graph') {
136-
# each of them will tell us it's `@type`
137-
if ($graphObject.'@type') {
138-
# and we can decorate each object appropriately
139-
$graphObject.pstypenames.insert(0, $graphObject.'@type')
140-
}
141-
$graphObject
142-
}
143-
}
196+
# If there was a `@type` or `@graph` property
197+
if (
198+
$jsonObject.'@type' -or
199+
$jsonObject.'@graph'
200+
) {
201+
# output the object as jsonld
202+
$jsonObject | output
203+
continue
204+
}
144205
# If there is neither a `@type` or a `@graph`
145206
else {
146207
# just output the object.

0 commit comments

Comments
 (0)