GEOUNIVERSAL .:.


pages - photos - log

webmaster

building web pages from markdown text files and templates

node js

notes as i build a markdown converter with node and showdownjs

all these that use readdirSync get mad if you have any folders in the md folder, will fix with typeof

test 1

make html file from txt file, no headers:

var showdown  = require('showdown'),
    fs        = require('fs'),
    converter = new showdown.Converter(),
    content   = fs.readFileSync('tetsuokogowa.txt', 'utf8');
    html      = converter.makeHtml(content);
    fs.writeFileSync("test.html", html);
console.log(content);console.log(html);

test 2

make html files from folder of txt files:

var showdown  = require('showdown'),
    fs        = require('fs'),
    converter = new showdown.Converter();

fs.readdirSync("md/").forEach(file => {
var content   = fs.readFileSync("md/" + file, 'utf8'),
    html      = converter.makeHtml(content),
    filename  = "html/" + file.slice(0, -4) + ".html";
fs.writeFileSync(filename, html);
});

test 3

build real html pages with headers and such from template:

var showdown  = require('showdown'),
    fs        = require('fs'),
    converter = new showdown.Converter();

fs.readdirSync("md/").forEach(file => {
var content   = fs.readFileSync("md/" + file, 'utf8'),
    body      =  '\n<section>\n' + converter.makeHtml(content) + '\n</section>\n',
    html      = fs.readFileSync("template/header.txt", 'utf8') + body + fs.readFileSync("template/footer.txt", 'utf8'),
    filename  = "html/" + file.slice(0, -4) + ".html";
fs.writeFileSync(filename, html);
});

test 4

make pages from txt files with certain metadata:

var showdown  = require('showdown'),
    fs        = require('fs'),
    converter = new showdown.Converter({metadata: true});

fs.readdirSync("md/").forEach(file => {
var content   = fs.readFileSync("md/" + file, 'utf8'),
    body      = '\n<section>\n' + converter.makeHtml(content) + '\n</section>\n',
    metadata  = converter.getMetadata(); console.log(metadata.tags !== undefined);
    if (metadata.tags !== undefined) { 
      if (metadata.tags.includes("pi")) {
        html      = fs.readFileSync("template/header.txt", 'utf8') + body + fs.readFileSync("template/footer.txt", 'utf8'),
        filename  = "html/" + file.slice(0, -4) + ".html";
        fs.writeFileSync(filename, html);
      }
    }
});

test 5

make multisection page from all txt files in a folder:

var showdown  = require('showdown'),
    fs        = require('fs'),
    converter = new showdown.Converter({metadata: true});

var header    = fs.readFileSync('template/header.txt', 'utf8'),
    body      = '',
    footer    = fs.readFileSync('template/footer.txt', 'utf8');

fs.readdirSync('md/').forEach(file => {
  var content = fs.readFileSync('md/' + file, 'utf8');
  body += '\n<section>\n' + converter.makeHtml(content) + '\n</section>\n';
});

html   = header + body + footer;
filename  = 'html/multi.html';
fs.writeFileSync(filename, html);

test 6

make multisection page from txt files with certain metadata:

var showdown  = require('showdown'),
    fs        = require('fs'),
    converter = new showdown.Converter({metadata: true}),
    header    = fs.readFileSync('template/header.txt', 'utf8'),
    body      = '',
    footer    = fs.readFileSync('template/footer.txt', 'utf8');

fs.readdirSync('md/').forEach(file => {
var content   = converter.makeHtml(fs.readFileSync('md/' + file, 'utf8')),
    metadata  = converter.getMetadata(); console.log(metadata.tags !== undefined);
    if (metadata.tags !== undefined) { 
      if (metadata.tags.includes('tech notes')) {
        body += '\n<section>\n' + content + '\n</section>\n';
      }
    }
});

html   = header + body + footer;
filename  = 'html/technotes.html';
fs.writeFileSync(filename, html);

link list test 1

make a page of links from a folder of internet shortcuts:

var fs        = require('fs'),
    header    = fs.readFileSync('template/header.txt', 'utf8'),
    body      = '',
    footer    = fs.readFileSync('template/footer.txt', 'utf8');

fs.readdirSync('links/').forEach(file => {
var link  = fs.readFileSync('links/' + file, 'utf8').split('URL=');    console.log(link[1]);
    link  = link[1].split('IDList=');
    link  = link[0].replace(/\r?\n|\r/g, "");; console.log(link)
    console.log(link);    

    body += '\n<section>\n<a href="' + link + '">' + file + '</a>\n</section>\n';

});

html      = header + body + footer;
filename  = 'html/test.html';

fs.writeFileSync(filename, html);

i lost the link to where i stole the replace function, regex still looks like alien mumbo jumbo to me

pages v1

var showdown  = require('showdown'),
    fs        = require('fs'),
    converter = new showdown.Converter({metadata: true}),
    header    = fs.readFileSync('template/header.txt', 'utf8'),
    body      = '',
    footer    = fs.readFileSync('template/footer.txt', 'utf8');

var customClassExt = { // https://github.com/showdownjs/showdown/issues/542
    type: 'output',
    filter: function (text) {
        return text
            // Add class for list (ol, ul)
            .replace(/<p>\[\.([a-z0-9A-Z\s]+)\]<\/p>[\n]?<(.+)>/g, `<$2 class="$1">`)

            // Add class for other blocks
            .replace(/<(.+)>\[\.([a-z0-9A-Z\s]+)\]/g, `<$1 class="$2">`)

            // Prevent class name with 2 dashs being replace by `<em>` tag
            .replace(/class="(.+)"/g, function (str) {
                if (str.indexOf("<em>") !== -1) {
                    return str.replace(/<[/]?em>/g, '_');
                }
                return str;
            });
    }
};
// Add extension to converter 
converter.addExtension(customClassExt);

fs.readdirSync("md/pages/").forEach(file => {
var content = converter.makeHtml(fs.readFileSync('md/pages/' + file, 'utf8'));
    body = content.split("<h2 ").join("</section>\n\n<section>\n<h2 ");
    body = body.split("\n").slice(1).join("\n") + "\n\n</section>";

    console.log(body);
    var html      = header + body + footer,
        filename  = 'html/' + file.slice(0, -4) + '.html';
    fs.writeFileSync(filename, html);
});

notebook v1

var showdown  = require('showdown'),
    fs        = require('fs'),
    converter = new showdown.Converter({metadata: true}),
    header    = fs.readFileSync('template/header.txt', 'utf8'),
    body      = '',
    footer    = fs.readFileSync('template/footer.txt', 'utf8');

fs.readdirSync('md/notes/').forEach(file => {
var content   = converter.makeHtml(fs.readFileSync('md/notes/' + file, 'utf8')),
    metadata  = converter.getMetadata(); console.log(metadata.tags !== undefined);
    if (metadata.tags !== undefined) { 
      if (metadata.tags.includes('notes')) {
        body = '\n<section>\n' + content + '\n</section>\n' + body;
      }
    }
});

var html = header + body + footer,
    filename = 'html/notes.html';
fs.writeFileSync(filename, html);

python

i switched to python as a web tool, recreated most of the above functions and here's what i got

webmaster6.py

import markdown
md = markdown.Markdown(extensions = ['meta', 'attr_list'])
import os

def convertlog(log):
    file = open(logpath + log, 'r')
    body = md.convert(file.read())
    file.close()

    if 'tags' in md.Meta:
        taglist = md.Meta['tags']
        tag = '<h4>' + ','.join(taglist) + '</h4>'
        print(log, tag)
        body = str(tag) + body

    body = '\n<section>' + body + '\n</section>'
    return body

def makepage(body):
    file = open('../templates/header.txt', 'r')
    header = file.read()
    file.close()

    file = open('../templates/footer.txt', 'r')
    footer = file.read()
    file.close

    html = header + body + footer
    return html

def savefile(html,filename):
    savepath = '../html/' + filename + '.html'
    savefile = open(savepath, 'w')
    savefile.write(html)
    savefile.close()

logpath = '../md/log/'
logpile = sorted(os.listdir(logpath))
logbook = []

for log in logpile:
    fullpath = os.path.join(logpath, log)

    if os.path.isdir(fullpath): 
        continue
    else: 
        filetype = log.split('.')[-1]
        filename = log.split('.')[0]

    if filetype == 'txt' or filetype == 'md':
        body = convertlog(log)
        logbook.append(body)
        page = makepage(body)
        savefile(page, filename)

logbook.reverse()

logbook = '\n'.join(logbook)
book = makepage(logbook)

savefile(book, "book")

log.py

import markdown
md = markdown.Markdown(extensions = ['meta', 'attr_list'])
import os

def convertlog(log, pagename):
  file = open(logpath + log, 'r')
  body = md.convert(file.read())
  print(log,pagename)
  file.close()

  body = makestamp(pagename) + '<hr>' + body

  if 'tags' in md.Meta:
    tags = md.Meta['tags'][0].split(', ')
    body = makelinktags(tags) + ' ' + body

  body = '\n<section>' + body + '\n</section>'
  return body

def makelinktags(tags):
  anchor_tag = '<a href="/log/{0}.html">{0}</a>'
  taglist = [anchor_tag.format(tag) for tag in tags]
  tagline = ', '.join(taglist)
  return tagline

def makestamp(pagename): # permalink for that entry
  return '<a href="/log/' + pagename + '.html">.:.</a>'

def makepage(body):
  file = open('../templates/header.txt', 'r')
  header = file.read()
  file.close()

  file = open('../templates/footer.txt', 'r')
  footer = file.read()
  file.close

  html = header + body + footer
  return html

def savefile(html, savepath):
  savefile = open(savepath, 'w')
  savefile.write(html)
  savefile.close()

logpath = '../md/log/'
logpile = sorted(os.listdir(logpath))
logbook = {}
logpages = []

# convert each log

for log in logpile:
  fullpath = os.path.join(logpath, log)

  if os.path.isdir(fullpath): 
    continue
  else: 
    filetype = log.split('.')[-1]
    pagename = log.split('.')[0]

  if filetype == 'txt' or filetype == 'md':
    logbook[pagename] = convertlog(log, pagename)

# make each individual page

for page in logbook:
  html = makepage(logbook[page])
  logpages.append(logbook[page])
  savepath = '../html/log/' + page + '.html'
  savefile(html, savepath)
  print(savepath)

# make one page for all entries

logpages.reverse()
logindex = makepage('\n'.join(logpages))
savepath = '../html/log/index.html'
savefile(logindex, savepath)