Skip to content

Commit

Permalink
Extract from @graph in json-ld
Browse files Browse the repository at this point in the history
Fix for Chobbes#58
  • Loading branch information
egh committed May 8, 2021
1 parent 5b461ed commit aea875b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions org-chef-json-ld.el
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
(and (stringp type)
(string= type "Recipe"))))
json)
((and (json-alist-p json)
(assq '@graph json)
(sequencep (cdr (assq '@graph json))))
(cl-some #'org-chef-json-ld-extract-recipe (cdr (assq '@graph json))))
((and (not (json-alist-p json))
(sequencep json))
(cl-some #'org-chef-json-ld-extract-recipe json))
Expand Down
37 changes: 37 additions & 0 deletions tests/json-ld-sample-graph.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"@context": "https://schema.org",
"@graph": [
{
"@context": "https://schema.org",
"@type": "Recipe",
"author": "Jake Smith",
"cookTime": "PT2H",
"datePublished": "2015-05-18",
"description": "Your recipe description goes here",
"image": "http://www.example.com/images.jpg",
"recipeIngredient": [
"ingredient 1",
"ingredient 2",
"ingredient 3",
"ingredient 4",
"ingredient 5"
],
"interactionStatistic": {
"@type": "InteractionCounter",
"interactionType": "http://schema.org/Comment",
"userInteractionCount": "5"
},
"name": "Rand's Cookies",
"nutrition": {
"@type": "NutritionInformation",
"calories": "1200 calories",
"carbohydrateContent": "12 carbs",
"proteinContent": "9 grams of protein",
"fatContent": "9 grams fat"
},
"prepTime": "PT15M",
"recipeInstructions": "This is the long part, etc.",
"recipeYield": "12 cookies"
}
]
}
8 changes: 8 additions & 0 deletions tests/org-chef-json-ld-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
(should (eq 5 (length ingredients)))
(should (string= "ingredient 1" (elt ingredients 0)))))

(ert-deftest org-chef-json-ld-extract-recipe-graph ()
(let* ((json (json-read-file "json-ld-sample-graph.json"))
(recipe (org-chef-json-ld-extract-recipe json))
(ingredients (cdr (assq 'recipeIngredient recipe))))
(should (string= "PT2H" (cdr (assq 'cookTime recipe))))
(should (eq 5 (length ingredients)))
(should (string= "ingredient 1" (elt ingredients 0)))))

(ert-deftest org-chef-json-ld-parse-duration ()
(should (string= "1 hour" (org-chef-json-ld-parse-duration "PT1H")))
(should (string= "1 hour" (org-chef-json-ld-parse-duration "PT01H")))
Expand Down

0 comments on commit aea875b

Please sign in to comment.