Archives For January 2018

taking out the trump

January 24, 2018

Last year about this time I started a series of anti-Trump posts based on the Devil’s Dictionary, where I would pick a letter for the day and a number of DD entries to slyly use as criticisms of that idiot. While it might have made me feel good it was totally useless as far as fixing the problem was concerned. After the letter J, I called a hiatus in early March. The reason then, as ever now, is because “the raging dumpster fire of a presidency” is still sucking the oxygen out of just about everything around it.

The far bigger issue for me is faith. I’m a Christian who practices his faith, in part, as a Methodist. When Tony Perkins, one of the self-proclaimed so-called evangelical Christian leaders, is willing to overlook Trump’s adulterous lifestyle, up to and including his cheating on Milania with porn actress Stormy Daniels while Milania was home with their new-born son (not to mention Milania is his third trophy wife), then I have no use for any of the so-called evangelical leaders in this country. Not a single damn one of them. They have literally sold their eternal souls to the devil in the form of Trump because, as Tony so eloquently put it, they “were tired of being kicked around by Barack Obama and his leftists.”

Let me tell you something Tony. Call me a leftist all you want, but also call me a strict constitutionalist. I will defend and die if necessary for the United State Constitution. And the First Amendment to the US Constitution says, and I quote:

Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech, or of the press; or the right of the people peaceably to assemble, and to petition the Government for a redress of grievances.”

Read that first third of the First Amendment over and over until it finally, if possible, sinks into that thick skull of yours. Ever heard of the phrase “freedom n individual unfit to run for public office, let alone be elected and serve. What’s scary about the special Alabama election that allowed Moore to run is that he nearly won. There were that many Alabama voters stupid enough to vote for him. Doug Jones didn’t win so much because Alabamans had an epiphany, it’s because many more Alabamans than usual voted, and it was those energized voters who voted against him. There’s a base of Republican idiots in Alabama and they came out in force for Moore, and it’s that base that’s still a threat to democracy in Alabama and the nation.

This is the last time I’ll ever write about Trump, unless he winds up being impeached and forced from office. And while I think it’s possible to impeach him, getting him out before his four years are up is going to be just about impossible. Unless, like Nixon, Mueller finds enough evidence against him to charge and convict him of whatever.

My blog is my corner of sanity where I can go and write about whatever I want, as long as it’s not about Trump and his brand of poisonous politics. I have a Twitter account for politics. And there are others out there on the web who like to write about the small fingered vulgarian and his industrial scale mendacity.

micro wiki written in go

January 21, 2018

It’s been quite a while since I last blogged about writing in Google’s Go. The language itself has evolved considerably over that period of time, but my skills in it have basically stood still. Go, however, has not, especially its use around today’s contemporary software environments, such as containers. It’s also available as a regular installable package on Arch ARM for the Raspberry Pi 2/3.

I’ve been investigating network programming on the Raspberry Pi, looking for a minimalist set of tools that can provide robust connectivity and functionality. One of the tools I’ve been looking for is a web server or wiki. Turns out that there’s a Go tutorial that leads the reader through the steps necessary to create a minimal wiki. It’s located here: Writing Web Applications.

Once I finished the tutorial I followed through with the suggested simple tasks at the bottom of the tutorial. One of them was the ability to add WiKi markup to create tags within the body of any page I created. I followed Help:Wikitext for syntax and to see how regular Wiki handles markup. I implemented very little: links, headers, single unordered lists, italic and bold text. I felt that was enough to get started, and it’s enough to drop onto a Raspberry Pi as a bare executable. There are a few more key features I’d like to add, such as table support, but that’s for another round of coding. Now, I’m taking a break while I go off and do other tasks.

Here’s the code.

// Personal Wiki Sandbox.
//
// Original Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//

package main

import (
	"html/template"
	"io/ioutil"
	"net/http"
	"os"
	"regexp"
	"fmt"
)

import s "strings"
var pr = fmt.Println

const dataDir = "data/"
const dataType = ".txt"
const templateDir = "templates/"

type Page struct {
	Title       string
	Body        []byte
	DisplayBody template.HTML
}

func (page *Page) save() error {
	os.Mkdir(dataDir, 0775)
	filename := dataDir + page.Title + dataType
	return ioutil.WriteFile(filename, page.Body, 0600)
}

func loadPage(title string) (*Page, error) {
	filename := dataDir + title + dataType
	body, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}
	return &Page{Title: title, Body: body}, nil
}

// I'm a regular expression neophyte. If you can figure out what I'm doing and can offer a better way of writing
// any of them, then I'm ready to learn.
//
var linkRegexp =   regexp.MustCompile( "\\[\\[([\\w ]+)\\]\\]" )
var headingOne =   regexp.MustCompile( "^=(.*)=$" )
var headingTwo =   regexp.MustCompile( "^==(.*)==$" )
var headingThree = regexp.MustCompile( "^===(.*)===$" )
var headingFour =  regexp.MustCompile( "^====(.*)====$" )
var headingFive =  regexp.MustCompile( "^=====(.*)=====$" )
var headingSix =   regexp.MustCompile( "^======(.*)======$" )
var boldText =     regexp.MustCompile( "'''(.*?)'''" )
var italicText =   regexp.MustCompile( "''(.*?)''" )

func expandHeading(line string) string {
    result := line

    if headingSix.MatchString(line) {
        result = "<h6>" + s.TrimSpace(s.Trim(line, "=")) + "</h6>\n"
    } else if headingFive.MatchString(line) {
        result = "<h5>" + s.TrimSpace(s.Trim(line, "=")) + "</h5>\n"
    } else if headingFour.MatchString(line) {
        result = "<h4>" + s.TrimSpace(s.Trim(line, "=")) + "</h4>\n"
    } else if headingThree.MatchString(line) {
        result = "<h3>" + s.TrimSpace(s.Trim(line, "=")) + "</h3>\n"
    } else if headingTwo.MatchString(line) {
        result = "<h2 style=\"margin-bottom: 0.25em; border-bottom: 1px solid #808080;\">" +
            s.TrimSpace(s.Trim(line, "=")) + "</h2>\n"
    } else if headingOne.MatchString(line) {
        result = "<h1 style=\"margin-bottom: 0.25em; border-bottom: 2px solid #808080;\">" +
            s.TrimSpace(s.Trim(line, "=")) + "</h1>\n"
    }

    return result
}

func viewHandler(writer http.ResponseWriter, request *http.Request, title string) {
	page, err := loadPage(title)

	// If the page doesn't exist, then redirect to the editing function and create the page.
	//
	if err != nil {
		http.Redirect(writer, request, "/edit/"+title, http.StatusFound)
		return
	}

	// Start page processing.
	// Break on the new line (\n) because that's common to both Windows/DOS and Unix and Unix-like editors.
	// We'll strip off any carriage returns (\r) as white space.
	//
	lines := s.Split(string(page.Body), "\n")

	var buildingUnorderedList bool
	// expandedBody is where all the displayable text with HTML decorations will be collected.
	//
	var expandedBody []string

	for _, oneline := range lines {
		//
		// Strip all leading and trailing white space, including carriage returns (\r).
		// This will help later when we join all the strings back together using the HTML break token
		// followed by a new line (\n).
		//
		newLine := s.TrimSpace(oneline)

        // Bold and italicize any marked text. Try to handle the mixing of both.
        //
        newLine = boldText.ReplaceAllStringFunc(
            newLine,
            func(str string ) string {
                matched := boldText.FindStringSubmatch(str)
                return "<b>" + matched[1] + "</b>"
            })

        newLine = italicText.ReplaceAllStringFunc(
            newLine,
            func(str string ) string {
                matched := italicText.FindStringSubmatch(str)
                return "<i>" + matched[1] + "</i>"
            })

        // Create links from [[PageName]] pattern.
        // Note that we can accept a link with spaces. I substitute the '_' for a space. I could, perhaps,
        // use the %20 called for in the standards, but I decided underscores were better.
        //
        newLine = linkRegexp.ReplaceAllStringFunc(
            newLine,
            func(str string) string {
                matched := linkRegexp.FindStringSubmatch(str)
                return "<a href=\"/view/" + s.Replace(matched[1], " ", "_", -1) + "\">" + matched[1] + "</a>"
            })

        // newLine = expandHeading(newLine)

		// Look for unordered lists. If we find an unordered list notation, then start building that list.
		//
		if s.HasPrefix(newLine, "*") {
			nl2 := s.TrimSpace(s.SplitAfterN(newLine, "*", 2)[1])

			if !buildingUnorderedList {
				buildingUnorderedList = true
				expandedBody = append(expandedBody, "<ul>\n")
			}

			expandedBody = append(expandedBody, "<li>"+nl2+"</li>\n")
			continue
		} else if buildingUnorderedList {
            buildingUnorderedList = false
            expandedBody = append(expandedBody, "</ul>\n")
		}

		// Look for headings and the rest of the document.
		//
		if s.HasPrefix( newLine, "=" ) && s.HasSuffix( newLine, "=" ) {
            expandedBody = append(expandedBody, expandHeading(newLine))
            continue
        }

        expandedBody = append(expandedBody, newLine+"<br />\n")
	}

	// Rejoin all the lines we created with the initial split using he HTML break followed by a new line.
	//
	page.DisplayBody = template.HTML([]byte(s.Join(expandedBody, "")))
	renderTemplate(writer, "view", page)
}

func editHandler(writer http.ResponseWriter, request *http.Request, title string) {
	p, err := loadPage(title)
	if err != nil {
		p = &Page{Title: title}
	}
	renderTemplate(writer, "edit", p)
}

func saveHandler(writer http.ResponseWriter, request *http.Request, title string) {
	body := request.FormValue("body")
	p := &Page{Title: title, Body: []byte(body)}
	err := p.save()
	if err != nil {
		http.Error(writer, err.Error(), http.StatusInternalServerError)
		return
	}
	http.Redirect(writer, request, "/view/"+title, http.StatusFound)
}

var templates = template.Must(
	template.ParseFiles(templateDir+"edit.html", templateDir+"view.html"))

func renderTemplate(writer http.ResponseWriter, tmpl string, p *Page) {
	err := templates.ExecuteTemplate(writer, tmpl+".html", p)
	if err != nil {
		http.Error(writer, err.Error(), http.StatusInternalServerError)
	}
}

func rootHandler(writer http.ResponseWriter, request *http.Request) {
	http.Redirect(writer, request, "/view/FrontPage", http.StatusFound)
}

var validPath = regexp.MustCompile("^/(edit|save|view)/([\\w ]+)$")

func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		m := validPath.FindStringSubmatch(r.URL.Path)
		if m == nil {
			http.NotFound(w, r)
			return
		}
		fn(w, r, m[2])
	}
}

func main() {
	http.HandleFunc("/", rootHandler)
	http.HandleFunc("/view/", makeHandler(viewHandler))
	http.HandleFunc("/edit/", makeHandler(editHandler))
	http.HandleFunc("/save/", makeHandler(saveHandler))

	http.ListenAndServe(":8080", nil)
}

Here’s a typical markup page.

= Main Page =

This is the front page. Basic testing of all the capabilities that can be added to a page of text with minimal Wiki markup support.

= Heading 1 With ''Bloody === Equals'' In The Middle =
== Heading 2 ==
=== Heading 3 ===
==== Heading 4 ====
===== Heading 5 =====
====== Heading 6 ======

Example testing of links.

[[TestName1]] [[foobar]]
[[TestName2]]
[[Test Name 3]]

[[ANewPage]]
[[ANewPage2]]
* one ''italic'' bulleted list entry with a link: [[ANewPage]]
* two '''bold''' bulleted list entry
Example of testing '''bold text multiple''' times in the '''same''' sentence.
Example of testing ''italic '''text''' multiple'' times in the ''same'' sentence.
This is ''' ''Italic and Bold text together.'' '''

And here’s what it looks like rendered in Chrome on a Macbook Pro.

Features

  1. Handles links with [[ and ]] notation. Also handles spaces within a page link.
  2. Handles italic and bold text, and combinations of the two.
  3. Handles headings, from h1 to h6.

Blank lines and lines that aren’t headings and part of an unordered list have a single HTML break appended to them. Other than that it’s just a way to organize text. I’m even writing a stripped down version that won’t allow you to edit any of the pages, for embedded read-only rendering.