Skip to content

Instantly share code, notes, and snippets.

View dmsnell's full-sized avatar

Dennis Snell dmsnell

View GitHub Profile
@dmsnell
dmsnell / extract-paragraphs.php
Last active August 9, 2024 17:55
Extract the HTML within P tags using the HTML API.
<?php
/**
* Place this file into a WordPress directory with a proper config and run.
* It doesn't need the config, but it needs to load all of the HTML API and
* Token Map modules. This can be done by replacing the `require_once` with
* a `require_once` for each of the appropriate files.
*
* - With no arguments it prints the sample HTML.
* - If the last argument is a dash (-), it reads the HTML from stdin.
@dmsnell
dmsnell / add-the-utf8-mark.py
Last active July 31, 2024 19:31
Prepend the BOM to indicate a file is UTF-8
import codecs
import sys
def prepend_bom(filename):
with open( filename, 'r+b' ) as csv:
existing = csv.read()
if b'\xef\xbb\xbf' == existing[0:3]:
print("Byte-order mark already present: leaving file as-is.")
return
@dmsnell
dmsnell / post-no-sourced-attributes.html
Created December 7, 2022 00:10
Gutenberg serialized HTML post with no sourced attributes.
<!-- wp:paragraph {"content":"This paragraph has \u003cem\u003e\u003cstrong\u003eno sourced attributes\u003c/strong\u003e\u003c/em\u003e. It's content is fully stored inside the JSON attributes \u003cem\u003eand\u003c/em\u003e it still contains the rendered HTML useful for rendering it with no server is available."} -->
<p>This paragraph has <em><strong>no sourced attributes</strong></em>. It's content is fully stored inside the JSON attributes <em>and</em> it still contains the rendered HTML useful for rendering it with no server is available.</p>
<!-- /wp:paragraph -->
<!-- wp:image {"url":"https://wordpress.org/files/2022/08/theme-styles.png","caption":"Image captions are usually sourced from the \u003ccode\u003efigcaption\u003c/code\u003e element.","width":650,"height":406,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large is-resized"><img src="https://wordpress.org/files/2022/08/theme-styles.png" alt="" width="650" height="406"/><figcaption class="wp-element-captio
@dmsnell
dmsnell / counter.mjs
Created August 29, 2020 20:31
50k `async` JS processes
export const counter = async ({send, receive}) => {
let count = 0;
while (1) {
const [action, args] = await receive();
switch (action) {
case 'inc':
count++;
break;
@dmsnell
dmsnell / demo.js
Last active August 27, 2020 21:56
Process Primitives
const { spawn, send } = require('./process.js')
const counter = spawn( async (receive) => {
let count = 0;
while (1) {
const message = await receive();
if (message === 'inc') {
console.log(++count);
@dmsnell
dmsnell / sequenced-async.ts
Created August 17, 2020 16:24
JS worker processing sync and async inbound messages sequentially, inspired by Erlang gen_server
import { MessageChannel, MessagePort } from 'worker_threads';
type ServerDescription<K extends string> = {
[type in K]: (...args: unknown[]) => unknown;
}
type ServerHandler<M extends ServerDescription<keyof M>, S> = <T extends keyof M>(msg: T, state: S) => [ReturnType<M[T]>, S] | Promise<[ReturnType<M[T]>, S]>;
class GenericServer<M extends ServerDescription<keyof M>, S = undefined> {
count = 0;
@dmsnell
dmsnell / tag-hasher.java
Last active July 5, 2020 01:01
Simplenote Tag Hashing Functions - generate tag entity id from tag name
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.Normalizer;
import java.util.Locale;
class TagHasher {
public static String tagHash(String tagName) {
try {
String normalized = Normalizer.normalize(tagName, Normalizer.Form.NFC);
String lowercased = normalized.toLowerCase(Locale.ENGLISH);
@dmsnell
dmsnell / BlockParser.java
Last active April 12, 2020 03:49
Native Java BlockParser for Gutenberg Posts
import java.util.ArrayList;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
class BlockParser {
@dmsnell
dmsnell / formula-block-editor.js
Last active September 9, 2019 19:51
Formula-entry and mathematical typesetting block
const { registerBlockType } = wp.blocks;
const { TextareaControl } = wp.components;
const { createElement: el } = wp.element;
const attributes = {
formula: { type: 'string' },
rendered: {
type: 'string',
source: 'attribute',
selector: 'img',
@dmsnell
dmsnell / Main.elm
Created April 4, 2019 05:25
Using the URL fragment/query args to determine which "sub app" to render, using initial URL to inform state
module Main exposing (main)
import Browser as B
import Browser.Navigation as BN
import Html as H
import Html.Attributes as HA
import Html.Events as HE
import Url as U