Skip to content

Instantly share code, notes, and snippets.

@teramako
Last active September 28, 2024 15:29
Show Gist options
  • Save teramako/47518fb914af0e099043925ce63728bc to your computer and use it in GitHub Desktop.
Save teramako/47518fb914af0e099043925ce63728bc to your computer and use it in GitHub Desktop.
For PowerShell 7.

https://github.com/takeokunn/awesome-yasunori のREAME.mdをパースして出力。

Parameters

-All

パース結果のすべてを出力する。

$ ./YasunoriSay.ps1 -All

title        : YST
date         : 2024-09-28T00:00:00.0000000+09:00
contents     : YST: yasunori study time (yasunoriがJST 22:00 ~ 24:00 にLeetCodeに取り組む時間)
cite         : vim-jp #times-yasunori
by           : ryoppippi
inspired     :
inspired_url :
memo         : - memo
                 - 2024-09-28 Sat 時点、yasunoriはLeetCodeをやらなければならない


...

JSONに変換可能

$ ./YasunoriSay.ps1 -All | ConvertTo-Json
[
  {
    "title": "YST",
    "date": "2024-09-28T00:00:00.0000000+09:00",
    "contents": "YST: yasunori study time (yasunoriがJST 22:00 ~ 24:00 にLeetCodeに取り組む時間)",
    "cite": "vim-jp #times-yasunori",
    "by": "ryoppippi",
    "inspired": null,
    "inspired_url": null,
    "memo": "- memo\n  - 2024-09-28 Sat 時点、yasunoriはLeetCodeをやらなければならない\n"
  },
  {
    ...
  }
]

-Cow

ランダムに一つ選んだ結果を cowsay コマンドに渡す

$ ./YasunoriSay.ps1 -Cow
 _______________________________________________________________________
/ キラ・yaunori                                                         \
|                                                                       |
| キラ・yaunori「無茶苦茶だ!こんなOSでこれだけの機体を動かそうなんて! |
| 」 マリュー「まだ,終わってないのよ」                                 |
| キラ・yasuori「そんなこともあろうかとここに,NixOSのインストーラが    |
| 入ったUSBメモリーが」                                                 |
|                                                                       |
\ ―― by rkarsnk                                                         /
 -----------------------------------------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

None

ランダムに一つ選んだ結果を主力

using namespace System.IO;
using namespace Markdig;
using namespace Markdig.Syntax;
param(
[Parameter()]
[switch] $All,
[Parameter()]
[switch] $Cow
)
class Yasunori {
[string] $title;
[string] $date
[string] $contents;
[string] $cite;
[string] $by;
[string] $inspired;
[string] $inspired_url;
[string] $memo;
}
$md = Invoke-WebRequest https://raw.githubusercontent.com/takeokunn/awesome-yasunori/refs/heads/main/README.md |
Select-Object -ExpandProperty Content
# [string]$md = [File]::ReadAllText((Get-Item -Path ./README.md))
$list = [Markdown]::Parse($md) |
ForEach-Object -Process {
$t = $_
if ($t -is [HeadingBlock] -and $t.Level -eq 3) {
$str = [Markdown]::ToPlainText($md.Substring($t.Span.Start, $t.Span.Length))
if ($str -match "^(.*?) \((.*)\s+\w+\)") {
if ($null -ne $item) {
Write-Output $item
}
$item = [Yasunori]::new();
$item.title = $Matches[1]
$item.date = (
[datetime]::SpecifyKind([datetime]$Matches[2], [System.DateTimeKind]::Local)
).ToString("o")
}
return
}
if ($null -eq $item) { return; }
if ($t -is [ParagraphBlock]) {
$str = [Markdown]::ToPlainText($md.Substring($t.Span.Start, $t.Span.Length))
switch -Regex ($str) {
"inspired by (.*)" {
$item.inspired = $Matches[1]
$link = $t.Inline | Where-Object { $_ -is [Inlines.LinkInline] }
if ($link.Count -gt 0) {
$item.inspired_url = $link[0].Url
}
break
}
"(.*?) by (.*)" {
$item.cite = $Matches[1]
$item.by = $Matches[2]
break
}
}
} elseif ($t -is [FencedCodeBlock]) {
$item.Contents = ($t.Lines -join "`n").Trim()
} elseif ($t -is [ListBlock]) {
$item.memo = $md.Substring($t.Span.Start, $t.Span.Length)
}
} -End {
Write-Output $item
}
if ($All) {
Write-Output $list
return
}
$index = [int](Get-Random -Maximum $list.Count)
$item = $list[$index]
if ($Cow) {
(
("{0}" -f $item.Title),
"",
$item.Contents,
"",
$(if ($item.inspired) { "Inspired by {0}" -f $item.inspired }),
("―― by {0}" -f $item.By)
) |
cowsay
} else {
("# {0}" -f $item.Title) |
ConvertFrom-Markdown -AsVT100EncodedString |
Select-Object -ExpandProperty VT100EncodedString
$item.Contents
""
if ($item.inspired) {
("Inspired by [{0}]({1})" -f $item.inspired,$item.inspired_url) |
ConvertFrom-Markdown -AsVT100EncodedString |
Select-Object -ExpandProperty VT100EncodedString
}
("―― by {0}" -f $item.By)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment