~sircmpwn/cgi-scripts

6dd0da30bc85ac5d498b8d398507dc26706ecc4c — Drew DeVault 3 years ago c0cd51b
web2gmi: implement ul, ol
1 files changed, 33 insertions(+), 13 deletions(-)

M web2gmi.js
M web2gmi.js => web2gmi.js +33 -13
@@ 19,19 19,30 @@ const sanitizePre = html => sanitizer(html, {

const convert = (dom, title) => {
  let output = "";
  output += "NOTICE: This page was generated from HTML. Your mileage may vary.\n\n";

  const elements = [...dom.querySelectorAll(`
    .page h1, .page h2, .page h3, .page h4, .page h5,
    .page p,
    .page img,
    .page blockquote,
    .page pre
    .page pre,
    .page a,
    .page ul, .page ol
  `)];
  if (elements[0].tagName.toLowerCase() !== "h1") {
    output += `# ${title}\n\n`;
  }

  const emitLink = link => {
      let desc = sanitize(link.innerHTML);
      if (desc === "") {
        const img = el.querySelector("img");
        if (img) {
          desc = `${img.alt ? " " + img.alt : ""}\n\n`;
        }
      }
      output += `=> ${link.href} ${desc}\n`;
  };

  let visited = [];
  for (let i = 0; i < elements.length; i++) {
    let el = elements[i];


@@ 40,22 51,31 @@ const convert = (dom, title) => {
    }
    visited.push(el);

    let links = [];
    switch (el.tagName.toLowerCase()) {
    case 'p':
        output += sanitize(el.innerHTML) + "\n\n";
        const links = [...el.querySelectorAll("a")];
        links.map(link => {
          let desc = sanitize(link.innerHTML);
          if (desc === "") {
            const img = el.querySelector("img");
            if (img) {
              desc = `${img.alt ? " " + img.alt : ""}\n\n`;
            }
          }
          output += `=> ${link.href} ${desc}\n`;
        links = [...el.querySelectorAll("a")];
        links.map(emitLink);
        if (links.length !== 0) {
          output += "\n";
        }
        break;
    case 'a':
        emitLink(el);
        break;
    case 'ul':
    case 'ol':
        links = [...el.querySelectorAll("a")];
        [...el.children].map(item => {
          output += `* ${sanitize(item.innerHTML)}\n`;
        });
        output += "\n";

        if (links.length !== 0) {
          output += "\n";
          links.map(emitLink);
          output += "\n";
        }
        break;
    case 'h1':