-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathGet-UnicodeByName.ps1
101 lines (85 loc) · 3.27 KB
/
Get-UnicodeByName.ps1
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<#
.SYNOPSIS
Returns characters based on Unicode code point name, GitHub short code, or HTML entity.
.INPUTS
System.String of a character name.
.OUTPUTS
System.String of the character(s) referenced by name.
.FUNCTIONALITY
Unicode
.LINK
https://www.unicode.org/Public/UCD/latest/ucd/NameAliases.txt
.LINK
https://html.spec.whatwg.org/multipage/named-characters.html
.EXAMPLE
Get-UnicodeByName.ps1 hyphen-minus
-
.EXAMPLE
Get-UnicodeByName.ps1 slash
/
.EXAMPLE
Get-UnicodeByName.ps1 :zero:
[0]
.EXAMPLE
Get-UnicodeByName.ps1 '&'
&
.EXAMPLE
Get-UnicodeByName.ps1 BEL
(beeps)
#>
#Requires -Version 3
[CmdletBinding()][OutputType([string])] Param(
# The name or alias of a Unicode character.
[Parameter(ParameterSetName='Name',Position=0,Mandatory=$true,ValueFromPipeline=$true)][string] $Name,
<#
Appends a U+FE0F VARIATION SELECTOR-16 suffix to the character, which suggests an emoji presentation
for characters that support both a simple text presentation as well as a color emoji-style one.
#>
[switch] $AsEmoji,
# Update the character name database.
[Parameter(ParameterSetName='Update')][switch] $Update
)
Begin
{
$basename = Join-Path -Path $PSScriptRoot -ChildPath data -AdditionalChildPath UnicodeByName
$cc = ConvertFrom-StringData (Get-Content "$basename.cc.txt" -Raw)
$codepoint = ConvertFrom-StringData (Get-Content "$basename.txt" -Raw)
$html = Get-Content "$basename.html.json" -Raw |ConvertFrom-Json -AsHashtable
$github = ConvertFrom-StringData (Get-Content "$basename.github.txt" -Raw)
filter ConvertTo-Char([Parameter(ValueFromPipeline)][string] $Value)
{
$result = (($Value -split '\W+') |
ForEach-Object {[char]::ConvertFromUtf32([convert]::ToInt32($_,16))}) -join ''
return $AsEmoji ? $result + ([char]0xFE0F) : $result
}
}
Process
{
if($Update)
{
$conflictingOldNames = '0007','01B7','0292','0404','0406','0454','0456','10D0','10D1','10D2','10D3','10D4',
'10D5','10D6','10D7','10D8','10D9','10DA','10DB','10DC','10DD','10DE','10DF','10E0','10E1','10E2','10E3',
'10E4','10E5','10E6','10E7','10E8','10E9','10EA','10EB','10EC','10ED','10EE','10EF','10F0','10F1','10F2',
'10F3','10F4','10F5','2016','314A','314B','314D','3209','320A','320C','3269','326A','326C','33B7','FFBA',
'FFBB','FFBD'
Get-UnicodeData.ps1 |
ForEach-Object {
if($_.OldName -and $_.Value -notin $conflictingOldNames){$_.OldName+'='+$_.Value}
if($_.Name -ne '<control>'){$_.Name+'='+$_.Value}
} |Out-File "$basename.txt" -Encoding utf8
Invoke-WebRequest https://html.spec.whatwg.org/entities.json -OutFile "$basename.html.json"
(Invoke-RestMethod https://api.github.com/emojis).PSObject.Properties |
Where-Object {$_.Value -notlike "*/$($_.Name).png[?]v8"} |
ForEach-Object {':'+$_.Name+':='+(((([uri]$_.Value).Segments[-1]) -replace '\.png\z').ToUpper() -replace '-',',')} |
Out-File "$basename.github.txt" -Encoding utf8
Write-Information 'Updated.'
return
}
else
{
if($cc.ContainsKey($Name)) {return $cc[$Name] |ConvertTo-Char}
elseif($github.ContainsKey($Name)) {return $github[$Name] |ConvertTo-Char}
elseif($html.ContainsKey($Name)) {return ($html[$Name].characters -join '') + ($AsEmoji ? [char]0xFE0F : '')}
else {return $codepoint[$Name] |ConvertTo-Char}
}
}