@@ -241,12 +241,12 @@ defmodule IO.ANSI.Docs do
241
241
end
242
242
243
243
defp table_lines ( lines , options ) do
244
- lines = Enum . map ( lines , & split_into_columns / 1 )
245
- count = lines |> Enum . map ( & length / 1 ) |> Enum . max
244
+ lines = Enum . map ( lines , & split_into_columns ( & 1 , options ) )
245
+ count = Enum . map ( lines , & length / 1 ) |> Enum . max
246
246
lines = Enum . map ( lines , & pad_to_number_of_columns ( & 1 , count ) )
247
247
248
248
widths = for line <- lines , do:
249
- ( for col <- line , do: effective_length ( col ) )
249
+ ( for { _col , length } <- line , do: length )
250
250
251
251
col_widths = Enum . reduce ( widths ,
252
252
List . duplicate ( 0 , count ) ,
@@ -255,24 +255,33 @@ defmodule IO.ANSI.Docs do
255
255
render_table ( lines , col_widths , options )
256
256
end
257
257
258
- defp split_into_columns ( line ) do
258
+ defp split_into_columns ( line , options ) do
259
259
line
260
260
|> String . strip ( ?| )
261
261
|> String . strip ( )
262
262
|> String . split ( ~r/ \s \| \s / )
263
+ |> Enum . map ( & render_column ( & 1 , options ) )
263
264
end
264
265
265
- defp pad_to_number_of_columns ( cols , col_count ) ,
266
- do: cols ++ List . duplicate ( "" , col_count - length ( cols ) )
266
+ defp render_column ( col , options ) do
267
+ col = col
268
+ |> String . replace ( ~r/ \\ \| / x , "|" )
269
+ |> handle_links
270
+ |> handle_inline ( nil , [ ] , [ ] , options )
267
271
268
- defp max_column_widths ( cols , widths ) do
269
- Enum . zip ( cols , widths ) |> Enum . map ( fn { a , b } -> max ( a , b ) end )
270
- end
272
+ len = col
273
+ |> String . replace ( ~r / \e \[ \d +m / , "" )
274
+ |> String . length
271
275
272
- defp effective_length ( text ) do
273
- String . length ( Regex . replace ( ~r/ ((^|\b )[`*_]+)|([`*_]+\b )/ , text , "" ) )
276
+ { col , len }
274
277
end
275
278
279
+ defp pad_to_number_of_columns ( cols , col_count ) ,
280
+ do: cols ++ List . duplicate ( "" , col_count - length ( cols ) )
281
+
282
+ defp max_column_widths ( cols , widths ) ,
283
+ do: Enum . zip ( cols , widths ) |> Enum . map ( fn { a , b } -> max ( a , b ) end )
284
+
276
285
# If second line is heading separator, use the heading style on the first
277
286
defp render_table ( [ first , second | rest ] , widths , options ) do
278
287
combined = Enum . zip ( first , widths )
@@ -291,20 +300,17 @@ defmodule IO.ANSI.Docs do
291
300
render_table ( rest , widths , options )
292
301
end
293
302
294
- defp render_table ( [ ] , _ , _ ) , do: nil
303
+ defp render_table ( [ ] , _ , _ ) ,
304
+ do: nil
295
305
296
306
defp table_header? ( row ) , do:
297
- Enum . all? ( row , fn col -> col =~ ~r/ ^:?-+:?$/ end )
307
+ Enum . all? ( row , fn { col , _ } -> col =~ ~r/ ^:?-+:?$/ end )
298
308
299
309
defp draw_table_row ( cols_and_widths , options , heading \\ false ) do
300
- columns = for { col , width } <- cols_and_widths do
301
- padding = width - effective_length ( col )
302
- col = Regex . replace ( ~r/ \\ \| / x , col , "|" ) # escaped bars
303
- text = col
304
- |> handle_links
305
- |> handle_inline ( nil , [ ] , [ ] , options )
306
- text <> String . duplicate ( " " , padding )
307
- end |> Enum . join ( " | " )
310
+ columns =
311
+ Enum . map_join ( cols_and_widths , " | " , fn { { col , length } , width } ->
312
+ col <> String . duplicate ( " " , width - length )
313
+ end )
308
314
309
315
if heading do
310
316
write ( :doc_table_heading , columns , options )
0 commit comments