When I write documentation, comments, etc. I love making ASCII tables. They usually end up looking pretty good, but I always feel that they could look even better - especially since UTF-8/Unicode includes the box drawing characters. However, these characters are very burdensome to use, requiring several key presses to insert. Your task? Write a program or a function that can automatically convert ASCII tables to the UTF-8/Unicode equivalent.
This challenge was sandboxed.
Challenge
Write a program, that given an ASCII table as an input string, outputs the table redrawn with the Unicode/UTF-8 box drawing characters. Specifically, the characters that are a part of the table should be translated as follows:
(Unicode, 3 bytes each in UTF-8)
- to ─ (\u2500)
| to │ (\u2502)
= to ═ (\u2550)
and + to one of:
┌ (\u250C), ┐ (\u2510), └ (\u2514), ┘ (\u2518),
├ (\u251C), ┤ (\u2524), ┬ (\u252C), ┴ (\u2534),
┼ (\u253C)
or, if '=' on either side:
╒ (\u2552), ╕ (\u2555), ╘ (\u2558), ╛ (\u255D),
╞ (\u255E), ╡ (\u2561), ╤ (\u2564), ╧ (\u2567),
╪ (\u256A)
Details
I/O:
- Default I/O is allowed
- You may take input in any reasonable format, including the table as a string, or a path to a file containing the table.
- You may output to a file and take the file name as an additional argument.
- However, you may not modify the input file. (It should be retained for ease of future editing)
Input:
- You may assume that every row of input has been padded to be the same length with
.
- You may not assume that the first character after a newline is a part of the table borders (as it may be whitespace).
- Input is considered a valid table if all characters (that are a part of the table)
-=|
are connected to exactly two characters and+
are connected to at least one character both horizontally and vertically. - Your program may not produce any errors with valid inputs.
- If the input is not valid the behavior is undefined and you may produce any output.
- The input may contain any UTF-8 characters, including the box drawing characters.
Output:
- Any of the characters
-=|+
that are not a part of the table must be left as-is. - Similarly, any other characters must be left as-is.
- A single leading and/or trailing newline is allowed.
Other:
- Standard loopholes are forbidden, as per usual.
- If your preferred language has a built-in that solves this problem, you may not use it.
- This means programs, functions, subroutines or instructions that would be valid submissions for this challenge with no additions.
- Each of the characters needed in this challenge are three bytes long when they're encoded in UTF-8.
Connected characters:
A character is connected to another, if:
- It is
|
and is directly above or below+
or|
; - It is
-
and is directly before or after+
or-
; - It is
=
and is directly before or after+
or=
; - It is
+
and is directly above or below|
or+
, or is directly before or after-
,=
or+
.
A character is considered a part of the table, if it is connected to any character that is a part of the table. By definition, the first +
in the input is a part of the table.
Examples
Examples available here as a copy-pastable version.
Input: Output:
+------------------+ ┌──────────────────┐
| Hello+World! | │ Hello+World! │
+==================+ ╞══════════════════╡
| This is+my first | -> │ This is+my first │
|+-+ code|golf +-+| │+-+ code|golf +-+│
|+-+chall|enge! +-+| │+-+chall|enge! +-+│
+------------------+ └──────────────────┘
+===+===+===+ ╒═══╤═══╤═══╕
| 1 | 2 | 3 | │ 1 │ 2 │ 3 │
+---+===+===+===+ ┌───╪═══╪═══╪═══╡
| 1 | 1 | 2 | 3 | │ 1 │ 1 │ 2 │ 3 │
+---+---+---+---+ -> ├───┼───┼───┼───┤
| 2 | 2 | 4 | 6 | │ 2 │ 2 │ 4 │ 6 │
+---+---+---+---+ ├───┼───┼───┼───┤
|-3 |-3 |-6 |-9 | │-3 │-3 │-6 │-9 │
+===+---+---+---+ ╘═══╧───┴───┴───┘
+-----+ -> <Undefined>
+-----+ -> ┌─────┐
+-----+ └─────┘
+-----------------+
| Hello, World! |
| This is invalid | -> <Undefined>
| input |
-----------------+
++++ ┌┬┬┐
++++ -> ├┼┼┤
++++ └┴┴┘
+--+
++++ -> <Undefined>
+--+
Finally...
This is code-golf, so the least amount of bytes wins. Happy golfing!
+-+
excerpts not considered to form a connected table? \$\endgroup\$Hello World
table, the inner tables are not considered to form a table because the text inside the table must stay unchanged, and they are not considered a part of the outer table borders as they are not connected to them properly. \$\endgroup\$+----+
example, it would be because the direction of the corners would be ambiguous. \$\endgroup\$