Skip to content

Instantly share code, notes, and snippets.

View deanebarker's full-sized avatar

Deane Barker deanebarker

View GitHub Profile
@deanebarker
deanebarker / dumb-parser-combinator.cs
Last active June 1, 2024 18:13
A very simple parser combinator
void Main()
{
// The text to test it on
var text = new WorkingText("Annie1aa2");
// The parser: hits on "Deane" or "Annie" following by a number, then two letters, then another number
var parser = new Sequence(
new Any(
new TextParser("Deane"),
new TextParser("Annie")
@deanebarker
deanebarker / HideVisualViewUIDescriptor.cs
Created November 14, 2022 20:28
Code to hide preview and on-page edit views for content that has no visual component
[UIDescriptorRegistration]
public class HideVisualViewUIDescriptor : UIDescriptor<[Page Type or interface>
{
public HideVisualViewUIDescriptor() : base()
{
DefaultView = CmsViewNames.AllPropertiesView;
EnableStickyView = false;
DisabledViews = new[] { CmsViewNames.PreviewView, CmsViewNames.OnPageEditView };
}
}
@deanebarker
deanebarker / BookRankings.cs
Last active October 13, 2022 22:34
Code to process the NY Times Bestseller List
void Main()
{
// This code depends on the FlatFiles Nuget package: https://github.com/jehugaleahsa/FlatFiles
// Download the dataset from here: https://view.data.post45.org/nytfull
var pathToFile = @"[path to file]";
var mapper = DelimitedTypeMapper.Define<BookRanking>();
mapper.Property(c => c.Year).ColumnName("year");
mapper.Property(c => c.Week).ColumnName("week").InputFormat("yyyy-MM-dd");
@deanebarker
deanebarker / ExtractTextFromWord.cs
Last active August 19, 2022 13:30
A quick and dirty way to extract text from Word document via C#
// This is some rough/stub code for extracting raw text from a Word document
// A modern Word document (.docx) is just a zip file. Extract it.
// Find a file called word/document.xml. That contains the text of the document.
// Paragraphs are in "w:p" tags, and text is in "w:t".
// Iterate the "p" tags, then concatenate all the "t" tags inside them
void Main()
{
var doc = XDocument.Parse(File.ReadAllText(" [path to word/document.xml] "));
XNamespace nsW = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
@deanebarker
deanebarker / Startup.cs
Created May 3, 2022 21:47
The simplest webhook system for Optimizely Content Cloud
public void Configuration(IAppBuilder app)
{
// This is the URL template to call
// The placeholders are optional -- they will be replaced with data specific to the content that is published
var webhookUrl = "https://example.com?id={id}&type={type}&version={version}";
// Here is an example URL that would be called for this example:
// https://example.com?id=17&type=ArticlePage&version=691
var events = ServiceLocator.Current.GetInstance<IContentEvents>();
@deanebarker
deanebarker / ExtractFragmentMiddleware.cs
Last active September 9, 2024 15:05
Replace the body of a response with HTML extracted from that same body
// Doc here: https://deanebarker.net/tech/code/extract-fragment-middleware/
// Requires AngleSharp (via Nuget)
using AngleSharp.Html.Parser;
using Microsoft.AspNetCore.Http;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace DeaneBarker.Optimizely.Middleware
@deanebarker
deanebarker / Editor.js
Last active February 16, 2022 14:16
The world's simplest code editor for Optimizely Content Cloud
/*
This is specifically designed to do as little as possible and have no dependencies.
1. It's styled sort of like a code editor (?)
2. Spellchecking is turned off (no red squiggly lines -- I hate those!)
3. The TAB key will insert 2 spaces, instead of moving focus
4. When you press enter, it will add leading spaces to match the leading spaces on the prior line
...and that's all it does.
@deanebarker
deanebarker / QueryValue.cs
Last active January 3, 2022 17:54
An example of a custom FluidValue to enable fluent(-ish) query specification
// Example:
// {% assign people = query.sortby['lastname'].direction['desc'].limit[2] %}
public class QueryValue : FluidValue
{
// This is only needed for testing
// In production, the query would be of some external resource
private IEnumerable<Person> _items;
// This is an anonymous method that handles the index
@deanebarker
deanebarker / XmlValue.cs
Last active October 11, 2021 18:50
An custom value type for Fluid that allows for easy access of XML element content
public class XmlValue : FluidValue
{
// The accessor to retrieve the text content of an element
public static string TextAccessor { get; set; } = "_text";
// The accessor to retrieve a dictionary of attributes of an element
public static string AttributesAccessor { get; set; } = "_attrs";
// The accessor to retrieve an array of the children of an element
public static string ChildrenAccessor { get; set; } = "_children";
@deanebarker
deanebarker / FluidFakeFileProvider.cs
Last active October 9, 2021 13:59
The minimum (I think) you need to do to mock the FileProvider for the Fluid templating engine
namespace DeaneBarker
{
public class FakeFileProvider : IFileProvider
{
// I don't think Fluid ever calls these (fingers crossed)
public IDirectoryContents GetDirectoryContents(string subpath) { throw new NotImplementedException(); }
public IChangeToken Watch(string filter) { throw new NotImplementedException(); }
public IFileInfo GetFileInfo(string path)
{