Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F122422662
myTpPluginOld.js
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Jul 17, 19:04
Size
3 KB
Mime Type
text/x-c++
Expires
Sat, Jul 19, 19:04 (2 d)
Engine
blob
Format
Raw Data
Handle
27482719
Attached To
rTPELEVENTY tei-publisher-eleventy-plugin
myTpPluginOld.js
View Options
const
{
JSDOM
}
=
require
(
"jsdom"
);
const
axios
=
require
(
"axios"
);
const
fs
=
require
(
"fs"
);
const
path
=
require
(
"path"
);
const
mkdirp
=
require
(
"mkdirp"
);
const
chalk
=
require
(
"chalk"
);
const
debug
=
require
(
"debug"
)(
"tei-publisher"
);
const
{
DateTime
}
=
require
(
"luxon"
);
const
{
AssetCache
}
=
require
(
"@11ty/eleventy-fetch"
);
const
{
TpPlugin
,
getAttributes
}
=
require
(
"@teipublisher/pb-eleventy-plugin/plugin"
);
function
log
(
input
,
...
messages
)
{
console
.
log
(
`
$
{
chalk
.
cyan
(
'[nietzsche-dm]'
)}
$
{
input
}
`
,
...
messages
);
}
function
warn
(
input
,
...
messages
)
{
console
.
warn
(
`
$
{
chalk
.
red
(
'[tei-publisher]'
)}
$
{
input
}
`
,
...
messages
);
}
function
computeKey
(
params
)
{
const
encParams
=
[];
Object
.
keys
(
params
).
sort
().
forEach
((
key
)
=>
{
encParams
.
push
(
`
$
{
key
}
=
$
{
params
[
key
]}
`
);
});
//log('Compute key %s with params %s', JSON.stringify(encParams), JSON.stringify(params))
return
encParams
.
join
(
'&'
);
}
function
createOutputDir
(
context
)
{
const
outputDir
=
path
.
dirname
(
context
.
outputPath
);
if
(
!
fs
.
existsSync
(
outputDir
))
{
mkdirp
.
sync
(
outputDir
);
}
return
{...
context
,
...{
outputDir
}};
}
class
MyPlugin
extends
TpPlugin
{
constructor
(
config
)
{
super
(
config
);
}
async
_retrieve
(
name
,
docPath
,
params
,
context
,
mapping
,
root
=
null
,
counter
=
1
,
images
)
{
const
url
=
`
api
/
parts
/
$
{
encodeURIComponent
(
docPath
)}
/
json
`
;
if
(
root
)
{
params
=
{...
params
,
...
root
};
}
if
(
params
[
'user.map'
])
{
params
[
'map'
]
=
params
[
'user.map'
];
}
if
(
params
[
'user.odd'
])
{
if
(
params
[
'odd'
]){
params
[
'user.alt_odd'
]
=
params
[
'odd'
];
}
params
[
'odd'
]
=
params
[
'user.odd'
];
}
const
response
=
await
this
.
client
.
request
({
url
,
method
:
'get'
,
params
})
.
catch
(
function
(
error
)
{
const
err
=
error
.
toJSON
();
debug
(
'Failed to retrieve fragment %s: %s'
,
chalk
.
bgRed
(
err
.
config
.
url
),
err
.
message
);
debug
(
err
);
return
null
;
});
if
(
response
&&
response
.
status
===
200
)
{
const
outName
=
`
$
{
name
}
-
$
{
counter
}.
json
`
;
const
outFile
=
path
.
resolve
(
context
.
outputDir
,
outName
);
if
(
!
response
.
data
.
content
)
{
debug
(
'No content received for %s'
,
chalk
.
bgRed
(
url
));
return
null
;
}
const
dom
=
new
JSDOM
(
response
.
data
.
content
);
const
{
transformed
,
ids
}
=
await
this
.
_expandPageContent
(
dom
,
images
,
`
$
{
this
.
config
.
remote
}
/
$
{
docPath
}
`
);
response
.
data
.
content
=
transformed
;
fs
.
writeFileSync
(
outFile
,
JSON
.
stringify
(
response
.
data
,
null
,
4
));
mapping
[
computeKey
(
params
)]
=
outName
;
if
(
response
.
data
.
id
)
{
ids
.
push
(
response
.
data
.
id
);
}
this
.
_addById
(
mapping
,
outName
,
params
,
ids
);
//log('url %s with params %s ', chalk.blue(url), chalk.magenta(JSON.stringify(params)), chalk.green(JSON.stringify(ids)))
if
(
response
.
data
.
next
)
{
return
{
root
:
response
.
data
.
next
};
}
}
return
null
;
}
_addById
(
mapping
,
outName
,
params
,
ids
)
{
const
paramsNoRoot
=
{...
params
};
delete
paramsNoRoot
.
root
;
ids
.
forEach
(
id
=>
{
const
paramsWithId
=
{...
paramsNoRoot
,
...{
id
}}
mapping
[
computeKey
(
paramsWithId
)]
=
outName
;
});
}
}
module
.
exports
=
{
MyPlugin
};
Event Timeline
Log In to Comment