-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
181 lines (155 loc) · 4.49 KB
/
Copy pathexample.js
File metadata and controls
181 lines (155 loc) · 4.49 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* Example usage and demonstration
* Run with: node example.js
* This generates a sample report without requiring Last.fm API credentials
*/
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
// Sample data
const sampleTopArtists = [
{ rank: 1, name: 'The Beatles', playcount: 42 },
{ rank: 2, name: 'Pink Floyd', playcount: 38 },
{ rank: 3, name: 'Led Zeppelin', playcount: 35 },
{ rank: 4, name: 'Queen', playcount: 32 },
{ rank: 5, name: 'David Bowie', playcount: 28 },
];
const sampleTopTracks = [
{ rank: 1, name: 'Bohemian Rhapsody', artist: 'Queen', playcount: 15 },
{ rank: 2, name: 'Comfortably Numb', artist: 'Pink Floyd', playcount: 12 },
{ rank: 3, name: 'Imagine', artist: 'John Lennon', playcount: 11 },
{ rank: 4, name: 'Stairway to Heaven', artist: 'Led Zeppelin', playcount: 10 },
{ rank: 5, name: 'Space Oddity', artist: 'David Bowie', playcount: 9 },
];
function generateTypstTemplate(topArtists, topTracks) {
console.log('📝 Generating Typst template from sample data...');
const artistsList = topArtists
.map(
(artist) =>
`[${"#"}strong[\`${artist.rank}.\`]] ${artist.name} _${artist.playcount} plays_`
)
.join('\n');
const tracksList = topTracks
.map(
(track) =>
`[${"#"}strong[\`${track.rank}.\`]] ${track.name} _by ${track.artist}_ \ _${track.playcount} plays_`
)
.join('\n');
const template = `#set page(width: 1080pt, height: 1920pt, margin: 0pt, background: rgb("#1a1a1a"))
#set text(font: "Inter", size: 28pt, fill: white)
#let title-style(content) = {
set text(size: 48pt, weight: "bold", fill: rgb("#00d9ff"))
content
}
#let section-title(content) = {
set text(size: 36pt, weight: "bold", fill: rgb("#00d9ff"))
v(24pt)
content
v(16pt)
}
#let item-text(content) = {
set text(size: 24pt, fill: white)
content
}
#grid(
columns: (1fr,),
gutter: 20pt,
// Header
{
set align(center)
v(40pt)
title-style[🎵 Story.report 🎵]
v(8pt)
set text(size: 18pt, fill: rgb("#aaaaaa"))
[Last 30 days]
v(32pt)
},
// Content section
{
set text(size: 24pt)
// Top Artists Section
{
set align(center)
section-title[TOP 5 ARTISTS]
}
{
set align(left)
set text(size: 22pt, fill: rgb("#ffffff"))
box(width: 100%, inset: 20pt, [
${artistsList}
])
}
// Top Tracks Section
{
set align(center)
section-title[TOP 5 TRACKS]
}
{
set align(left)
set text(size: 20pt, fill: rgb("#ffffff"))
box(width: 100%, inset: 20pt, [
${tracksList}
])
}
},
// Footer
{
set align(center)
v(1fr)
set text(size: 16pt, fill: rgb("#666666"))
[Generated by Story.report]
v(20pt)
}
)`;
return template;
}
async function compileTypstToPng(typstFilePath, outputImagePath) {
try {
console.log('🔨 Compiling Typst to PNG...');
// Check if typst CLI is available
try {
execSync('typst --version', { stdio: 'pipe' });
} catch {
console.error(
'❌ Error: typst CLI not found. Please install Typst first.'
);
console.error(' Visit: https://github.com/typst/typst/releases');
process.exit(1);
}
// Compile Typst to PNG
execSync(`typst compile "${typstFilePath}" "${outputImagePath}"`, {
stdio: 'inherit',
});
console.log(`✅ PNG image generated: ${outputImagePath}`);
} catch (error) {
console.error('❌ Error compiling Typst:', error.message);
throw error;
}
}
async function main() {
try {
console.log('\n🚀 Generating Story.report (Sample)\n');
// Generate Typst template from sample data
const typstTemplate = generateTypstTemplate(
sampleTopArtists,
sampleTopTracks
);
// Save Typst template to file
const typstFilePath = path.join(process.cwd(), 'report-example.typ');
fs.writeFileSync(typstFilePath, typstTemplate);
console.log(`✅ Typst template saved: ${typstFilePath}`);
// Compile to PNG
const outputImagePath = path.join(process.cwd(), 'report-example.png');
await compileTypstToPng(typstFilePath, outputImagePath);
console.log('\n✨ Done! Sample report is ready!');
console.log(`📸 Image saved at: ${outputImagePath}`);
console.log(
'\n📖 To generate your real report with Last.fm data, run: npm start'
);
console.log('\n');
} catch (error) {
console.error('\n❌ Fatal error:', error.message);
process.exit(1);
}
}
main();