Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add automated formatting
- Loading branch information
Showing
5 changed files
with
4,870 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| const util = require('util'); | ||
| const exec = util.promisify(require('child_process').exec); | ||
| const fs = require('fs').promises | ||
|
|
||
|
|
||
| const REPLACES = [ | ||
| [/([^/])\bjavascript/gi, '$1JavaScript'], // stylise javascript to JavaScript | ||
| [/\bML ops/gi, 'MLOps'], | ||
| [/\bdev ops/gi, 'DevOps'], | ||
| [/\bclick[ -]ops/g, 'ClickOps'], | ||
|
|
||
| [/\[00:(\d{2}:\d{2}).\d{2}\]/g, '\[$1]'], // Remove leading 00: and fractions in timestamps | ||
| [/\[(\d{2}:\d{2}).\d{2}\]/g, '\[$1]'], // Remove fractions in timestamps | ||
| [/\\`(.*?)\\`/g, '`$1`'], // Remove escaping backslashes before backticks | ||
| ] | ||
|
|
||
|
|
||
| // Perform multiple rounds of replaces on a string | ||
| function applyReplaces(text) { | ||
| return REPLACES.reduce((prev, [a, b]) => prev.replace(a, b), text) | ||
| } | ||
|
|
||
| // Returns an array of the filenames of all the episodes | ||
| async function getTranscripts() { | ||
| const { stdout } = await exec('find **/*.md') | ||
| return stdout | ||
| .split('\n') // make an array | ||
| .filter(x => x.endsWith('.md')) // remove blanks | ||
| } | ||
|
|
||
| // Read a textfile to a string, pass the string through processor, and write the results out to a file | ||
| async function processFile(filename, processor, newname = '') { | ||
| const inText = await fs.readFile(filename).then(buffer => buffer.toString()) | ||
| const outText = processor(inText) | ||
| if (outText != inText) { | ||
| await fs.writeFile(newname || filename, outText) | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| async function main() { | ||
| const transcripts = await getTranscripts() | ||
| transcripts.forEach(filename => processFile(filename, applyReplaces)) | ||
| } | ||
|
|
||
|
|
||
|
|
||
| if (require.main === module) { | ||
| main(); | ||
| } | ||
|
|
||
| module.exports = { | ||
| getTranscripts, processFile, applyReplaces | ||
| } |
This file contains 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| const fs = require('fs').promises | ||
| const os = require('os') | ||
| const path = require('path') | ||
|
|
||
| const {applyReplaces, getTranscripts, processFile}= require('./format') | ||
|
|
||
|
|
||
| test('applyReplaces works as expected', () => { | ||
| expect(applyReplaces('I love javascript')).toBe('I love JavaScript') | ||
| expect(applyReplaces('r/javascript')).toBe('r/javascript') | ||
|
|
||
| expect(applyReplaces('\[00:04:06.27\]')).toBe('\[04:06\]') | ||
| expect(applyReplaces('\[00:10:17.78\]')).toBe('\[10:17\]') | ||
| expect(applyReplaces('\[03:43.78\]')).toBe('\[03:43\]') | ||
|
|
||
| expect(applyReplaces("\\`console.log($TOKEN)\\`")).toBe('`console.log($TOKEN)`') | ||
| expect(applyReplaces("The difference between \\`foo\\` and \\`bar\\`")).toBe('The difference between `foo` and `bar`') | ||
|
|
||
| expect(applyReplaces("ML ops is ML ops, but OCAML ops is not")).toBe('MLOps is MLOps, but OCAML ops is not') | ||
| }) | ||
|
|
||
|
|
||
| test('getTranscripts works as expected', async () => { | ||
| const transcripts = await getTranscripts() | ||
| // TODO: What are good ways to test this function? | ||
| }) | ||
|
|
||
|
|
||
| test('processFile works as expected', async () => { | ||
| const location = await fs.mkdtemp(path.join(os.tmpdir(), 'format-')) | ||
| const filename = path.join(location, 'temp.txt') | ||
| const text = "The Quick Brown Fox Jumps Over the Lazy dog.\nI wander lonely as a cloud.\n" | ||
| await fs.writeFile(filename, text) | ||
| await processFile(filename, x => x.toLowerCase()) | ||
| const outText = await fs.readFile(filename).then(x => x.toString()) | ||
| expect(outText).toBe(text.toLowerCase()) | ||
| await fs.rm(location, {recursive: true}) | ||
| }) |
Oops, something went wrong.