Skip to content

Instantly share code, notes, and snippets.

View deanebarker's full-sized avatar

Deane Barker deanebarker

View GitHub Profile
@deanebarker
deanebarker / auto-submit.html
Last active August 30, 2025 14:50
Binds some JS to an input field to perform some action when the user pauses typing
<!--
Binds the JS required to do something when the user pauses typing (when the element "triggers," for the purposes of this comment)
<input submitdelay="300" eventname="body:inputEntry" submitfunction="submit" /">
* "submitdelay" is the millisecond delay in typing that will trigger. If not included, defaults to 300 milliseconds.
* "submitevent" is the name of the event that will dispatch from the element when the element triggers.
The event will have two properties:
@deanebarker
deanebarker / data-storage-field.js
Last active August 6, 2025 12:05
Saves and populates form elements from local storage
/*
Just put a "data-storage-key" attribute on your form fields:
<input name="authkey" data-storage-key="saved_auth_key"/>
The value can be anything that is a valid key in localStorage (stick with mostly strings and you'll be fine).
When the form submits, whatever is in the field will be saved to local storage under the provided key name.
Next time the page loads, that form field will be populated from local storage.
@deanebarker
deanebarker / WebServer.cs
Created April 5, 2025 12:26
A very simple web server for C#, suitable for testing
// Doc here: https://deanebarker.net/tech/code/webserver/
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace DeaneBarker.Utils
@deanebarker
deanebarker / markdig-post-parse.cs
Last active February 14, 2025 13:48
How to iterate and manipulate a parsed Markdig document
var markdown = @"
This is some text.
This is a [link](http://deanebarker.net/)
";
var builder = new MarkdownPipelineBuilder();
var pipeline = builder.Build();
@deanebarker
deanebarker / FixWrongIdentifier.cs
Last active February 11, 2025 21:46
Demostrates using a visitor to modify a Fluid template post-parse
void Main()
{
var source = "Hi {{ this_is_the_wrong_identifier }}!";
var template = new FluidParser().Parse(source);
// This replaces "this_is_the_wrong_identifier" with "name"
var visitor = new FixWrongIdentifier();
template = visitor.VisitTemplate(template);
var context = new TemplateContext();
@deanebarker
deanebarker / LiquidScriptData.cs
Last active December 10, 2024 14:21
A hacked up way of writing script-ish code in Liquid/Fluid
// Example usage
var data = new LiquidScriptData()
{
["first_name"] = "Annie",
["last_name"] = "Barker"
};
var keysThatChanged = data.Evaluate(@"
if first_name == 'Annie'
assign first_name = 'Deane'
@deanebarker
deanebarker / FileSystemSyncManager.cs
Last active September 28, 2024 12:00
C# code to do a one-way file sync between a source and a target directory
// Doc here: https://deanebarker.net/tech/code/directory-sync/
public class FileSystemSyncManager
{
public List<LogEntry> Log { get; private set; } = new();
public FileRefCollection SourceFiles { get; set; }
public FileRefCollection TargetFiles { get; set; }
// Return false from any of these to cancel the operation
@deanebarker
deanebarker / JsonataValue.cs
Last active August 26, 2024 11:21
A Fluid value that allows extraction of data from JSON using JSONata.
// Information on JSONdata: https://jsonata.org/
// Requires this class also: https://gist.github.com/deanebarker/d9983155603d1adf26197787a9c74ae4
// in C#
var json = GetABunchOfJson();
var context = new TemplateContext();
context.SetValue("json", new JsonataValue(json));
@deanebarker
deanebarker / Tag.cs
Last active September 4, 2024 18:45
Handy C# class for easily and cleanly building HTML tags
// Doc here: https://deanebarker.net/tech/code/tag-builder/
public class Tag
{
private readonly string[] selfClosing = new[] { "img", "br", "hr", "input", "link", "meta" };
public List<Tag> Children = new();
public Tag(string input, string content, params Tag[] children)
{
@deanebarker
deanebarker / JsonataDoc.cs
Last active November 23, 2025 01:37
A combined search and deserialization library using JSONata syntax
// Requires Nuget package Jsonata.Net.Native
// Doc here: https://deanebarker.net/tech/code/jsonata-doc/
public class JsonataDoc
{
// This is public so you can change the options if you want
public JsonSerializerOptions DeserializerOptions = new();
private string json;