Skip to content

Instantly share code, notes, and snippets.

View jonniesweb's full-sized avatar
💭
hacking

Jon Simpson jonniesweb

💭
hacking
View GitHub Profile
@jonniesweb
jonniesweb / ParagraphParserExample.ts
Created January 9, 2024 21:19
Parse over paragraphs of a HMTL document while keeping track of the hierarchy of headings for it
// array of HTML elements to be parsed for headings and paragraphs
const htmlElements = document.querySelectorAll('h1,h2,h3,h4,h5,h6,p');
// store paragraphs with their hierarchy of headings
const paragraphData: Array<[string[], string]> = [];
// store current hierarchy of headings
const stack: Element[] = [];
// iterate through all html elements
Array.from(htmlElements).map((element) => {
@jonniesweb
jonniesweb / dnsmasq.conf
Last active October 18, 2020 17:37
A Dnsmasq configuration similar to the one that I use for my local network
domain-needed
bogus-priv
domain=home.mysite.com
local=/home.mysite.com/
expand-hosts
#log all dns queries
log-queries
#dont use hosts nameservers
no-resolv
no-poll
@jonniesweb
jonniesweb / docker-compose.yml
Last active October 18, 2020 16:13
A configuration file for running Dnsmasq using docker-compose
version: '2'
services:
dns:
image: strm/dnsmasq
volumes:
- ./config/dnsmasq.conf:/etc/dnsmasq.conf
ports:
- "53:53"
network_mode: host
cap_add:
@jonniesweb
jonniesweb / home.nix
Created May 23, 2020 16:35
An example home-manager configuration
{ config, pkgs, ... }:
{
programs.home-manager.enable = true;
programs.bat.enable = true;
programs.fzf.enable = true;
programs.fzf.enableZshIntegration = true;
home.packages = with pkgs; [
entr
fd
@jonniesweb
jonniesweb / application_controller.rb
Created January 7, 2019 16:17
rack_mini_profiler only run if user is authorized and url param is present
before_action do
if current_user
&& current_user.is_admin?
&& params(:pp)
Rack::MiniProfiler.authorize_request
end
end
@jonniesweb
jonniesweb / redcarpet.rb
Created July 29, 2018 23:30
Render markdown into HTML using the Redcarpet library
require 'redcarpet'
text = '*markdown!*'
# Initialize a Markdown parser
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
# render the text to HTML
markdown.render(text) # returns "<p><em>markdown!</em></p>"
@jonniesweb
jonniesweb / future_lazy_resolve.rb
Last active April 3, 2018 02:15
Using Concurrent::Future as return values in GraphQL Ruby
# app/graphql/types/query_type.rb
Types::QueryType = GraphQL::ObjectType.define do
name "Query"
field :ticket, types.String do
resolve ->(obj, args, ctx) {
Concurrent::Future.execute do
BackendTicketService.fetch(1234)
end
}
@jonniesweb
jonniesweb / typhoeus_example.rb
Last active March 25, 2018 15:55
A simple example of using the Typhoeus parallel HTTP client library
# setup requests
request_one = Typhoeus.get("www.example.com")
request_two = Typhoeus.get("www.google.com")
request_three = Typhoeus.get("www.bing.com")
# initialize a "Hydra" for performing requests in parallel
hydra = Typhoeus::Hydra.new
hydra.queue(request_one)
hydra.queue(request_two)
hydra.queue(request_three)
@jonniesweb
jonniesweb / lazy_resolver.rb
Created March 23, 2018 13:36
Lazily Resolved GraphQL
# app/graphql/ticket_resolver.rb
class TicketResolver
def initialize(id)
@id = id
end
def ticket
if @ticket
@ticket
else
@jonniesweb
jonniesweb / compiler.rb
Created March 12, 2018 14:48
Destroy All Software - Build a Compiler
class Tokenizer
TOKEN_TYPES = [
[:def, /\bdef\b/],
[:end, /\bend\b/],
[:identifier, /\b[a-zA-Z]+\b/],
[:integer, /\b[0-9]+\b/],
[:oparen, /\(/],
[:cparen, /\)/],
[:comma, /,/]
]