How to get Andrej Sládkovič into C++

Zápisník experimentátora

Hierarchy: Node.js

For a long time, I was thinking about transferring Andrej Sládkovič's work (the famous Slovak poet) to a computer. In Banská Bystrica, he has a strange statue in the garden of the Evangelical Church, and he is also buried at the nearby cemetery. Now, during the holidays, I found some free time and I converted his poem Marína into a C++ array. For the time being, I will not disclose for what purpose I needed it. Let's see how this conversion can be done.

Marína

Marína is a poetic poem by our poet about his unhappy love. It has a 291 strophes, and every college student learns excerpts from her at school. Because Sládkovič died in 1872, his work is freely available. I have downloaded the materials from the Gold Fund as HTML.

Conversion into a C++ array

I converted the poem into the C++ array so that each strophe corresponds to one array item. The result is a 110K file. I used Node.js and Cheerio package for the conversion. I wrote three programs.

  • index.js - Extraction of the HTML elements that contain the text of the poem itself.
  • array.js - Conversion of the source into the array and the output of the last strophe.
  • tocpp.js - Conversion into a C++ array.

index.js

Cheerio uses jQuery-like syntax. This selects elements that have a literallayout class set.

var fs = require('fs');
const cheerio = require('cheerio');

var content = fs.readFileSync(__dirname + '/sladkovic-andrej_marina_txt.html');
const $ = cheerio.load(content);

var txt = $('.literallayout').text();
console.log(txt);

array.js

This is how the previous results can be converted to the array. The program lists the last strophe that is well known.

Marína moja! teda tak sme my
ako tie božie plamene,
ako tie kvety v chladnej zemi,
ako tie drahé kamene;
padajú hviezdy, aj my padneme,
vädnú tie kvety, aj my zvädneme,
a klenoty hruda kryje:
Ale tie hviezdy predsa svietili,
a pekný život tie kvety žili,
a diamant v hrude nezhnije!

Each of us taught it at the class of Slovak.

var fs = require('fs');
const cheerio = require('cheerio');

var content = fs.readFileSync(__dirname + '/sladkovic-andrej_marina_txt.html');
const $ = cheerio.load(content);

var ar = $('.literallayout').toArray();
console.log('Length:', ar.length);
console.log('Verse 291:', $(ar[290]).text());

tocpp.js

This looks like a C++ conversion. The individual array elements are transformed to a text string corresponding to C++, and to be used in C++, the necessary quotes, the beginning and the end of the array are added.

var fs = require('fs');
const cheerio = require('cheerio');

var content = fs.readFileSync(__dirname + '/sladkovic-andrej_marina_txt.html');
const $ = cheerio.load(content);

var ar = $('.literallayout').toArray();

var ws = fs.createWriteStream('marina.h');
ws.write('const char PROGMEM *marina[] = {\n');

for(var i=0;i<ar.length;i++) {
  ws.write('  "' + $(ar[i]).text().trim().split('\n').join('\\n') + '",\n');
  }

ws.write('};\n');
ws.write('const int marinalen = ' + ar.length + ';\n');

Source code

The source code is located on the GitHub server.


26.12.2017


Menu