Skip to content

Commit 178df8f

Browse files
committed
add moar examples
1 parent 0e0d094 commit 178df8f

File tree

8 files changed

+689
-58
lines changed

8 files changed

+689
-58
lines changed

docs/img/thanks.png

99.4 KB
Loading

docs/unicode.md

Lines changed: 558 additions & 24 deletions
Large diffs are not rendered by default.

examples/boost_utf_conv.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ using boost::locale::conv::utf_to_utf;
55
using boost::locale::conv::from_utf;
66

77
int main() {
8+
setlocale(LC_ALL, "C.utf-8");
89
std::string s = "你好";
910
// UTF-8 转 UTF-32:
1011
std::u32string s32 = utf_to_utf<char32_t>(s);

examples/date_time.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <ctime>
2+
#include <iomanip>
3+
#include <iostream>
4+
#include <locale>
5+
6+
int main() {
7+
time_t t = time(NULL);
8+
tm *tm = localtime(&t);
9+
10+
auto locale_zh = std::locale("zh_CN.UTF-8");
11+
std::cout.imbue(locale_zh);
12+
std::cout << std::put_time(tm, "%c") << '\n';
13+
14+
auto locale_en = std::locale("en_US.UTF-8");
15+
std::cout.imbue(locale_en);
16+
std::cout << std::put_time(tm, "%c") << '\n';
17+
}

examples/locale_wctype.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <fmt/format.h>
2+
#include <boost/locale.hpp>
3+
4+
int main() {
5+
fmt::println("默认: {}", iswpunct(L''));
6+
7+
setlocale(LC_ALL, "C");
8+
fmt::println("C: {}", iswpunct(L''));
9+
10+
setlocale(LC_ALL, "C.UTF-8");
11+
fmt::println("C.UTF-8: {}", iswpunct(L''));
12+
13+
setlocale(LC_ALL, "zh_CN.UTF-8");
14+
fmt::println("zh_CN.UTF-8: {}", iswpunct(L''));
15+
16+
setlocale(LC_ALL, "zh_CN.GBK");
17+
fmt::println("zh_CN.GBK: {}", iswpunct(L''));
18+
19+
setlocale(LC_ALL, "en_US.ISO-8859-1");
20+
fmt::println("en_US.ISO-8859-1: {}", iswpunct(L''));
21+
22+
setlocale(LC_ALL, "POSIX");
23+
fmt::println("POSIX: {}", iswpunct(L''));
24+
25+
setlocale(LC_ALL, "en_CA.ISO-8859-1");
26+
fmt::println("en_CA: {}", iswalpha(L'é'));
27+
setlocale(LC_ALL, "fr_CA.ISO-8859-1");
28+
fmt::println("fr_CA: {}", iswalpha(L'é'));
29+
30+
return 0;
31+
}

examples/wcout.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <iostream>
2+
3+
int main() {
4+
setlocale(LC_ALL, "");
5+
std::wcout << L"我是 wcout!" << L'\n';
6+
std::cout << "我是 cout!" << '\n';
7+
return 0;
8+
}

misc/afdian.py

Lines changed: 73 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,26 @@
77
from PIL import Image, ImageDraw, ImageFont
88
from io import BytesIO
99

10-
class User(namedtuple('User', ['name', 'avatar', 'all_sum_amount'])):
10+
class User(namedtuple('User', ['name', 'avatar', 'all_sum_amount', 'remark'])):
11+
pass
12+
13+
class Order(namedtuple('Order', ['remark'])):
1114
pass
1215

1316
manual_sponsors = [
14-
User('等疾风', 'https://i0.hdslb.com/bfs/face/b658b5ca52f41e53321d04f978be6784ca6f8687.jpg', '1000.00'),
15-
User('只喝白开水', 'https://i2.hdslb.com/bfs/face/821b88a24c1319d1fb51b3854884e2f829855c75.jpg', '100.00'),
16-
User('包乾', '', '26.90'),
17-
User('柿柿如意', '', '20.00'),
18-
User('Starry', '', '100.00'),
19-
User('阿哲', '', '100.00'),
20-
User('Eureka', '', '20.00'),
21-
User('孙斌', '', '200.00'),
22-
User('nullptr', 'https://i0.hdslb.com/bfs/face/effa1ec9bb0f5d09ed415da75129aca9d16092ac.jpg', '23.30'),
23-
User('Fred Song', '', '25.00'),
24-
User('**振', '', '20.00'),
17+
User('等疾风', 'https://i0.hdslb.com/bfs/face/b658b5ca52f41e53321d04f978be6784ca6f8687.jpg', '1000.00', '小彭老师加油,希望给个赞助位'),
18+
User('只喝白开水', 'https://i2.hdslb.com/bfs/face/821b88a24c1319d1fb51b3854884e2f829855c75.jpg', '100.00', '确实快了 30 倍[赞]'),
19+
User('包乾', '', '26.90', ''),
20+
User('柿柿如意', '', '20.00', '请小彭老师喝奶茶'),
21+
User('Starry', '', '100.00', '小彭老师加油!'),
22+
User('阿哲', '', '100.00', '从小彭老师视频里学到太多'),
23+
User('Eureka', '', '20.00', '希望我能赚的多一点,之后发电也就多一点'),
24+
User('孙斌', '', '200.00', '06班孙斌,望越来越好'),
25+
User('nullptr', 'https://i0.hdslb.com/bfs/face/effa1ec9bb0f5d09ed415da75129aca9d16092ac.jpg', '23.30', '小彭老师千古,伟大无需多言'),
26+
User('Fred Song', '', '25.00', '小彭老师早点休息'),
27+
User('**振', '', '20.00', ''),
28+
User('**伟', '', '20.00', ''),
29+
User('**枫', '', '26.90', ''),
2530
]
2631

2732
def afd_query(which, **params):
@@ -39,34 +44,58 @@ def afd_query(which, **params):
3944
assert res['ec'] == 200, res
4045
return res['data']
4146

42-
def afd_query_sponsors():
47+
def afd_paged_query(which):
4348
i = 1
44-
res = []
4549
while True:
46-
page = afd_query('query-sponsor', page=i)
50+
page = afd_query(which, page=i)
4751
n = page['total_page']
48-
for user in page['list']:
49-
res.append(User(user['user']['name'], user['user']['avatar'], user['all_sum_amount']))
52+
for item in page['list']:
53+
yield item
5054
if i >= n:
5155
break
5256
i += 1
57+
58+
def afd_query_orders():
59+
order_lut = {}
60+
for order in afd_paged_query('query-order'):
61+
print(f'{order['user_id']}: {order['remark']}')
62+
order_lut[order['user_id']] = Order(order['remark'])
63+
return order_lut
64+
65+
def afd_query_sponsors():
66+
res = []
67+
order_lut = afd_query_orders()
68+
for user in afd_paged_query('query-sponsor'):
69+
user_id = user['user']['user_id']
70+
last_order = order_lut.get(user_id, None)
71+
if last_order:
72+
remark = last_order.remark
73+
else:
74+
remark = ''
75+
user_obj = User(user['user']['name'],
76+
user['user']['avatar'],
77+
user['all_sum_amount'],
78+
remark)
79+
res.append(user_obj)
5380
return res
5481

5582
def afd_gen_thank_list():
5683
sponsors = afd_query_sponsors()
5784
sponsors += manual_sponsors
5885
max_x = 30
5986
max_y = 30
60-
limit_y = 600
87+
stride_x = 450
88+
stride_y = 120
89+
limit_y = 720
6190
max_max_y = max_y
6291
for user in sponsors:
63-
max_y += 100
92+
max_y += stride_y
6493
if max_y + 10 >= limit_y:
6594
max_max_y = max(max_max_y, max_y)
6695
max_y = 30
67-
max_x += 400
96+
max_x += stride_x
6897
max_max_y = max(max_max_y, max_y)
69-
max_max_x = max_x + 400
98+
max_max_x = max_x + stride_x
7099
max_y += 10
71100
img = Image.new('RGB', (max_max_x, max_max_y), color='#19242e')
72101
x = 30
@@ -75,27 +104,38 @@ def afd_gen_thank_list():
75104
for user in sponsors:
76105
draw = ImageDraw.Draw(img)
77106
font = ImageFont.truetype('/usr/share/fonts/noto-cjk/NotoSansCJK-Medium.ttc', size=20)
107+
font_small = ImageFont.truetype('/usr/share/fonts/noto-cjk/NotoSerifCJK-Medium.ttc', size=13)
78108
if user.avatar:
79109
avatar = Image.open(BytesIO(requests.get(user.avatar).content))
80-
elif os.path.exists(f'/home/bate/下载/wx-{user.name.replace('/', '|')}.png'):
81-
avatar = Image.open(f'/home/bate/下载/wx-{user.name.replace('/', '|')}.png')
82110
else:
83-
this_dir = os.path.dirname(os.path.abspath(__file__))
84-
avatar = Image.open(os.path.join(this_dir, '../docs/img/favicon.ico'))
85-
avatar = avatar.resize((80, 80))
111+
wx_path = f'/home/bate/下载/wx-{user.name.replace('/', '|')}.png'
112+
if os.path.exists(wx_path):
113+
avatar = Image.open(wx_path)
114+
else:
115+
this_dir = os.path.dirname(os.path.abspath(__file__))
116+
avatar = Image.open(os.path.join(this_dir, '../docs/img/favicon.ico'))
117+
avatar = avatar.resize((90, 90))
86118
img.paste(avatar, (x, y))
87119
draw.text((x + 100, y), f'{user.name}', fill='white', font=font)
88120
draw.text((x + 100, y + 30), f'¥{user.all_sum_amount}', fill='#aaaaaa', font=font)
89-
print(f'{user.name}{user.all_sum_amount}')
121+
remark = user.remark
122+
if remark:
123+
remark = remark.rstrip('。').replace('.。', ',')
124+
draw.text((x + 100, y + 60), f'{remark}', fill='#779977', font=font_small)
125+
print(f'{user.name}{user.all_sum_amount} {remark}')
90126
total += float(user.all_sum_amount)
91-
y += 100
127+
y += stride_y
92128
if y + 10 >= limit_y:
93129
y = 30
94-
x += 400
130+
x += stride_x
95131
print(total)
96132
return img
97133

98-
img = afd_gen_thank_list()
99-
file = 'docs/img/thanks.png'
100-
img.save(file)
101-
img.show()
134+
def main():
135+
img = afd_gen_thank_list()
136+
file = 'docs/img/thanks.png'
137+
img.save(file)
138+
os.system(f'display {file}')
139+
140+
if __name__ == '__main__':
141+
main()

misc/export_to_pdf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ console.log('Saving', url, 'to', pdfPath);
1313
// totalPages – total pages in the document
1414
headerHtml = `
1515
<div style="font-size: 10px; padding-right: 1em; text-align: right; width: 100%;">
16-
<span>${title}</span> <span class="pageNumber"></span> / <span class="totalPages"></span>
16+
<span>${title}</span> <span class="pageNumber"></span> 页,共 <span class="totalPages"></span>
1717
</div>`;
1818

1919
footerHtml = ` `;

0 commit comments

Comments
 (0)