Skip to content

Commit

Permalink
feature(cheerio): Add html parsing on Cheerio
Browse files Browse the repository at this point in the history
Add htmlSrc option to parse with Cheerio for support nested html in translate and translate-attr directives
  • Loading branch information
ymkins committed Dec 11, 2017
1 parent 31d8f8d commit 89312b4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ WIP!
Options src and jsonSrc may be specified according to the grunt Configuring tasks guide.

- [src](#src)
- [htmlSrc](#htmlSrc)
- [nullEmpty](#nullempty-v026)
- [namespace](#namespace-v026)
- [interpolation](#interpolation)
Expand All @@ -116,10 +117,21 @@ Options src and jsonSrc may be specified according to the grunt Configuring task
Type: `Array`
Default: `undefined`

Example: `[ 'src/**/*.js' ]`
Example: `[ 'src/**/*.js', 'src/**/*.html' ]`

Define a file list to parse for extract translation.

#### htmlSrc

Type: `Array`
Default: `undefined`

Example: `[ 'src/**/*.html' ]`

Define a file list to parse with [cheerio](https://github.com/cheeriojs/cheerio).

There **translate** and **translate-attr** directives with nested html are supported.

#### nullEmpty (v0.2.6)

Type: `Boolean`
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
],
"readmeFilename": "README.md",
"dependencies": {
"cheerio": "^1.0.0-rc.2",
"flat": "^1.2.0",
"glob-expand": "0.1.0",
"json-stable-stringify": "^1.0.0",
Expand Down
42 changes: 42 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*l
*/

import cheerio from 'cheerio'
import fs from 'fs'
import path from 'path'
import _ from 'lodash'
Expand Down Expand Up @@ -42,6 +43,7 @@ var extractor
// Declare all var from configuration
var files = _utils.expand(this.data.src),
dest = _utils.getRealPath(this.data.dest || '.'),
htmlSrc = _utils.expand(this.data.htmlSrc || []),
jsonSrc = _utils.expand(this.data.jsonSrc || []),
jsonSrcName = _.union(this.data.jsonSrcName || [], ['label']),
interpolation = this.data.interpolation || {startDelimiter: '{{', endDelimiter: '}}'},
Expand Down Expand Up @@ -276,6 +278,46 @@ var extractor

})

// Parse all files in htmlSrc with cheerio
htmlSrc.forEach((file) => {
_log.debug("Process file with cheerio: " + file)
let content = fs.readFileSync(file, { encoding: 'utf8' }),
$ = cheerio.load(content, { decodeEntities: false }),
$targets,
storeTranslationValueByKey = (k, v) => {
// Avoid empty keys
if (k === '') return
// Avoid interpolated keys
if (_.startsWith(k, interpolation.startDelimiter) &&
_.endsWith(k, interpolation.endDelimiter) &&
!(k.split(interpolation.endDelimiter).length - 2)) {
return
}
results[k] = (keyAsText === true) ? k : v
}

// Find translate directive
$targets = $('[translate]')
$targets.each((i, el) => {
el = $(el)
let translationValue = el.html().trim(),
translationKey = el.attr('translate').trim() || translationValue
storeTranslationValueByKey(translationKey, translationValue)
})

// Find translate-attr directive
$targets = $('[translate-attr]')
$targets.each((i, el) => {
el = $(el)
let ta = eval(`(${el.attr('translate-attr')})`)
_.forEach(ta, (v, k) => {
let translationValue = (el.attr(k) || '').trim(),
translationKey = v
storeTranslationValueByKey(translationKey, translationValue)
})
})
})

// Parse all extra files to extra
jsonSrc.forEach(function (file) {
_log.debug("Process extra file: " + file)
Expand Down

0 comments on commit 89312b4

Please sign in to comment.