UNPKG

lib-songbeamer

Version:

transforming SongBeamer Song-Files and Ablaufplan-Files

151 lines (132 loc) 4.39 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: libs/song.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: libs/song.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>/** * @overview Song-File Lib * @module song * @author Dominik Sigmund * @version 1.0 * @description Gets Json from and json to song-files * @memberof lib-songbeamer */ /** Get Json from a string * @param {string} data - String as delivered by fs.readFile * @param {song~jsonCallback} callback - A Callback with an error or the Json * @returns Nothing * */ exports.toJSON = function (data, callback) { var lines = data.split('\r') var json = {} json.slides = [] for (let index = 0; index &lt; lines.length; index++) { var line = lines[index].trim() if (line.startsWith('#')) { // Properties var kv = line.split('=') json[kv[0].replace('#', '')] = kv[1] } else { // Slides var slide = {} slide.lines = [] while (line !== '---') { // check if line is a caption if (isCaption(line)) { slide.caption = line } else { slide.lines.push(line) } index++ if (typeof lines[index] !== 'undefined') { line = lines[index].trim() } else { break } } if (slide.lines.length > 0) { json.slides.push(slide) } } } callback(null, json) } /** Write Json to a File String * @param {object} json - String as delivered by fs.readFile * @param {song~stringCallback} callback - A Callback with an error or the String as delivered to fs.writeFile * @returns Nothing * */ exports.toFile = function (json, callback) { var file = '' for (var property in json) { if (property !== 'slides' &amp;&amp; json.hasOwnProperty(property)) { file += '#' + property + '=' + json[property] + '\r' } } for (let index = 0; index &lt; json.slides.length; index++) { file += '---\r' const slide = json.slides[index] if (typeof slide.caption !== 'undefined') { file += slide.caption + '\r' } for (let lineindex = 0; lineindex &lt; slide.lines.length; lineindex++) { const line = slide.lines[lineindex] file += line + '\r' } } callback(null, file) } /** Checks if a string is a caption (Vers, Bridge, Chorus, ...) * @param {string} text - A Line of Text * @returns {bool} - True, if is a caption, else false * */ function isCaption (text) { var captions = ['Vers', 'Verse', 'Bridge', 'Chorus', 'Refrain'] if (new RegExp(captions.join('|')).test(text)) { return true } else { return false } } /** * This callback is displayed as part of the song class. * @callback song~jsonCallback * @param {object} Error or null * @param {object.status} Number of Error (Uses HTTP-Status) * @param {object.message} Custom Error Message * @param {object} Json - The JSON. See Schema */ /** * This callback is displayed as part of the song class. * @callback song~stringCallback * @param {object} Error or null * @param {object.status} Number of Error (Uses HTTP-Status) * @param {object.message} Custom Error Message * @param {object} String - The String, as in the file */ </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-ablauf.html">ablauf</a></li><li><a href="module-index.html">index</a></li><li><a href="module-song.html">song</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Thu Jan 04 2018 16:30:08 GMT+0100 (Mitteleuropäische Zeit) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>