Skip to content

Commit

Permalink
ADD: example Object
Browse files Browse the repository at this point in the history
  • Loading branch information
fega committed Mar 8, 2017
1 parent 4e07b2e commit fe1f5d0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 23 deletions.
84 changes: 62 additions & 22 deletions lib/lib/process-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,71 @@ function WRDtableMap (html) {
//iterate for each tr element
$('tr').map(function (i, el) {
var element = $(this)
var id = element.attr('id')
var clss = element.attr('class')
var html = element.html()
// set the title
if (clss === 'wrtopsection'){
if (isHeaderItem(element)) { // Creates a header item
result.title = element.text()
} else
// create a "translations element"
if (id !== undefined && (clss === 'even' || clss === 'odd')) {
var V = cheerio.load($(this).html())

var from = V('strong').text()
V('.ToWrd em span').remove()
V('.FrWrd em span').remove()
var fromType = V('.FrWrd em').text()
var toType = V('.ToWrd em').text()
V('.ToWrd em').remove()
var to = V('.ToWrd').text()
var item = {
from,
fromType,
toType,
to
}
result.translations.push(item)
} else if (isTranslationItem(element)) { // create a "translations element"
result.translations.push(createTranslationItem(html))
} else if (isExampleItem(element)) { // Adds the examples
result = pushExample(result, html)
}
})
return result
}
/**
* Creates a translation item from the tr provided as html
* @param {String} html
* @return {String}
*/
function createTranslationItem (html) {
var $ = cheerio.load(html)
var from = $('strong').text()
$('.ToWrd em span').remove()
$('.FrWrd em span').remove()
var fromType = $('.FrWrd em').text()
var toType = $('.ToWrd em').text()
$('.ToWrd em').remove()
var to = $('.ToWrd').text()
return {
from,
fromType,
toType,
to,
example: {
from: [],
to: []
}
}
}
/**
* push an example item contained in the html in the obj
* @param {Object} obj
* @param {String} html
* @return {Object}
*/
function pushExample (obj, html) {
var $ = cheerio.load(html)

if ($('.FrEx').text() !== '') {
obj.translations[obj.translations.length - 1].example.from.push($('.FrEx').text())
} else if($('.ToEx').text() !== '') {
obj.translations[obj.translations.length - 1].example.to.push($('.ToEx').text())
}

return obj
}
function isHeaderItem(element){
return element.attr('class') === 'wrtopsection'
}

function isTranslationItem(element){
var id = element.attr('id')
var clss = element.attr('class')
return (id !== undefined && (clss === 'even' || clss === 'odd'))
}
function isExampleItem(element){
var id = element.attr('id')
var clss = element.attr('class')
return (id === undefined && (clss === 'even' || clss === 'odd'))
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wordreference-api",
"version": "1.0.1",
"version": "1.1.0",
"description": "An api to use the worldreference.com information",
"homepage": "",
"author": {
Expand Down

0 comments on commit fe1f5d0

Please sign in to comment.