-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Mtillmann/psc-etc
Podlove simple Chapters
- Loading branch information
Showing
8 changed files
with
254 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { FormatBase } from "./FormatBase.js"; | ||
import jsdom from "jsdom"; | ||
import { NPTToSeconds, secondsToNPT } from "../util.js"; | ||
|
||
export class PodloveSimpleChapters extends FormatBase { | ||
|
||
supportsPrettyPrint = true; | ||
filename = 'podlove-simple-chapters-fragment.xml'; | ||
mimeType = 'text/xml'; | ||
|
||
detect(inputString) { | ||
|
||
return /<psc:chapters/.test(inputString); | ||
} | ||
|
||
parse(string) { | ||
if (!this.detect(string)) { | ||
throw new Error('Input must contain <psc:chapters ...> node'); | ||
} | ||
|
||
let dom; | ||
if (typeof DOMParser !== 'undefined') { | ||
dom = (new DOMParser()).parseFromString(string, 'application/xml'); | ||
} else { | ||
const { JSDOM } = jsdom; | ||
dom = new JSDOM(string, { contentType: 'application/xml' }); | ||
dom = dom.window.document; | ||
} | ||
|
||
|
||
this.chapters = [...dom.querySelectorAll('[start]')].reduce((acc, node) => { | ||
|
||
if (node.tagName === 'psc:chapter') { | ||
const start = node.getAttribute('start'); | ||
const title = node.getAttribute('title'); | ||
const image = node.getAttribute('image'); | ||
const href = node.getAttribute('href'); | ||
|
||
const chapter = { | ||
startTime: NPTToSeconds(start) | ||
} | ||
|
||
if (title) { | ||
chapter.title = title; | ||
} | ||
if (image) { | ||
chapter.img = image; | ||
} | ||
if (href) { | ||
//is this ever used, except for this format? | ||
chapter.href = href; | ||
} | ||
|
||
acc.push(chapter); | ||
} | ||
return acc; | ||
|
||
}, []); | ||
|
||
} | ||
|
||
toString(pretty = false) { | ||
const indent = (depth, string, spacesPerDepth = 2) => (pretty ? ' '.repeat(depth * spacesPerDepth) : '') + string; | ||
|
||
let output = [ | ||
'<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">', | ||
indent(1, '<channel>'), | ||
indent(2, '<!-- this is only a fragment of an rss feed, see -->'), | ||
indent(2, '<!-- https://podlove.org/simple-chapters/#:~:text=37%20seconds-,Embedding%20Example,-This%20is%20an -->'), | ||
indent(2, '<!-- for more information -->'), | ||
indent(2, '<psc:chapters version="1.2" xmlns:psc="http://podlove.org/simple-chapters">'), | ||
]; | ||
|
||
this.chapters.forEach(chapter => { | ||
|
||
const node = [ | ||
`<psc:chapter start="${secondsToNPT(chapter.startTime)}"`, | ||
]; | ||
|
||
if (chapter.title) { | ||
node.push(` title="${chapter.title}"`); | ||
} | ||
if (chapter.img) { | ||
node.push(` image="${chapter.img}"`); | ||
} | ||
if (chapter.href) { | ||
node.push(` href="${chapter.href}"`); | ||
} | ||
node.push('/>'); | ||
|
||
output.push(indent(3, node.join(''))); | ||
|
||
}); | ||
|
||
output.push( | ||
indent(2, '</psc:chapters>'), | ||
indent(1, '</channel>'), | ||
indent(0, '</rss>') | ||
); | ||
|
||
return output.join(pretty ? "\n" : ''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
import { readFileSync } from "fs"; | ||
import { sep } from "path"; | ||
import { FFMetadata } from "../src/Formats/FFMetadata.js"; | ||
import { PodloveSimpleChapters } from "../src/Formats/PodloveSimpleChapters.js"; | ||
|
||
|
||
describe('PodloveSimpleChapters Format Handler', () => { | ||
it('accepts no arguments', () => { | ||
expect(() => { | ||
new PodloveSimpleChapters(); | ||
}).not.toThrowError(TypeError); | ||
}); | ||
|
||
|
||
it('fails on malformed input', () => { | ||
expect(() => { | ||
new PodloveSimpleChapters('asdf'); | ||
}).toThrowError(Error); | ||
}); | ||
|
||
const content = readFileSync(module.path + sep + 'samples' + sep + 'podlove-simple-chapters.xml', 'utf-8'); | ||
|
||
it('parses well-formed input', () => { | ||
expect(() => { | ||
new PodloveSimpleChapters(content); | ||
}).not.toThrow(Error); | ||
}); | ||
|
||
const instance = new PodloveSimpleChapters(content); | ||
|
||
it('has the correct number of chapters from content', () => { | ||
expect(instance.chapters.length).toEqual(4); | ||
}); | ||
|
||
it('has parsed the timestamps correctly', () => { | ||
expect(instance.chapters[1].startTime).toBe(187) | ||
}); | ||
|
||
it('has parsed the chapter titles correctly', () => { | ||
expect(instance.chapters[0].title).toBe('Welcome') | ||
}); | ||
|
||
it('exports to correct format', () => { | ||
expect(instance.toString()).toContain('psc:chapters'); | ||
}); | ||
|
||
it('export includes correct timestamp', () => { | ||
expect(instance.toString()).toContain('3:07'); | ||
}); | ||
|
||
it('can import previously generated export', () => { | ||
expect(new PodloveSimpleChapters(instance.toString()).chapters[1].startTime).toEqual(187); | ||
}); | ||
|
||
it('can convert into other format', () => { | ||
expect(instance.to(FFMetadata)).toBeInstanceOf(FFMetadata) | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> | ||
<channel> | ||
<title>Podlove Podcast</title> | ||
<atom:link type="text/html" href="http://podlove.org/" /> | ||
<item> | ||
<title>Fiat Lux</title> | ||
<link href="http://podlove.org/podcast/1"/> | ||
<guid isPermaLink="false">urn:uuid:3241ace2-ca21-dd12-2341-1412ce31fad2</guid> | ||
<pubDate>Fri, 23 Mar 2012 23:25:19 +0000</pubDate> | ||
<description>First episode</description> | ||
<link rel="enclosure" type="audio/mpeg" length="12345" href="http://podlove.org/files/fiatlux.mp3"/> | ||
<!-- specify chapter information --> | ||
<psc:chapters version="1.2" xmlns:psc="http://podlove.org/simple-chapters"> | ||
<psc:chapter start="0" title="Welcome" /> | ||
<psc:chapter start="3:07" title="Introducing Podlove" href="http://podlove.org/" /> | ||
<psc:chapter start="8:26.250" title="Podlove WordPress Plugin" href="http://podlove.org/podlove-podcast-publisher" /> | ||
<psc:chapter start="12:42" title="Resumée" /> | ||
</psc:chapters> | ||
</item> | ||
</channel> | ||
</rss> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> | ||
<channel> | ||
<!-- this is only a fragment of an rss feed, see --> | ||
<!-- https://podlove.org/simple-chapters/#:~:text=37%20seconds-,Embedding%20Example,-This%20is%20an --> | ||
<!-- for more information --> | ||
<psc:chapters version="1.2" xmlns:psc="http://podlove.org/simple-chapters"> | ||
<psc:chapter start="0" title="Welcome"/> | ||
<psc:chapter start="3:07" title="Introducing Podlove" href="http://podlove.org/"/> | ||
<psc:chapter start="8:26.250" title="Podlove WordPress Plugin" href="http://podlove.org/podlove-podcast-publisher"/> | ||
<psc:chapter start="12:42" title="Resumée"/> | ||
</psc:chapters> | ||
</channel> | ||
</rss> |