From b5888ebf4c3ac41c364ffdf4fc39a8f07c63db8c Mon Sep 17 00:00:00 2001 From: Sanjay Bhangar Date: Sat, 17 Mar 2018 18:35:03 +0530 Subject: [PATCH] first commit - WIP: lib functions + fixtures + tests --- .gitignore | 1 + index.js | 0 lib/json-to-html.js | 89 +++++ lib/yaml-to-json.js | 7 + package.json | 21 ++ test/fixtures/expected-html.html | 539 +++++++++++++++++++++++++++++++ test/fixtures/expected-json.json | 244 ++++++++++++++ test/fixtures/playlist.yml | 179 ++++++++++ test/index.js | 29 ++ 9 files changed, 1109 insertions(+) create mode 100644 .gitignore create mode 100644 index.js create mode 100644 lib/json-to-html.js create mode 100644 lib/yaml-to-json.js create mode 100644 package.json create mode 100644 test/fixtures/expected-html.html create mode 100644 test/fixtures/expected-json.json create mode 100644 test/fixtures/playlist.yml create mode 100644 test/index.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/index.js b/index.js new file mode 100644 index 0000000..e69de29 diff --git a/lib/json-to-html.js b/lib/json-to-html.js new file mode 100644 index 0000000..220b89c --- /dev/null +++ b/lib/json-to-html.js @@ -0,0 +1,89 @@ + +module.exports = function(json) { + const html = json.map(getHTML).join('\n\n') + return html +} + +function getHTML(slideData) { + let video = audio = '' + if (!slideData.title) slideData.title = '' + title = ` +
+ ${slideData.title} +
+ ` + if (slideData.video) { + video = getVideoHtml(slideData.video) + } + + if (slideData.audio) { + audio = getAudioHtml(slideData.audio) + } + + const duration = `data-duration="${slideData.duration ? slideData.duration : 5}"` + return ` +
+ ${title} + ${video} + ${audio} +
+ ` +} + +function getVideoHtml(videoData) { + let urls = [] + let volume = '' + if (typeof(videoData) === 'string') { + urls.push(videoData) + } else { // video is an object + if (videoData.hasOwnProperty('url')) { + urls.push(videoData.url) + } else if (videoData.hasOwnProperty('zoom')) { + urls = videoData.zoom + } + if (videoData.volume) { + volume = `data-volume="${videoData.volume}"` + } + } + const urlsHtml = urls.map((url, index) => { + return `data-url_${index}="${makeEmbed(url)}"` + }).join('\n') + return ` +
+ ` +} + +function getAudioHtml(audioData) { + let url = continueStr = volume = '' + + // short circuit to return an empty div if audioData is null or not an object + // this is useful for user to specify empty audio track to cause existing + // audio to pause + if (!audioData || !(typeof(audioData === 'object'))) { + return `
+ ` +} + +function makeEmbed(url) { + if (!url.endsWith('#embed')) { + return `${url}#embed` + } else { + return url + } +} \ No newline at end of file diff --git a/lib/yaml-to-json.js b/lib/yaml-to-json.js new file mode 100644 index 0000000..1f2ebd5 --- /dev/null +++ b/lib/yaml-to-json.js @@ -0,0 +1,7 @@ +const YAML = require('yamljs') + +module.exports = function(yamlTxt) { + const arr = yamlTxt.split('\n\n') + const data = arr.map(o => YAML.parse(o)) + return data +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..5521643 --- /dev/null +++ b/package.json @@ -0,0 +1,21 @@ +{ + "name": "player-yaml-html", + "version": "1.0.0", + "description": "Convert playlist YAML-ish documents to JSON -> HTML", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "https://code.with.camp/sanj/player-yaml-html" + }, + "author": "CAMP", + "license": "ISC", + "dependencies": { + "yamljs": "^0.3.0" + }, + "devDependencies": { + "tape": "^4.9.0" + } +} diff --git a/test/fixtures/expected-html.html b/test/fixtures/expected-html.html new file mode 100644 index 0000000..b5d77c8 --- /dev/null +++ b/test/fixtures/expected-html.html @@ -0,0 +1,539 @@ +
+ +
+ FROM JANTA COLONY TO JANTA COLONY +
+ + + +
+ + + +
+ +
+ At first, three stories from the year 1950 in Bombay +
+ + + +
+ + + +
+ +
+ +
+ + + +
+ + + +
+ +
+ ONE +
+ + + +
+ + + +
+ +
+ +
+ + +
+ + +
+ + + +
+ +
+ The Greater Bombay Tenant's Association is formed in April 1950 +
+ + +
+ + +
+ + + +
+ +
+ It demands the "rationing" of living space in Bombay +
+ + +
+ + +
+ +
+ + + +
+ +
+ One week later, 250 people removed from Mahim causeway +
+ + +
+ + +
+ + + +
+ +
+ occupy Samudra Mahal in Worli +
+ + +
+ + +
+ + + +
+ +
+ +
+ + +
+ + +
+ + + +
+ +
+ It is reoccupied one day later. +
+ + +
+ + +
+ + + +
+ +
+ 5 days after that, a govt. conference on the "squatter issue" was held. +
+ + +
+ + +
+ + + +
+ +
+ It refers to the 1949 survey of homeless done by TISS as its main source. +
+ + + +
+ + + +
+ +
+ Three months later, a proposal is made to create a "Poor Man's Colony" in Mankhurd. +
+ + +
+ + +
+ + + +
+ +
+ For 4,000 families, "Between Sion and Trombay" +
+ + +
+ + +
+ + + +
+ +
+ In an area known as Manbudruk, in Farsi, big brother of Mankhurd. +
+ + +
+ + +
+ + + +
+ +
+ In another year the squatter population has doubled to 75,000 +
+ + +
+ + +
+ + + +
+ +
+ On December there are evictions of 1,700 huts at Sion Circle, out of which 500 moved to Mankhurd. +
+ + +
+ + +
+ + + +
+ +
+ The following year there are many people who refuse to be transported to Mankhurd. +
+ + +
+ + +
+ + + +
+ +
+ On a national level, in 1954 Nehru had decided he was against paying compensation in such cases. +
+ + +
+ + +
+ + + +
+ +
+ A better idea, he is quoted in TOI, is "to burn them". +
+ + +
+ + +
+ + + +
+ +
+ +
+ + + +
+ + + +
+ +
+ Meanwhile in April 1950, the limits of Bombay city have been officially extended. +
+ + +
+ + +
+ + + +
+ +
+ +
+ + +
+ + +
+ + + +
+ +
+ +
+ + + +
+ + + +
+ +
+ OK, Lets watch a three minute version of Raj Kapoor's Sree 420 +
+ + +
+ + +
+ + + +
+ +
+ +
+ + +
+ + +
+ + + +
+ +
+ Abbas +
+ + +
+ + +
+ + + +
+ +
+ In Mani Kaul Satah se Uthata +
+ + +
+ + +
+ + + +
+ +
+ In Guru Dutt's first film. Mr and Mrs 55 +
+ + +
+ + +
+ + + +
+ +
+ This is Shastri Nagar, Bandra, where 16 years later Anand Patwardhan would shoot Hamara Shehar. +
+ + +
+ + +
+ + + +
+ +
+ +
+ + +
+ + +
+ + + +
+ +
+ Cut to: Vidhu Vinod Chopra's first film, on Mankhurd Children's Home. +
+ + +
+ + +
+ + + +
+ +
+ A Deonar Farm Road rumour: One Evening in the mid-fifties, Homi Bhabha, Raj Kapoor and Jamshedji Tata were having tea. With Nehru. +
+ + + +
+ +
+ + + +
+ +
+ All three shared their cultural, scientific and industrial vision with Nehru, who gave them in turn land for RK Studios, BARC and TISS. +
+ + + +
+ + + +
+ +
+ +
+ + +
+ + +
+ + + +
+ +
+ +
+ + +
+ + +
+ + + +
+ +
+ +
+ + +
+ + +
+ + + +
+ +
+ +
+ + +
+ + +
diff --git a/test/fixtures/expected-json.json b/test/fixtures/expected-json.json new file mode 100644 index 0000000..c901831 --- /dev/null +++ b/test/fixtures/expected-json.json @@ -0,0 +1,244 @@ +[ + { + "title": "FROM JANTA COLONY TO JANTA COLONY", + "duration": 5 + }, + { + "title": "At first, three stories from the year 1950 in Bombay", + "duration": 5 + }, + { + "title": null, + "duration": 2 + }, + { + "title": "ONE", + "duration": 5 + }, + { + "video": "https://pad.ma/documents/AFL/0,0,6580,3763", + "duration": 3 + }, + { + "video": { + "zoom": [ + "https://pad.ma/documents/AFL/0,0,6580,3763", + "https://pad.ma/documents/AFL/1933,24,3578,977" + ] + }, + "title": "The Greater Bombay Tenant's Association is formed in April 1950", + "duration": 8 + }, + { + "video": { + "zoom": [ + "https://pad.ma/documents/AFL/1933,24,3578,977", + "https://pad.ma/documents/AFL/1568,1461,3352,2401" + ] + }, + "title": "It demands the \"rationing\" of living space in Bombay", + "audio": { + "url": null, + "volume": 0.05, + "continue": true + }, + "duration": 15 + }, + { + "video": { + "zoom": [ + "https://pad.ma/documents/ADZ/0,0,6554,5409", + "https://pad.ma/documents/ADZ/3836,2088,6401,3440" + ] + }, + "title": "One week later, 250 people removed from Mahim causeway", + "duration": 15 + }, + { + "video": "https://pad.ma/documents/AEA", + "title": "occupy Samudra Mahal in Worli", + "duration": 4 + }, + { + "video": "https://pad.ma/documents/AEB", + "duration": 5 + }, + { + "video": { + "zoom": [ + "https://pad.ma/documents/AED/381,0,6178,3090", + "https://pad.ma/documents/AED/1088,4231,4018,5776" + ] + }, + "title": "It is reoccupied one day later.", + "duration": 10 + }, + { + "video": { + "zoom": [ + "https://pad.ma/documents/AEE/278,3664,3352,5477", + "https://pad.ma/documents/AEE/778,5104,2231,6034" + ] + }, + "title": "5 days after that, a govt. conference on the \"squatter issue\" was held.", + "duration": 8 + }, + { + "title": "It refers to the 1949 survey of homeless done by TISS as its main source.", + "duration": 5 + }, + { + "video": { + "zoom": [ + "https://pad.ma/documents/AEK/0,0,6671,6170", + "https://pad.ma/documents/AEK/0,2620,2925,4162" + ] + }, + "title": "Three months later, a proposal is made to create a \"Poor Man's Colony\" in Mankhurd.", + "duration": 10 + }, + { + "video": "https://pad.ma/documents/AEK/0,2620,2925,4162", + "title": "For 4,000 families, \"Between Sion and Trombay\"", + "duration": 5 + }, + { + "video": "https://pad.ma/documents/AEP/0,0,1538,1378", + "title": "In an area known as Manbudruk, in Farsi, big brother of Mankhurd.", + "duration": 5 + }, + { + "video": "https://pad.ma/documents/AEZ/916,3015,6633,6030", + "title": "In another year the squatter population has doubled to 75,000", + "duration": 5 + }, + { + "video": "https://pad.ma/documents/AEZ/916,3015,6633,6030", + "title": "On December there are evictions of 1,700 huts at Sion Circle, out of which 500 moved to Mankhurd.", + "duration": 8 + }, + { + "video": "https://pad.ma/documents/AEW/0,0,4024,3608", + "title": "The following year there are many people who refuse to be transported to Mankhurd." + }, + { + "video": "https://pad.ma/documents/AFA/0,0,1830,1302", + "title": "On a national level, in 1954 Nehru had decided he was against paying compensation in such cases.", + "duration": 5 + }, + { + "video": { + "zoom": [ + "https://pad.ma/documents/AFB/0,0,1856,1306", + "https://pad.ma/documents/AFB/706,653,1834,1306" + ] + }, + "title": "A better idea, he is quoted in TOI, is \"to burn them\".", + "duration": 9 + }, + { + "title": null, + "duration": 3 + }, + { + "video": "https://pad.ma/documents/AHX", + "title": "Meanwhile in April 1950, the limits of Bombay city have been officially extended.", + "duration": 6 + }, + { + "video": { + "zoom": [ + "https://pad.ma/documents/AEL/853,169,5855,2620", + "https://pad.ma/documents/AEL/830,2505,5832,4956" + ] + }, + "title": null, + "duration": 10 + }, + { + "title": null, + "duration": 3 + }, + { + "video": { + "url": "https://indiancine.ma/AKDP/player/BFS", + "volume": 0.6 + }, + "title": "OK, Lets watch a three minute version of Raj Kapoor's Sree 420" + }, + { + "video": { + "url": "https://indiancine.ma/AKDP/player/BFR", + "volume": 0.6 + }, + "title": null + }, + { + "video": "https://pad.ma/documents/AFC/0,926,1866,2154", + "title": "Abbas", + "duration": 10 + }, + { + "video": "https://indiancine.ma/VEW/player/CLW", + "title": "In Mani Kaul Satah se Uthata" + }, + { + "video": "https://indiancine.ma/AKCY/player/EZ", + "title": "In Guru Dutt's first film. Mr and Mrs 55" + }, + { + "video": "https://indiancine.ma/AKBH/player/DV", + "title": "This is Shastri Nagar, Bandra, where 16 years later Anand Patwardhan would shoot Hamara Shehar." + }, + { + "video": "https://indiancine.ma/AKBH/player/DX", + "title": null + }, + { + "video": "https://indiancine.ma/AMGS/player/IV", + "title": "Cut to: Vidhu Vinod Chopra's first film, on Mankhurd Children's Home." + }, + { + "title": "A Deonar Farm Road rumour: One Evening in the mid-fifties, Homi Bhabha, Raj Kapoor and Jamshedji Tata were having tea. With Nehru.", + "duration": 6, + "audio": { + "url": "https://indiancine.ma/AKBH/player/EC", + "volume": 0.5, + "continue": true + } + }, + { + "title": "All three shared their cultural, scientific and industrial vision with Nehru, who gave them in turn land for RK Studios, BARC and TISS.", + "duration": 10 + }, + { + "video": "https://pad.ma/documents/AIA/0,56,2448,1688", + "title": null, + "duration": 5 + }, + { + "video": { + "zoom": [ + "https://pad.ma/documents/AEG/0,1327,3493,3311", + "https://pad.ma/documents/AEG/202,0,3752,1983" + ] + }, + "title": null, + "duration": 10 + }, + { + "video": { + "zoom": [ + "https://pad.ma/documents/AEI/851,0,5596,2652", + "https://pad.ma/documents/AEI/0,0,6096,5303" + ] + }, + "title": null, + "duration": 8 + }, + { + "video": "https://pad.ma/documents/AEJ/0,0,5691,5296", + "title": null, + "duration": 7 + } +] diff --git a/test/fixtures/playlist.yml b/test/fixtures/playlist.yml new file mode 100644 index 0000000..5e68a7a --- /dev/null +++ b/test/fixtures/playlist.yml @@ -0,0 +1,179 @@ +title: FROM JANTA COLONY TO JANTA COLONY +duration: 5 + +title: At first, three stories from the year 1950 in Bombay +duration: 5 + +title: +duration: 2 + +title: ONE +duration: 5 + +video: https://pad.ma/documents/AFL/0,0,6580,3763 +duration: 3 + +video: + zoom: + - https://pad.ma/documents/AFL/0,0,6580,3763 + - https://pad.ma/documents/AFL/1933,24,3578,977 + +title: The Greater Bombay Tenant's Association is formed in April 1950 +duration: 8 + +video: + zoom: + - https://pad.ma/documents/AFL/1933,24,3578,977 + - https://pad.ma/documents/AFL/1568,1461,3352,2401 +title: It demands the "rationing" of living space in Bombay +audio: + url: + volume: 0.05 + continue: true +duration: 15 + +video: + zoom: + - https://pad.ma/documents/ADZ/0,0,6554,5409 + - https://pad.ma/documents/ADZ/3836,2088,6401,3440 +title: One week later, 250 people removed from Mahim causeway +duration: 15 + +video: https://pad.ma/documents/AEA +title: occupy Samudra Mahal in Worli +duration: 4 + +video: https://pad.ma/documents/AEB +duration: 5 + +video: + zoom: + - https://pad.ma/documents/AED/381,0,6178,3090 + - https://pad.ma/documents/AED/1088,4231,4018,5776 +title: It is reoccupied one day later. +duration: 10 + +video: + zoom: + - https://pad.ma/documents/AEE/278,3664,3352,5477 + - https://pad.ma/documents/AEE/778,5104,2231,6034 +title: 5 days after that, a govt. conference on the "squatter issue" was held. +duration: 8 + +title: It refers to the 1949 survey of homeless done by TISS as its main source. +duration: 5 + +video: + zoom: + - https://pad.ma/documents/AEK/0,0,6671,6170 + - https://pad.ma/documents/AEK/0,2620,2925,4162 +title: Three months later, a proposal is made to create a "Poor Man's Colony" in Mankhurd. +duration: 10 + +video: https://pad.ma/documents/AEK/0,2620,2925,4162 +title: For 4,000 families, "Between Sion and Trombay" +duration: 5 + +video: https://pad.ma/documents/AEP/0,0,1538,1378 +title: In an area known as Manbudruk, in Farsi, big brother of Mankhurd. +duration: 5 + +video: https://pad.ma/documents/AEZ/916,3015,6633,6030 +title: In another year the squatter population has doubled to 75,000 +duration: 5 + +video: https://pad.ma/documents/AEZ/916,3015,6633,6030 +title: On December there are evictions of 1,700 huts at Sion Circle, out of which 500 moved to Mankhurd. +duration: 8 + +video: https://pad.ma/documents/AEW/0,0,4024,3608 +title: The following year there are many people who refuse to be transported to Mankhurd. + +video: https://pad.ma/documents/AFA/0,0,1830,1302 +title: On a national level, in 1954 Nehru had decided he was against paying compensation in such cases. +duration: 5 + +video: + zoom: + - https://pad.ma/documents/AFB/0,0,1856,1306 + - https://pad.ma/documents/AFB/706,653,1834,1306 +title: A better idea, he is quoted in TOI, is "to burn them". +duration: 9 + +title: +duration: 3 + +video: https://pad.ma/documents/AHX +title: Meanwhile in April 1950, the limits of Bombay city have been officially extended. +duration: 6 + +video: + zoom: + - https://pad.ma/documents/AEL/853,169,5855,2620 + - https://pad.ma/documents/AEL/830,2505,5832,4956 +title: +duration: 10 + +title: +duration: 3 + +video: + url: https://indiancine.ma/AKDP/player/BFS + volume: 0.6 +title: OK, Lets watch a three minute version of Raj Kapoor's Sree 420 + +video: + url: https://indiancine.ma/AKDP/player/BFR + volume: 0.6 +title: + +video: https://pad.ma/documents/AFC/0,926,1866,2154 +title: Abbas +duration: 10 + +video: https://indiancine.ma/VEW/player/CLW +title: In Mani Kaul Satah se Uthata + +video: https://indiancine.ma/AKCY/player/EZ +title: In Guru Dutt's first film. Mr and Mrs 55 + +video: https://indiancine.ma/AKBH/player/DV +title: This is Shastri Nagar, Bandra, where 16 years later Anand Patwardhan would shoot Hamara Shehar. + +video: https://indiancine.ma/AKBH/player/DX +title: + +video: https://indiancine.ma/AMGS/player/IV +title: Cut to: Vidhu Vinod Chopra's first film, on Mankhurd Children's Home. + +title: A Deonar Farm Road rumour: One Evening in the mid-fifties, Homi Bhabha, Raj Kapoor and Jamshedji Tata were having tea. With Nehru. +duration: 6 +audio: + url: https://indiancine.ma/AKBH/player/EC + volume: 0.5 + continue: true + +title: All three shared their cultural, scientific and industrial vision with Nehru, who gave them in turn land for RK Studios, BARC and TISS. +duration: 10 + +video: https://pad.ma/documents/AIA/0,56,2448,1688 +title: +duration: 5 + +video: + zoom: + - https://pad.ma/documents/AEG/0,1327,3493,3311 + - https://pad.ma/documents/AEG/202,0,3752,1983 +title: +duration: 10 + +video: + zoom: + - https://pad.ma/documents/AEI/851,0,5596,2652 + - https://pad.ma/documents/AEI/0,0,6096,5303 +title: +duration: 8 + +video: https://pad.ma/documents/AEJ/0,0,5691,5296 +title: +duration: 7 diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..d6b1c61 --- /dev/null +++ b/test/index.js @@ -0,0 +1,29 @@ +const tape = require('tape') +const path = require('path') +const fs = require('fs') +const yamlToJson = require('../lib/yaml-to-json') +const jsonToHtml = require('../lib/json-to-html') + + +tape('test yaml to json', assert => { + const txt = fs.readFileSync(path.join(__dirname, 'fixtures', 'playlist.yml'), 'utf8') + const jsonData = yamlToJson(txt) + const expectedJson = JSON.parse(fs.readFileSync(path.join(__dirname, 'fixtures', 'expected-json.json'), 'utf8')) + assert.deepEqual(jsonData, expectedJson, 'json parsed as expected') + // console.log(JSON.stringify(jsonData, null, 2)) + assert.end() +}) + +tape('test json to html', assert => { + const jsonData = JSON.parse(fs.readFileSync(path.join(__dirname, 'fixtures', 'expected-json.json'), 'utf8')) + const html = jsonToHtml(jsonData) + const expectedHtml = fs.readFileSync(path.join(__dirname, 'fixtures', 'expected-html.html'), 'utf8') + assert.equal(html.trim(), expectedHtml.trim(), 'html as expected') + assert.end() +}) + +// const filename = process.argv[1] + +// const yamlTxt = fs.readFileSync(filename, 'utf-8') + +// console.log(jsonToHtml(yamlToJson(yamlTxt))) \ No newline at end of file