Skip to content

Commit

Permalink
feat(#227): prepare for 2 steps submission
Browse files Browse the repository at this point in the history
  • Loading branch information
NPavie committed Jun 19, 2024
1 parent 8967623 commit a588f77
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 23 deletions.
56 changes: 45 additions & 11 deletions src/renderer/components/ScriptForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,21 @@ export function ScriptForm({ job, script }: { job: Job; script: Script }) {
// - page-width
// - page-height

// Cannot use const isBrailleJob = optional.findIndex((item) => item.name === 'stylesheet')
// as other non braille steps have a stylesheet option
const isBrailleJob = (script && script.id.endsWith('to-pef')) || false
// Cannot use this now as other non braille steps have a stylesheet option
//const is2StepsJob =
// optional.findIndex((item) => item.name === 'stylesheet') > -1
const is2StepsJob = (script && script.id.endsWith('to-pef')) || false
// Filter out the options that are to be defined in the first step of braille script
const filteredOptions = ['stylesheet', 'page-width', 'page-height']
const filteredOptions = [
'stylesheet',
'page-width',
'page-height',
'audio',
'braille',
'tts',
]
const hiddenOptions = ['transform', 'stylesheet-parameters']
if (isBrailleJob) {
if (is2StepsJob) {
optional = optional.filter((item) =>
filteredOptions.includes(item.name)
)
Expand All @@ -85,7 +93,7 @@ export function ScriptForm({ job, script }: { job: Job; script: Script }) {
// When this property is set
// 'optional' is what is displayed on screen as optional values. they could technically be job inputs, options, or stylesheet parameters
// but the user input values aren't stored there, those go in the job request itself
if (isBrailleJob && job.stylesheetParameters != null) {
if (is2StepsJob && job.stylesheetParameters != null) {
required = []
optional = [
...getAllOptional(script)
Expand Down Expand Up @@ -165,11 +173,37 @@ export function ScriptForm({ job, script }: { job: Job; script: Script }) {
// submit a job
let onSubmit = async (e) => {
e.preventDefault()
if (isBrailleJob && job.stylesheetParameters == null) {
App.store.dispatch(requestStylesheetParameters(job))
if (is2StepsJob && job.stylesheetParameters == null) {
/* constraints on the stylesheet parameters :
the /stylesheet-parameters call should not be made if
- TTS is disabled (audio = false) on scripts dtbook-to-daisy3, dtbook-to-epub3 and zedai-to-epub3
- Braille and TTS is disabled (braille = false and audio = false) on epub3-to-epub3
Note : epub-to-daisy also has a "tts" option which might be renamed to "audio".
*/
const hasAudio = job.jobRequest.options.find(
(o) => o.name === 'audio' || o.name === 'tts'
)
const hasBraille = job.jobRequest.options.find(
(o) => o.name === 'braille'
)
if (
(hasAudio &&
(hasAudio.value === true || hasAudio.value !== 'false')) ||
(hasBraille && hasBraille.value === true) ||
job.script.id.endsWith('to-pef')
) {
App.store.dispatch(requestStylesheetParameters(job))
} else {
App.store.dispatch(
updateJob({
...job,
stylesheetParameters: [],
})
)
}
} else {
let options = [...job.jobRequest.options]
if (isBrailleJob) {
if (is2StepsJob) {
// format all the stylesheet parameter options as a string
// and assign it to the 'stylesheet-parameters' option
let stylesheetParametersOption =
Expand Down Expand Up @@ -381,12 +415,12 @@ export function ScriptForm({ job, script }: { job: Job; script: Script }) {
</div>
)}
<div className="form-buttons">
{isBrailleJob && job.stylesheetParameters != null && (
{is2StepsJob && job.stylesheetParameters != null && (
<button className="run" onClick={previous}>
Back
</button>
)}
{isBrailleJob && job.stylesheetParameters == null ? (
{is2StepsJob && job.stylesheetParameters == null ? (
<button className="run" type="submit">
Next
</button>
Expand Down
112 changes: 100 additions & 12 deletions src/shared/parser/pipelineXmlConverter/jobToStylesheetParametersXml.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,94 @@
import { Job } from 'shared/types'

/**
* Media type to be used in the userAgentStylesheet tag
*
* "application/x-dtbook+xml" for dtbook-to-pef, dtbook-to-daisy3 and dtbook-to-epub3
*
* "application/xhtml+xml" for html-to-pef, epub3-to-pef, epub3-to-epub3 and epub-to-daisy
*
* "application/z3998-auth+xml" for zedai-to-epub3
*/
const ScriptMediaType = {
'dtbook-to-pef': 'application/x-dtbook+xml',
'dtbook-to-daisy3': 'application/x-dtbook+xml',
'dtbook-to-epub3': 'application/x-dtbook+xml',
'html-to-pef': 'application/xhtml+xml',
'epub3-to-pef': 'application/xhtml+xml',
'epub3-to-epub3': 'application/xhtml+xml',
'epub-to-daisy': 'application/xhtml+xml',
'zedai-to-epub3': 'application/z3998-auth+xml',
}

/**
* media tag builders to be used in the request with the following per script values :
* - "embossed AND (width:XXX) AND (height:XXX)" for "dtbook-to-pef", "html-to-pef" and "epub3-to-pef"
* - "speech" for "dtbook-to-daisy3", "dtbook-to-epub3", "epub-to-daisy" and "zedai-to-epub3
* - "braille, speech" or "braille" or "speech" for "epub3-to-epub3"
*/
const ScriptMediaTag = {
'dtbook-to-pef': (j: Job) => {
// braille script have width and height options to be used in a media tag
const width = j.jobRequest.options.filter(
(option) => option.name === 'page-width'
)
const height = j.jobRequest.options.filter(
(option) => option.name === 'page-height'
)
return `<media value="embossed${
width[0] !== undefined && ` AND (width:${width[0].value})`
}${height[0] !== undefined && ` AND (height:${height[0].value})`}"/>`
},
'dtbook-to-daisy3': () => `<media value="speech"/>`,
'dtbook-to-epub3': () => `<media value="speech"/>`,
'html-to-pef': (j: Job) => {
// braille script have width and height options to be used in a media tag
const width = j.jobRequest.options.filter(
(option) => option.name === 'page-width'
)
const height = j.jobRequest.options.filter(
(option) => option.name === 'page-height'
)
return `<media value="embossed${
width[0] !== undefined && ` AND (width:${width[0].value})`
}${height[0] !== undefined && ` AND (height:${height[0].value})`}"/>`
},
'epub3-to-pef': (j: Job) => {
// braille script have width and height options to be used in a media tag
const width = j.jobRequest.options.filter(
(option) => option.name === 'page-width'
)
const height = j.jobRequest.options.filter(
(option) => option.name === 'page-height'
)
return `<media value="embossed${
width[0] !== undefined && ` AND (width:${width[0].value})`
}${height[0] !== undefined && ` AND (height:${height[0].value})`}"/>`
},
'epub3-to-epub3': (j: Job) => {
console.log(j.jobRequest.options)
const values = j.jobRequest.options
.filter(
(option) =>
(option.name === 'braille' && option.value === true) ||
(option.name === 'audio' && option.value === true) ||
(option.name === 'tts' && option.value !== 'false')
)
.map((option) => {
if (option.name === 'braille') {
return 'braille'
} else {
return 'speech'
}
})
.join(', ')

return `<media value="${values}"/>`
},
'epub-to-daisy': () => `<media value="speech"/>`,
'zedai-to-epub3': () => `<media value="speech"/>`,
}

/**
* Build a request xml string that can be sent to pipeline on the
* "stylesheet-parameters" using the job inputs and the following
Expand All @@ -12,33 +101,32 @@ import { Job } from 'shared/types'
* new script parameters
*/
function jobToStylesheetParametersXml(j: Job): string {
const scriptRef = j.script.id

const stylesheet = j.jobRequest.options.filter(
(option) => option.name === 'stylesheet'
)[0]
const width = j.jobRequest.options.filter(
(option) => option.name === 'page-width'
)[0]
const height = j.jobRequest.options.filter(
(option) => option.name === 'page-height'
)[0]
// Build request tags
const mediaTag = ScriptMediaTag[scriptRef](j)
let sourceDocument = `<sourceDocument>${j.jobRequest.inputs
.filter(
(input) =>
input.value && input.isFile && !input.name.endsWith('.scss')
)
.map((input) => `<file href="${input.value}"/>`)
.join('')}</sourceDocument>`

// workaround https://github.com/daisy/pipeline-ui/issues/198
if (j.jobRequest.scriptHref.includes('epub3-to-pef')) {
// we cannot send epub files, so source documents are discarded for epub* scripts
if (scriptRef.startsWith('epub')) {
sourceDocument = ''
}

const mimetype = `<userAgentStylesheet mediaType="${ScriptMediaType[scriptRef]}"/>`

return `<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<parameters xmlns="http://www.daisy.org/ns/pipeline/data">
<media value="embossed AND (width:${width.value}) AND (height:${
height.value
})"/>
${mediaTag}
<!-- ${mimetype} -->
<userStylesheets>${
stylesheet && stylesheet.value
? Array.isArray(stylesheet.value)
Expand Down

0 comments on commit a588f77

Please sign in to comment.