How to use the htmlparser2.Parser function in htmlparser2

To help you get started, we’ve selected a few htmlparser2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ionic-team / ionic-v1 / config / gulp-tasks / docs.js View on Github external
if(name == 'section') {
                // section closed, index this section then clear it out
                addToIndex(section.path, section.title, pageData.layout, section.body);
                section = { body: '', title: '' };
              } else if( (name == 'h1' || name == 'h2' || name == 'h3') && isTitleOpen) {
                isTitleOpen = false;
              }
            }
          });
          parser.write(contents);
          parser.end();

        } else {
          // index the entire page
          var body = '';
          parser = new htmlparser.Parser({
            ontext: function(text){
              body += text.replace(/{%.*%}/, '', 'g'); // Ignore any Jekyll expressions
            }
          });
          parser.write(contents);
          parser.end();

          addToIndex(path, pageData.title, pageData.layout, body);
        }

        callback();

      })).on('end', function() {
        // Write out as one json file
github jshint / jshint / src / cli.js View on Github external
return true;
      });
    }

    // check for startOffset again to remove leading white space from first line
    lines.forEach(function() {
      lineCounter += 1;
      if (startOffset) {
        offsets[lineCounter] = startOffset.length;
      } else {
        offsets[lineCounter] = 0;
      }
    });
  }

  var parser = new htmlparser.Parser({ onopentag: onopen, onclosetag: onclose, ontext: ontext });
  parser.parseComplete(code);
  return offsets;
}
github prisma / photonjs / packages / fetch-engine / src / getLatestAlphaTag.ts View on Github external
return new Promise(resolve => {
    const parser = new htmlparser.Parser(
      new htmlparser.DomHandler((err, result) => {
        const bucketTag = result.find(child => child.name === 'listbucketresult')
        if (!bucketTag) {
          resolve({ objects: [], isTruncated: false, nextContinuationToken: null })
        }
        const isTruncated = getKey(bucketTag, 'istruncated')
        const nextContinuationToken = getKey(bucketTag, 'nextcontinuationtoken')
        resolve({
          objects: bucketTag.children
            .filter(c => c.name === 'contents')
            .map(child => {
              return child.children.reduce((acc, curr) => {
                acc[curr.name] = curr.children[0].data
                return acc
              }, {})
            }),
github lalalic / docx4js / src / document.js View on Github external
static parseXml(data){
		try{
			let opt={xmlMode:true,decodeEntities: false}
			let handler=new ContentDomHandler(opt)
			new Parser(handler,opt).end(data)
			let parsed=cheer.load(handler.dom,opt)
			if(typeof(parsed.cheerio)=="undefined")
				parsed.cheerio="customized"
			return parsed
		}catch(error){
			console.error(error)
			return null
		}
	}
}
github itoche / fluent-ffmpeg-filters / generator / metadata.js View on Github external
}

  function decLevel() {
    level--;
  }

  function logOpenTag(name) {
    //console.log(identStr() + '<' + name + '>');
    incLevel();
  }
  function logEndTag(name) {
    decLevel();
    //console.log(identStr() + '');
  }

  var parser = new htmlparser.Parser({
      onopentag: function(tagname, attribs){
          if (log) logOpenTag(tagname);
          if(tagname === "h3") {
            current = new FilterMeta();
            captureText = true;
          }
          if (captureParams && tagname === "dl") {
            if (captureParam) {
                captureAllowedValues = true;
            } else {
                captureParam = true;
            }

          }
          if (captureParam && tagname === "dt") {
            if (captureAllowedValues) {
github mmoskal / gitwed / server / expander.ts View on Github external
function setLocations(e: Cheerio, filename: string, fileContent: string) {
        allFiles[filename] = fileContent
        let mapping: SMap = {}
        let parser: any
        let handler = new htmlparser2.DomHandler(cheerioOptions, (elt: CheerioElement) => {
            //console.log(elt.tagName, elt.startIndex, parser.endIndex)
            mapping[elt.startIndex + ""] = parser.startIndex
        });
        parser = new htmlparser2.Parser(handler, cheerioOptions)
        parser.end(fileContent);

        let setloc = (x: Cheerio) => {
            let start = x[0].startIndex
            let end = mapping[start + ""]
            if (fileContent[start] == "<") {
                while (start < fileContent.length && fileContent[start] != ">")
                    start++;
                start++;
            }
            //console.log(`${ch.tagName}: "${fileContent.slice(start, end)}"`)
            x.attr("gw-pos", filename + "@" + start + "-" + (end - start))
        }

        if (e.attr("edit")) setloc(e)
        e.find("[edit]").each((i, ch) => setloc(h(ch)))
github superfly / fly / packages / v8env / src / document.ts View on Github external
function parseDOMSync(html) {
  const handler = new htmlparser.DomHandler()
  new htmlparser.Parser(handler).end(html)
  return handler.dom
}
github i18next / i18next-scanner / src / jsx-parser.js View on Github external
}
                    stack[0].childNodes.push({
                        nodeName: '#expression',
                        value: m[2],
                        childNodes: []
                    });
                    txt = m[3];
                }
            }
            if (txt) {
                addText(txt);
            }
        }
    };

    const parser = new htmlparser.Parser(handler, {
        xmlMode: true,
        decodeEntities: true
    });
    parser.write(fragment);

    return stack[0].childNodes;
}
github alexmingoia / html-virtualize / lib / virtualize.js View on Github external
function parse(html) {
  var vtree;
  var vnode;
  var parent_vnode;
  var open = [];

  var parser = new htmlparser.Parser({
    onopentag: function(name, attribs) {
      vnode = new VirtualNode(name, attribs, []);
      open.push(vnode);
      if (!vtree) vtree = vnode;
      if (parent_vnode) {
        var parent = parent_vnode;
        vnode.parent = parent_vnode;
        parent_vnode.children.push(vnode);
        parent.count++;
        while (parent = parent.parent) {
          parent.count++;
        }
      }
      parent_vnode = vnode;
    },
    ontext: function(text) {
github outmoded / metaphor / lib / tags.js View on Github external
...
                
            
            
                <img alt="Nate Cohn" src="https://pbs.twimg.com/profile_images/430382254993334272/R24BAgcz_400x400.png" class="ProfileAvatar-image">
            
        
     */

    const tweet = Twitter.isTweet(base);

    const tags = { og: [], twitter: [], meta: [] };
    let oembedLink = null;
    let smallestIcon = Infinity;

    const parser = new HtmlParser2.Parser({
        onopentag: function (name, attributes) {

            if (name === 'meta') {
                const property = attributes.property || attributes.name;
                const value = attributes.content || attributes.value;
                if (!property ||
                    !value) {

                    return;
                }

                if (['author', 'description'].indexOf(property) !== -1) {
                    tags.meta[property] = value;
                    return;
                }