Last active
February 20, 2022 23:11
-
-
Save isoraqathedh/abb8ea76a34b08c494f50d8911b5743f to your computer and use it in GitHub Desktop.
Convert a station specifier to a table.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/awk -f | |
# Script for automatically generating metro networks | |
# based on a more compact script. | |
# We're working with a CSV-like language. | |
BEGIN { | |
debug = 0; | |
time_pad = 4; | |
FS = ", "; | |
} | |
/^ *#/ || /^$/ { next; } # Comment "processing" | |
setup_complete_p && debug { | |
print(" <!--", $0, "-->"); | |
} | |
function to_images(tilestring) { | |
split(tilestring, tiles, "/"); | |
for (tile in tiles) { | |
gsub(/^ */, "", tiles[tile]); | |
printf("<img src=\"Metro icons/%s.png\"/>", tiles[tile]); | |
} | |
} | |
function pad(number, amount) { | |
if (number == "") { | |
return ""; | |
} | |
# Pads with figure space instead of regular space. | |
# Ignore the fraction part. | |
l = length(number) | |
real_number = substr(number, 0, l - 1); | |
final_string = number ~ /-$/ ? "" : substr(number, l, l); | |
done_string = sprintf("%" amount "d%s", real_number, final_string); | |
gsub(/ /, " ", done_string); | |
return done_string; | |
} | |
# Setup | |
$1 ~ /^name$/ { line_name = $2; } | |
$1 ~ /^debug$/ { debug = 1; } | |
$1 ~ /^table-class/ { classp = $2; } | |
# Loop processing | |
$1 ~ /^direction$/ { | |
gsub(/^ */, "", $2); | |
gsub(/^ */, "", $3); | |
forward = $2; | |
backward = $3; | |
} | |
$1 ~ /^loop$/ { | |
do_loop = 1; | |
gsub(/^ */, "", $2); | |
gsub(/^ */, "", $3); | |
forward = $2; | |
backward = $3; | |
} | |
$1 ~ /^first-row-symbols$/ { | |
first_row_curve = $2; | |
first_row_pad = $3; | |
start_curve = $4; | |
} | |
$1 ~ /^first-row-padding$/ { | |
first_row_pad_amount = $2; | |
first_row_start = $3; | |
} | |
$1 ~ /^final-row-symbols$/ { | |
final_row_curve = $2; | |
final_row_pad = $3; | |
finish_curve = $4; | |
} | |
$1 ~ /^final-row-padding$/ { | |
final_row_pad_amount = $2; | |
final_row_start = $3; | |
} | |
$1 ~ /^return-column-symbol$/ { | |
return_column_symbol = $2; | |
} | |
/^setup-complete$|^line-data$/ { | |
# Setup is complete, time to start printing the header. | |
setup_complete_p = 1; | |
if (classp) { printf("<table class=\"%s\">\n", do_loop ? "loop-" classp : classp) } | |
else { print "<table>"; } | |
printf( " <caption>%s</caption>\n", line_name); | |
print " <thead>"; | |
print " <tr>"; | |
if (do_loop) { print " <th rowspan=\"2\"></th>"; } | |
print " <th colspan=\"2\">Code</th>"; | |
print " <th rowspan=\"2\">Station name</th>"; | |
print " <th rowspan=\"2\">Time</th>"; | |
print " <th rowspan=\"2\"></th>"; | |
print " <th rowspan=\"2\"></th>"; | |
print " </tr>"; | |
print " <tr>"; | |
printf( " <th>%s</th>\n", forward); | |
printf( " <th>%s</th>\n", backward); | |
print " </tr>"; | |
print " </thead>"; | |
print " <tbody>"; | |
if (do_loop) { | |
print " <tr>" | |
printf( " <td>"); | |
to_images(first_row_curve); | |
print ( "</td>"); | |
printf( " <td colspan=\"4\">"); | |
for(i = 0; i < first_row_pad_amount; i++) { to_images(first_row_pad); } | |
print ( "</td>"); | |
printf( " <td>"); | |
for(i = 0; i < first_row_start; i++) { to_images(first_row_pad); } | |
to_images(start_curve); | |
print( "</td>"); | |
print ( " <td colspan=\"3\"></td>"); | |
print ( " </tr>"); | |
} | |
next; # Halt processing to prevent the printing of any other things. | |
} | |
# Each of the data lines should have the following items, in this order. | |
# the forward code; | |
# the backward code; | |
# the station name; | |
# the time, which is a number that is a multiple of ¼; | |
# the station figure, which contains: | |
# at least one slash-separated object, | |
# which is the filename of the line tile. | |
# for instance, the blank tile has the filename ".png", | |
# so its representation is the empty string (plus the delimiting slash); | |
# the notes, which is any string. | |
setup_complete_p { | |
# Strip leading spaces | |
gsub(/^ */, "", $1); | |
gsub(/^ */, "", $2); | |
gsub(/^ */, "", $3); | |
gsub(/^ */, "", $4); | |
gsub(/^ */, "", $5); | |
gsub(/^ */, "", $6); | |
# Print the row | |
printf(" <tr>"); | |
if (do_loop) { | |
print ""; | |
printf(" <td>"); | |
to_images(return_column_symbol); | |
printf("</td>"); | |
} | |
printf("<td>%s</td><td>%s</td><td>%s</td><td>%s</td>\n", | |
$1, $2, $3, pad($4, time_pad)); | |
printf(" <td>"); to_images($5); printf("</td>\n") | |
printf(" <td>%s</td>\n", $6); | |
printf(" </tr>\n"); | |
} | |
END { | |
if (do_loop) { | |
print " <tr>" | |
printf( " <td>"); | |
to_images(final_row_curve); | |
print ( "</td>"); | |
printf( " <td colspan=\"4\">"); | |
for(i = 0; i < final_row_pad_amount; i++) { to_images(final_row_pad); } | |
print ( "</td>"); | |
printf( " <td>"); | |
for(i = 0; i < final_row_start; i++) { to_images(final_row_pad); } | |
to_images(finish_curve); | |
print( "</td>"); | |
print ( " <td colspan=\"3\"></td>"); | |
print ( " </tr>"); | |
} | |
print " </tbody>"; | |
print "</table>"; | |
} |
We can make this file beautiful and searchable if this error is corrected: It looks like row 3 should actually have 1 column, instead of 2. in line 2.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# An example used in practise. | |
# Northeast line | |
name, Northwest Line | |
direction, N, S | |
setup-complete | |
1, 20, North Suburbs, 0-, KBHFa | |
2, 19, Legata, 2-, BHF | |
3, 18, Besiyon, 4½, BHF | |
, , 10 ↑↓ 9, , STR+GRZq/exCONTg, To Orpulťe | |
, , , , STR/exSTR/exCONTg, To Tavros | |
4, 17, Vesüsorřanta, 7-, BHF/exCPICl/exCPICr | |
, , , , STR/exCONTf/exSTR, To Lūš | |
, , , , STR//exCONTf, To the Loganar Complex | |
5, 16, Lollütred, 12¼, BHF | |
6, 15, Élstriu, 15¾, BHF | |
, , 9 ↑↓ 8, 18-, STR+GRZq | |
7, 14, Beltana, 21-, BHF | |
, , , , STR/exCONTg, To the Loganar Complex via Yurpen | |
8, 13, Burenba, 23¾, CPICl/exCPICr | |
9, 12, Burenba South, 25½, CPICl/exCPICr | |
, , , , STR/exLSTR | |
, , , , STR | |
, , , , STR/exCONTg, To the Loganar Complex | |
10, 11, Lodūrén North, 28¼, CPICl/exCPICr | |
11, 10, Lodūrén South, 30-, CPICl/exCPICr | |
, , , , STR/exBHF, Yopúlu | |
12, 9, Loknatā, 33-, BHF/exSTR | |
, , , , STR/exSTR2/exSTRc3 | |
, , , , STR/exSTRc1/exSTR+4 | |
, , , , tSTRa//extSTRa | |
, , , , tSTR/extSTRrg/extKRZt/extCONTfq, To the Loganar Complex via Vurtene | |
13, 8, Bulusena, 37¼, tCPICl/extCPICm/extCPICr | |
, , , , tSTR/extCONTf/extSTR, To Tavros | |
, , , , tSTR//extCONTf, To the Loganar Complex via Losendar | |
14, 7, Biltra, 43¾, tBHF | |
15, 6, Telvraton, 48-, tBHF | |
16, 5, Lrednat, 51½, tBHF | |
, , 8 ↑↓ 4, , tSTR+GRZq | |
, , , , tSTR/extCONTg, to Galsena | |
17, 4, Ťeťnu, 54¾, tBHF/extBHF | |
, , , , tSTR/extCONTf, to Manlün | |
18, 3, Eln, 57½, tBHF | |
, , 4 ↑↓ 8, , tSTR+GRZq | |
, , , , tABZgl+l/tSTRlg | |
, , , , tSTR/tÜST | |
18S, 2S, (Hnorkit), , tSTR/KDSTCCe | |
, , 8 ↑↓ 7, , tSTR+GRZq | |
, , , , tSTR/extLSTR | |
19, 2, Eleyoba, 65-, tBHF/extBHF | |
, , , , tSTR/extCONTf, to the Loganar Complex via Losendar | |
, , , , tSTR | |
, , , , tSTR/extCONTg, to the Loganar Complex | |
, , , , tSTR/extSTR/extCONTg, to Ellantan | |
20, 1, Eleyoba South, 70½, tCPICle/extCPICm/extCPICre | |
, , , , /extCONTf, to Tavros | |
# Note: comments must not be preceded by anything other than whitespace. |
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 1 column, instead of 2. in line 1.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a test of the metro script to see if it can handle loops. | |
name, Test | |
table-class, metro-diagram | |
loop, I, O | |
return-column-symbol, tSTR | |
first-row-symbols, tSTRrg, tSTRq, tSTRlg | |
first-row-padding, 13, 0 | |
final-row-symbols, tSTRlf, tSTRq, tSTRrf | |
final-row-padding, 13, 2 | |
line-data | |
, , , , tSTRe | |
1, 5, Station A, 0-, BHF | |
2, 4, Station B, 1-, BHF | |
3, 3, Station C, 2-, BHF, Some notes. | |
, , , , STRlf/STRq/STRlg | |
, , , , //tSTRa | |
4, 2, Station D, 3-, //tBHF | |
5, 1, Station E, 4-, //tBHF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment