글을 쓰다보면 메시지가 온다거나, 또는 문득 떠오른 궁금증에 이끌려 다른 곳에 집중해 버리거나, Notification으로 온 유혹때문에 길게 작성하지 못하는 경우가 종종 있습니다. 그러다가 제정신을 차리고 다시 원래의 상태로 돌아오면 문득 궁금해집니다.
궁금한 점
- 나는 몇 포스트나 썼지?
- 공개포스트와 draft는 몇 개야?
- 전체 라인의 수는 몇 라인이야
- 문자로 따지면 전체 문자수는 몇 문자야
- 오늘은 어제보다 얼마나 쓴거지
- 전체 통계치를 이쁘게 볼 수 없을까
Get-HugoPost
통계치를 구하기 위해서, 전체 블로그 포스트 객체를 구할 필요가 있었습니다.
다음 스크립트는 특정된 디렉토리이하의 md파일을 객체를 구하는 내용입니다. 약간의 FrontMatter안의 내용을 구하는 로직도 넣었는데, 저는 yaml 포맷으로 작성하고 있기 때문에 ConvertFrom-Yaml 을 이용하였습니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
| Function Get-HugoPost {
[CmdletBinding()]
param(
[string] $SiteName = 'blog'
)
Set-Location $env:userprofile\$SiteName\content\ko
$files = Get-ChildItem -Path $env:userprofile\$SiteName\content\ko -Include *.md -Recurse
$files.Count
$files | ft -Property DirectoryName, Name
$posts = [System.Collections.ArrayList]@()
Foreach($f in $files) {
$post = @{}
$post['DirectoryName'] = $f.DirectoryName
$post['Name'] = $f.Name
$content = Get-Content $f -Encoding UTF8
$frontmatter = ""
$delimeter = $content[0]
if ($delimeter -notin @("---", "+++")) {
Write-Host "no delimeter found."
Write-Host $post.Name
Continue
}
$index = 1
$IN_FONTMATTER = $true
$frontmatter = ""
$post['Content'] = ""
While($index -lt $content.Length) {
$line = $content[$index]
if ($IN_FONTMATTER) {
$frontmatter += $line + "`n"
} else {
$post['Content'] += $line + "`n"
}
if ($line -eq $delimeter) {
# add frontmatter
$IN_FONTMATTER = $false
try {
$post['FrontMatter'] = (ConvertFrom-Yaml $frontmatter)
} catch {
Write-Host $_
Write-Host $post.DirectoryName
Write-Host $post.Name
Write-Host $frontmatter
}
}
$index++
}
[void]$posts.Add($post)
}
return $posts
}
|
나는 몇 포스트나 썼지?
1
2
| $posts = get-hugopost
$posts.Count
|
그중에 공개한 포스트는 몇 개고, draft는 몇 개야?
1
2
| "public : $( ($posts | where-object { $_.FrontMatter.draft }).Count )"
"draft : $( ($posts | where-object {! $_.FrontMatter.draft }).Count )"
|
전체 라인의 수는 몇 라인이야
1
2
3
4
5
| $total_line = 0
foreach ($p in $posts) {
$total_line += $p.Content.Split("`n").Count
}
$total_line
|
문자로 따지면 전체 문자수는 몇 문자야
참고로 frontmatter에 draft의 키가 없으면 true로 처리됩니다.
1
2
3
4
5
| $total_char = 0
foreach ($p in $posts) {
$total_char += $p.Content.ToCharArray().Count
}
$total_char
|
오늘은 좀 썼는데, 어제보다 얼마나 쓴거지
$posts.FrontMatter.Date | sort
전체 리포트를 이쁘게 볼 수 없을까
Show-HugoReport
라고 하는 함수로 만들어 보았습니다.
Format-Table을 이용했고, 라인수를 기준으로 소팅하였습니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| Function Show-HugoReport {
$posts = get-hugopost
$report = [System.Collections.ArrayList]@()
foreach ($p in $posts) {
$total_line += $p.Content.Split("`n").Count
$r = [PSCustomObject]@{
Date = $p.FrontMatter.Date
LineCount = $p.Content.Split("`n").Count
CharCount = $p.Content.ToCharArray().Count
FileCode = $p.Name.SubString(0, 4)
Draft = $p.FrontMatter.Draft
}
[void]$report.Add($r)
}
$report |where {[char]::IsDigit($_.FileCode.SubString(1,1))} | sort -Property LineCount -Descending | ft
}
|
결과는 다음과 같습니다.
하루에도 여러 번 호출하고 있습니다. 또, 추이가 어떻게 되는 지도 볼 수 있으면 좋겠다는 생각이 듭니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
| PS C:\Users\Administrator> Show-HugoReport
Date LineCount CharCount FileCode Draft
---- --------- --------- -------- -----
2020/07/18 23:34:34 187 4503 h040 False
2020/07/19 0:20:16 169 6054 h042 True
2020/07/13 10:40:01 135 3172 h036 True
2020/07/09 23:11:15 107 2554 h001 False
2020/07/10 22:45:54 104 2199 h006 False
2020/07/19 17:51:13 101 4331 p007 False
2020/07/10 21:39:35 100 2083 h002 False
2020/07/11 21:20:24 80 4986 d003 True
2020/07/10 22:23:41 69 2142 h004 False
|
레퍼런스