1
+ /**
2
+ * Generates CSS from an .atxt file's content.
3
+ * @param {string } atxtContent - The full content of the .atxt file, including the header.
4
+ * @param {string } className - The class name to use in the generated CSS.
5
+ * @returns {string } - The generated CSS string.
6
+ * @throws {Error } - Throws an error if the size in the header is invalid.
7
+ */
8
+ export function generateAsciiArtCss ( atxtContent , className ) {
9
+ // Split the content into lines
10
+ const lines = atxtContent . trim ( ) . split ( '\n' ) ;
11
+
12
+ // Parse the header
13
+ const header = lines [ 0 ] ;
14
+ if ( ! header . startsWith ( '#ascii-art' ) ) {
15
+ throw new Error ( 'Invalid .atxt file: Missing #ascii-art header.' ) ;
16
+ }
17
+
18
+ // Extract the size from the header (e.g., "10x10")
19
+ const sizeMatch = header . match ( / # a s c i i - a r t ( \d + ) x ( \d + ) / ) ;
20
+ if ( ! sizeMatch ) {
21
+ throw new Error ( 'Invalid .atxt file: Missing or invalid size in header.' ) ;
22
+ }
23
+ const [ _ , width , height ] = sizeMatch . map ( Number ) ;
24
+
25
+ // Extract the ASCII art content
26
+ const asciiArt = lines . slice ( 1 ) ;
27
+
28
+ // Fill missing rows with blank lines to match the specified height
29
+ const filledAsciiArt = Array . from ( { length : height } , ( _ , y ) => {
30
+ return asciiArt [ y ] ? asciiArt [ y ] . padEnd ( width , ' ' ) : ' ' . repeat ( width ) ;
31
+ } ) ;
32
+
33
+ // Validate the dimensions of the ASCII art
34
+ for ( const line of filledAsciiArt ) {
35
+ if ( line . length !== width ) {
36
+ throw new Error ( `Invalid .atxt file: Expected each row to have ${ width } columns, but got "${ line } ".` ) ;
37
+ }
38
+ }
39
+
40
+ // Convert ASCII art to CSS box-shadow styles
41
+ const boxShadow = filledAsciiArt
42
+ . flatMap ( ( line , y ) =>
43
+ line . split ( '' ) . map ( ( char , x ) => {
44
+ if ( char === ' ' ) return null ; // Skip empty spaces
45
+ const color = char === '#' ? 'black' : 'gray' ; // Define colors based on characters
46
+ return `${ x } px ${ y } px 0 0 ${ color } ` ;
47
+ } )
48
+ )
49
+ . filter ( Boolean ) // Remove null values
50
+ . join ( ',\n ' ) ; // Format for better readability
51
+
52
+ // Generate the CSS content
53
+ return `
54
+ .${ className } {
55
+ width: 1px;
56
+ height: 1px;
57
+ box-shadow: ${ boxShadow } ;
58
+ transform: scale(10); /* Scale up the image */
59
+ transform-origin: top left; /* Important for scaling */
60
+ }
61
+ ` ;
62
+ }
0 commit comments