Last active
June 22, 2020 00:57
-
-
Save algal/07c2f1325171bfcdd9ee3b61289e71d9 to your computer and use it in GitHub Desktop.
Deno and Swift scripts to reverse H2 headers in text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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