Skip to content

Commit

Permalink
Action Prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectronicsArchiver committed Jan 21, 2023
1 parent c925516 commit 1f49f60
Show file tree
Hide file tree
Showing 11 changed files with 365 additions and 42 deletions.
32 changes: 31 additions & 1 deletion .github/.News.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
# #
# ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ #
# ┃ ┃ #
# ┃ MarkedDown/News ┃ #
# ┃ Action Settings ┃ #
# ┃ ┃ #
# ┃ 🗞 News sections generated from your input. ┃ #
# ┃ ┃ #
# ┃ https://github.com/MarkedDown/News ┃ #
# ┃ ┃ #
# ┃ ┃ #
# ┃ Configuration ┃ #
# ┠────────────────────────────────────────────────────────────────────────┨ #
# ┃ ┃ #
# ┃ A configuration describes from where data ┃ #
# ┃ should be loaded and where to inject it. ┃ #
# ┃ ┃ #
# ┃ ┃ #
# ┃ Example ┃ #
# ┠────────────────────────────────────────────────────────────────────────┨ #
# ┃ ┃ #
# ┃ - Inject : Example/README.md ┃ #
# ┃ Input : Example/News.yaml ┃ #
# ┃ ┃ #
# ┃ ┃ #
# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ #
# #


- Inject : Example/README.md
Input : Example/News.yaml

test : testing
48 changes: 48 additions & 0 deletions Example/News.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

#
# News Articles
# =============
#
# Structure:
# ----------
#
# - title : <Title>
#
# lines : |
# <Line>
# <Line>
#
# links :
# <Link Name> : <Url>
#
# Warning:
# --------
#
# Lines shouldn't be longer than 50 characters.
#
##########################################################| Stop Here
#


- title : 🔬 Testing

lines : |
This is a test message!
- title : 🔬 Testing

lines : |
This is a test message!
- title : 🔬 Testing

lines : |
This is a test message!
- title : 🔬 Testing

lines : |
This is a test message!
10 changes: 10 additions & 0 deletions Example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

<!---------------------------------- 🗞 News ---------------------------------->

<pre align = center>

<kbd align = left> <br>   <b>🔬 Testing</b><br>   <br>   This is a test message!<br>   <br>                                                                                </kbd>     <kbd align = left> <br>   <b>🔬 Testing</b><br>   <br>   This is a test message!<br>   <br>                                                                                </kbd><br><br><kbd align = left> <br>   <b>🔬 Testing</b><br>   <br>   This is a test message!<br>   <br>                                                                                </kbd>     <kbd align = left> <br>   <b>🔬 Testing</b><br>   <br>   This is a test message!<br>   <br>                                                                                </kbd>

</pre>

<!---------------------------------- 🗞 News ---------------------------------->
38 changes: 0 additions & 38 deletions Source/Action.ts

This file was deleted.

33 changes: 33 additions & 0 deletions Source/App.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

import { insertNews } from './Readme.ts'

import configs from './Config.ts'


const
{ writeTextFile , readTextFile } = Deno ,
{ log } = console ;


log(`🗞 Inserting updated News`);


for ( const config of configs ){

log(`
Building Config
Inject : ${ config.Inject }
Input : ${ config.Input }
`)

const readme = await readTextFile(config.Inject);

const updated = await insertNews(readme,config);

await writeTextFile(config.Inject,updated);

}


log(`🗞 Finished news insertion`);

46 changes: 46 additions & 0 deletions Source/Config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

import { parse as parseYAML } from 'YAML'
import { setFailed } from 'Actions'
import { parse } from 'Flags'
import { join } from 'Path'


export interface Config {
Inject : string
Input : string
}

const { readTextFile , args , env } = Deno;


const repository = env
.get('GITHUB_WORKSPACE') as string;


const flags = parse(args);

const config = join(repository,flags.config);



const yaml = await readTextFile(config)
.catch(fail) as string


const configs = <Config []>
parseYAML(yaml)


export default configs



function fail (){

setFailed(`
The given config path cannot be found!
Path : \`${ config }\`
`)

Deno.exit(1);
}
90 changes: 90 additions & 0 deletions Source/Content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

import { loadNews , News } from './News.ts'
import { Config } from './Config.ts'
import { chunk } from 'Chunk'


const { log } = console;


const Fill = '-'.repeat(34);

const Mark = `<!${ Fill } 🗞 News ${ Fill }>`;


const Space = ' ';


const indent = ( line : string ) =>
`${ Space.repeat(3) }${ line }`

const trim = ( line : string ) =>
line.trim();


const link = /(\[[\S\s]+?\])/g




function toPanel ( news : News ){

const { links = {} } = news;

const insertLinks = ( line : string ) =>
line.replaceAll(link,( text ) => {

const name = text
.slice(1,-1);

const url = links[name] ?? '#';

return `<a href = '${ url }'>${ name }</a>`
})


const { title , lines } = news;

const rows = lines
.split('\n')
.map(trim);

rows.unshift(`<b>${ title }</b>`,'');

const text = rows
.map(indent)
.map(insertLinks)
.join('<br>');

return [
`<kbd align = left>${ Space }<br>` ,
text ,
`<br>${ Space.repeat(80) }</kbd>`
].join('')
}


export async function compileNews ( config : Config ){

const articles = await loadNews(config);

log(`🗞 Found ${ articles.length } news articles`)

const panels = articles
.map(toPanel);

const news = chunk(panels,2)
.map(( pair ) => pair.join(Space.repeat(5)))
.join('<br><br>');

const items = [
Mark ,
'<pre align = center>' ,
news ,
'</pre>' ,
Mark
]

return items
.join('\n \n')
}
26 changes: 26 additions & 0 deletions Source/News.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

import { Config } from './Config.ts'
import { parse } from 'YAML'

const { readTextFile } = Deno;


export interface News {

title : News

lines : string

links ?: {
[ alias : string ] : string
}
}


export async function loadNews ( config : Config ){

const yaml = await readTextFile(config.Input);

return <News[]>
parse(yaml) ?? []
}
20 changes: 20 additions & 0 deletions Source/Readme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

import { compileNews } from './Content.ts'
import { Config } from './Config.ts'

const
content = `[\\s\\S]+?` ,
marker = `<!-{2,} *🗞 News *-{2,}>` ,
pattern = `${ marker }(${ content })${ marker }` ;

const outdated =
new RegExp(pattern,'im');


export async function insertNews ( readme : string , config : Config ){

const news = await compileNews(config);

return readme
.replace(outdated,news);
}
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"importMap" : "Imports.json" ,

"tasks" : {
"develop" : "deno run --watch=Source,.github/News.yaml --allow-env=INPUT_CONFIG --allow-read=. --allow-write=. Source/Action.ts --config=\".github/.News.yml\"" ,
"update" : "deno run --allow-env=INPUT_CONFIG --allow-read=. --allow-write=. Source/Action.ts --config=\".github/.News.yml\""
"develop" : "export GITHUB_WORKSPACE=. && deno run --watch=Source,.github/News.yaml --allow-env=GITHUB_WORKSPACE --allow-read=. --allow-write=. Source/App.ts --config=\".github/.News.yml\"" ,
"update" : "export GITHUB_WORKSPACE=. && deno run --allow-env=GITHUB_WORKSPACE --allow-read=. --allow-write=. Source/App.ts --config=\".github/.News.yml\""
}
}
Loading

0 comments on commit 1f49f60

Please sign in to comment.