Skip to content

Instantly share code, notes, and snippets.

@algal
Last active June 22, 2020 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save algal/07c2f1325171bfcdd9ee3b61289e71d9 to your computer and use it in GitHub Desktop.
Save algal/07c2f1325171bfcdd9ee3b61289e71d9 to your computer and use it in GitHub Desktop.
Deno and Swift scripts to reverse H2 headers in text
#!/usr/bin/swift
/*
Reads stdin, requiring UTF8-encoded text.
Parses it as initial text followed by a sequence of H2 markdown blocks.
Prints to stdout the initial text plus the H2 blocks in reversed order.
*/
import Foundation
let s = AnyIterator { readLine() }.joined(separator: "\n")
let splitter = "\n## "
let ss = s.components(separatedBy:splitter)
let out = ss[0] + ss[1...].reversed().map({ splitter + $0 }).joined(separator:"")
print(out)
#!/usr/local/bin/deno run -q
/*
Reads stdin, requiring UTF8-encoded text.
Parses it as initial text followed by a sequence of H2 markdown blocks.
Prints to stdout the initial text plus the H2 blocks in reversed order.
*/
const s = new TextDecoder().decode(await Deno.readAll(Deno.stdin));
const splitter = "\n## ";
const [first, ...rest] = s.split(splitter);
const out = first + rest.reverse().map( x => splitter + x ).join("");
console.log(out);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment