is there any way to configure DocPad to generate pages starting from a json array saved in external file (or inline string) instead of from a collection of files?
To clarify, for show posts details I fetch from a JSON file, see below.
instead of this:
<% for post in #getCollection("html").findAll({ relativeOutDirPath: 'posts' }).toJSON(): %>
I use this:
<% for post in JSON.parse #include("posts.json"): %>
Ok. Now I would to generate the post pages directly from this JSON and not creating a page for each post like in the example..
For example I would to create page with url /posts/{urlname}.html when {urlname} exist in JSON like this:
[
{ "id": "1", "urlname": "prod1", "metadata": { "title": "val1" } },
{ "id": "2", "metadata": null },
{ "id": "3", "urlname": "prod3", "metadata": { "title": "val1b", "prop2": "val2b" } }
]
I would to generate /posts/prod1.html and /posts/prod2.html page with metadata those in metadata properties..
Thank's for the replies.. ;)
PS Great work!!!!!!!
Currently there is no official way to inject data into the DocPad in memory database besides having it parsed from the file system on the src directory (the way we are all use to). HOWEVER, this feature (called importers) is the next big todo for DocPad, you can find the task issue here.
For the meantime, you could include the JSON inside your template data, which is suitable for content listings, but not suitable for providing individual documents for each entry.
Related
I'm working on my first python discord bot and it's turning out to be pretty decent but I wanted to use embeds for certain responses. One of them includes sending all the features of the bots which uses many embeds. I don't have any experience about Javascript or JSON so I used a website "https://discohook.org/" to create an embed as it's GUI based. But as a result, I only get the JSON file and I couldn't find a way where I could load a JSON file and send it as an embed.
The JSON file looks something like this -
{
"content": "Bot Name",
"embeds": [
{
"description": "This command lists all the possible commands which can be used to interact with the bot.",
"author": {
"name": "Help",
"icon_url": "http://4.bp.blogspot.com/-wuUbc-OPOt4/T3ioPh2pAAI/AAAAAAAAAdM/BFFvS5fxVMY/w1200-h630-p-k-no-nu/Questionmark.jpg"
}
},
{
"description": "Bot blesses you. ",
"author": {
"name": "Bless ",
"icon_url": "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fwallpapercave.com%2Fwp%2Fwp4775695.jpg&f=1&nofb=1"
}
},
{
"description": "Set status as idle",
"color": 16777215,
"author": {
"name": "Go Sleep",
"icon_url": "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fmedia.forgecdn.net%2Favatars%2F170%2F652%2F636723641550179947.png&f=1&nofb=1"
}
}
]
You could probably just load the individual variables.
with open(“JSON_FILE.txt”) as json_file:
data = json.load(json_file)
data = data[“embeds”]
Then you can have some way to get the specific dictionary.
for embed in data:
pass
Then just have data defined as the dictionary you want to use.
embed = discord.Embed(description = data[“description”])
embed.set_author(name = data[“author”][“name”], icon_url = data[“author”][“icon_url”]
Maybe it've been a long time, but I have exactly the same problem and I found my own solution. Here it is:
from json import loads
from discord import Embed
# Just for our convinience let's make a function
def parse_embed_json(json_file):
embeds_json = loads(json_file)['embeds']
for embed_json in embeds_json:
embed = Embed().from_dict(embed_json)
yield embed
# And the main code looks like this
with open("bot/embeds/temp_ban_embeds.json", "r") as file:
temp_ban_embeds = parse_embed_json(file.read())
for embed in temp_ban_embeds:
await ctx.send(embed=embed)
So I found a special method from_dict. And it seems like it works with dicts we can get from json.loads method. So you can find the documentation here.
I managed to retrieve all the basic info about a medium.com publication via JSON using file_get_contents("https://medium.com/publication_name/latest?format=json"); however is there a way to retrieve the name and photo of the author of the posts?
Given a JSON payload that resembles this:
{
"payload": {
"posts": [
{
"creatorId": "foobar",
"title": "How to foo your bars"
}
],
"references": {
"User": {
"foobar": {
"userId": "foobar",
"name": "Senor Foobar",
"imageId": "baz.jpg"
}
}
}
}
}
The creatorId for a post also exists in the references's User key. In this example I have used the id foobar. From that key you can fetch the author name and filename for the author photo.
Translating the author photo filename to a URL may require more work. The current CDN is https://cdn-images-1.medium.com/fit/c/60/60/ and so the full URL would be https://cdn-images-1.medium.com/fit/c/60/60/foobar.jpg. This CDN is likely to change over time so this URL is not absolute.
The information about the CDN is not available in the JSON payload and was found by inspecting the source for a sample Medium article.
If you use the below link you could get author details. replace your user name instead of <#publication_name>
https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/<#publication_name>
I'm looking for a way to pass a variable (normal string) from a linked template back up to my main template.
I want to use something like: (in linked template)
"outputs": {
"installStringNodes": {
"type": "string",
"value": "[variables('installString').value]"
}
}
And then i want to call this variable into my main template. But i can't seem to crack how.
"variables":{
"installStringFromNodeResources": {
"value": "[??('node-resources')??.outputs.installStringNodes.value]"
},
}
There's a 'sharing state in resource manager templates' doc with the usage of reference() but apparently that can't be used in variables as it gives me an error while trying to deploy.
Seems to me there should be an easy solution for this but i haven't been able to see it yet..
In the main template, the variable should be:
"installStringFromNodeResources": {
"value": "[reference('node-resources').outputs.installStringNodes.value]"
}
Follow this walk through sharing state between templates
Say I have a json structure like so:
{
"A":{
"name":"dog",
"foo":"bar",
"array":[
{"name":"one"},
{"name":"two"}
]
},
"B":{
"name":"cat",
"foo":"bar",
"array":[
{"name":"one"},
{"name":"three"}
]
}
}
I want to be able to do two things.
1: Query for any "name":* within "A.array".
2: Query for any "name":"one" within "*.array".
That is, any object within a specific document's array, and any specific object within any document's array.
I hope I have used proper terminology here, I am just starting to familiarize myself with a lot of these concepts. I have tried searching for an answer but am having trouble finding something like my case.
Thanks.
EDIT:
Since I still haven't really made progress towards this, I'll just explain what I'm trying to do: I want to use the "AllSets" dataset (after I trim it down below 16mb) available on mtgjson.com. I am having problems getting mongo to play nicely though.
In an effort to try and learn what's going on, I have downloaded one set: http://mtgjson.com/json/OGW.json.
Here is a photo of its structure laid out:
I am unable to even get mongo to return an object from within the cards array using:
"find({cards: {$elemMatch: {name:"Deceiver of Form"}}})"
"find({"cards.name":"Deceiver of Form"})"
When I run either of the commands above it just returns the entire document to me.
You could use the positional projection $ operator to limit the contents of an array. For example, if you have a single document like below:
{
"block": "Battle for Zendikar",
"booster": "...",
"translations": "...",
"cards": [
{
"name": "Deceiver of Form",
"power": "8"
},
{
"name": "Eldrazi Mimic",
"power": "2"
},
{
"name": "Kozilek, the Great Distortion",
"power": "12"
}
]
}
You can query for a card name matching "Deceiver of Form", and limit fields to return only the matching array card element(s) using:
> db.collection.find({"cards.name":"Deceiver of Form"}, {"cards.$":1})
{
"_id": ObjectId("..."),
"cards": [
{
"name": "Deceiver of Form",
"power": "8"
}
]
}
Having said the above, I think you should re-consider your data model. MongoDB is a document-oriented database. A record in MongoDB is a document, so having a single record in a database does not bring out the potential of the database i.e. similar to storing all data in a single row in a table.
You should try storing the 'cards' into a collection instead. Where each document is a single card, (depending on your use case) you could add a reference to another collection containing the deck information. i.e: block, type, releaseDate, etc. For example:
// a document in cards collection:
{
"name": "Deceiver of Form",
"power": "8",
"deck_id": 1
}
// a document in decks collection:
{
"deck_id": 1,
"releaseDate": "2016-01-22",
"type": "expansion"
}
For different types of data model designs and examples, please see Data Model Design.
I'm having trouble properly formatting one particular soap parameter using the node-soap module for node.js as a client, to a 3rd-party SOAP service.
The client.describe() for this method says this particular input should be in the shape of:
params: { 'param[]': {} }
I have tried a bunch of different JSON notations to try to fit my data to that shape.
Examples of formats that do NOT work:
"params": { "param": [ {"myParameterName": "myParameterValue"} ] }
"params": [ "param": { "name": "myParameterName", "_": "myParameterValue"} ]
"params": { "param" : [ {"name": "myParameterName", "_": "myParameterValue"} ] }
"params": { "param[]": {"myParameterName": "myParameterValue" } }
"params": { "param[myParameterName]": {"_": "myParameterValue" } }
I must be overlooking something, and I suspect I'm going to feel like Captain Obvious when some nice person points out what I'm doing wrong.
Here is what DOES work, using other soap clients, and how they handle the "named parameter with a value"
soapUI for this method successfully accepts this particular input via XML in the shape of:
<ns:params>
<ns:param name="myParameterName">myParameterValue</ns:param>
</ns:params>
Also, using PHP, I can successfully make the call by creating a stdClass of arrays like so:
$parms = new stdClass;
$parms->param = array(
array(
"name"=>"myParameterName","_"=>"myParameterValue"
)
);
and then eventually passing
'params' => $parms
to the PHP soap client
Many thanks!
To get a better look at what XML was being generated by node-soap, I added a console.log(message) statement to the node_modules/soap/lib/client.js after the object-to-XML encoding. I then began experimenting with various JSON structures to figure out empirically how they were mapping to XML structures.
I found a JSON structure for node-soap to generate the XML in my 3rd-party's required named-parameter-with-value format. I was completely unaware of the "$value" special keyword. Looks like this may have been added in the 0.4.6 release from mid-June 2014. See the change history
"params": [
{
"param": {
"attributes": {
"name": "myParameterName"
},
$value: "myParameterValue"
}
}
]
(note the outer array, which gives me the luxury of specifying multiple "param" entries, which is sometimes needed by this particular 3rd-party API)
generates this XML:
<tns:params>
<tns:param name="myParameterName">myParameterValue</tns:param>
</tns:params>
which perfectly matches the structure in soapUI (which I already knew worked) of:
<ns:params>
<ns:param name="myParameterName">myParameterValue</ns:param>
</ns:params>