@@ -194,7 +194,7 @@ defmodule IO.ANSI.Docs do
194
194
lines
195
195
|> Enum . join ( " " )
196
196
|> handle_links
197
- |> handle_inline ( true , nil , [ ] , [ ] , options )
197
+ |> handle_inline ( options )
198
198
|> String . split ( ~r{ \s } )
199
199
|> write_with_wrap ( options [ :width ] - byte_size ( indent ) , indent , no_wrap )
200
200
@@ -262,7 +262,7 @@ defmodule IO.ANSI.Docs do
262
262
col = col
263
263
|> String . replace ( ~r/ \\ \| / x , "|" )
264
264
|> handle_links
265
- |> handle_inline ( true , nil , [ ] , [ ] , options )
265
+ |> handle_inline ( options )
266
266
{ col , length_without_escape ( col , 0 ) }
267
267
end
268
268
@@ -392,108 +392,110 @@ defmodule IO.ANSI.Docs do
392
392
Regex . replace ( ~r{ \[ (.*?)\] \( (.*?)\) } , text , "\\ 1 (\\ 2)" )
393
393
end
394
394
395
- # Single inline quotes.
396
- @ single [ ?` , ?_ , ?* ]
397
395
398
- # ` does not require space in between
399
- @ spaced [ ?_ , ?* ]
396
+ # We have four entries: **, *, _ and `.
397
+ #
398
+ # The first three behave the same while the last one is simpler
399
+ # when it comes to delimiters. But, since the first has two
400
+ # characters, we need to handle 3 cases:
401
+ #
402
+ # 1. **
403
+ # 2. _ and *
404
+ # 3. `
405
+ #
406
+ # Where the first two should have the same code but match differently.
407
+ @ single [ ?_ , ?* ]
400
408
401
409
# Characters that can mark the beginning or the end of a word.
402
410
# Only support the most common ones at this moment.
403
411
@ delimiters [ ?\s , ?' , ?" , ?! , ?@ , ?# , ?$ , ?% , ?^ , ?& , ?- , ?+ , ?( , ?) , ?[ , ?] , ?{ , ?} , ?< , ?> ]
404
412
405
- # Clauses for handling spaces
406
- defp handle_inline ( << ?* , ?* , ?\s , rest :: binary >> , _line_starter , nil , buffer , acc , options ) do
407
- handle_inline ( rest , false , nil , [ ?\s , ?* , ?* | buffer ] , acc , options )
408
- end
413
+ # Inline start
409
414
410
- defp handle_inline ( << mark , ?\s , rest :: binary >> , _line_starter , nil , buffer , acc , options ) when mark in @ spaced do
411
- handle_inline ( rest , false , nil , [ ?\s , mark | buffer ] , acc , options )
415
+ defp handle_inline ( << ?* , ?* , rest :: binary >> , options ) do
416
+ handle_inline ( rest , ?d , [ "**" ] , [ ] , options )
412
417
end
413
418
414
- # Inline start
415
- defp handle_inline ( << delimiter , ?* , ?* , rest :: binary >> , _line_starter , nil , buffer , acc , options )
416
- when rest != "" and delimiter in @ delimiters do
417
- handle_inline ( rest , false , ?d , [ "**" ] , [ delimiter , Enum . reverse ( buffer ) | acc ] , options )
419
+ defp handle_inline ( << mark , rest :: binary >> , options ) when mark in @ single do
420
+ handle_inline ( rest , mark , [ << mark >> ] , [ ] , options )
418
421
end
419
422
420
- defp handle_inline ( << delimiter , mark , rest :: binary >> , _line_starter , nil , buffer , acc , options )
421
- when rest != "" and delimiter in @ delimiters and mark in @ single do
422
- handle_inline ( rest , false , mark , [ << mark >> ] , [ delimiter , Enum . reverse ( buffer ) | acc ] , options )
423
+ defp handle_inline ( rest , options ) do
424
+ handle_inline ( rest , nil , [ ] , [ ] , options )
423
425
end
424
426
425
- defp handle_inline ( << ?* , ?* , rest :: binary >> , true , nil , buffer , acc , options ) do
426
- handle_inline ( rest , false , ?d , [ "**" ] , [ Enum . reverse ( buffer ) | acc ] , options )
427
- end
427
+ # Inline delimiters
428
428
429
- defp handle_inline ( << mark , rest :: binary >> , true , nil , buffer , acc , options ) when mark in @ single do
430
- handle_inline ( rest , false , mark , [ << mark >> ] , [ Enum . reverse ( buffer ) | acc ] , options )
429
+ defp handle_inline ( << delimiter , ?* , ?* , rest :: binary >> , nil , buffer , acc , options )
430
+ when rest != "" and delimiter in @ delimiters do
431
+ handle_inline ( rest , ?d , [ "**" ] , [ delimiter , Enum . reverse ( buffer ) | acc ] , options )
431
432
end
432
433
433
- # Clauses for handling spaces
434
- defp handle_inline ( << ?\s , ?* , ?* , rest :: binary >> , _line_starter , limit , buffer , acc , options ) do
435
- handle_inline ( rest , false , limit , [ ?* , ?* , ?\s | buffer ] , acc , options )
434
+ defp handle_inline ( << delimiter , mark , rest :: binary >> , nil , buffer , acc , options )
435
+ when rest != "" and delimiter in @ delimiters and mark in @ single do
436
+ handle_inline ( rest , mark , [ << mark >> ] , [ delimiter , Enum . reverse ( buffer ) | acc ] , options )
436
437
end
437
438
438
- defp handle_inline ( << ?\s , mark , rest :: binary >> , _line_starter , limit , buffer , acc , options ) when mark in @ spaced do
439
- handle_inline ( rest , false , limit , [ mark , ?\s | buffer ] , acc , options )
439
+ defp handle_inline ( << ?` , rest :: binary >> , nil , buffer , acc , options )
440
+ when rest != "" do
441
+ handle_inline ( rest , ?` , [ "`" ] , [ Enum . reverse ( buffer ) | acc ] , options )
440
442
end
441
443
442
444
# Clauses for handling escape
443
- defp handle_inline ( << ?\\ , ?\\ , ?* , ?* , rest :: binary >> , _line_starter , nil , buffer , acc , options )
445
+
446
+ defp handle_inline ( << ?\\ , ?\\ , ?* , ?* , rest :: binary >> , nil , buffer , acc , options )
444
447
when rest != "" do
445
- handle_inline ( rest , false , ?d , [ "**" ] , [ ?\\ , Enum . reverse ( buffer ) | acc ] , options )
448
+ handle_inline ( rest , ?d , [ "**" ] , [ ?\\ , Enum . reverse ( buffer ) | acc ] , options )
446
449
end
447
450
448
- defp handle_inline ( << ?\\ , ?\\ , mark , rest :: binary >> , _line_starter , nil , buffer , acc , options )
451
+ defp handle_inline ( << ?\\ , ?\\ , mark , rest :: binary >> , nil , buffer , acc , options )
449
452
when rest != "" and mark in @ single do
450
- handle_inline ( rest , false , mark , [ << mark >> ] , [ ?\\ , Enum . reverse ( buffer ) | acc ] , options )
451
- end
452
-
453
- defp handle_inline ( << ?\\ , ?\\ , rest :: binary >> , _line_starter , limit , buffer , acc , options ) do
454
- handle_inline ( rest , false , limit , [ ?\\ | buffer ] , acc , options )
453
+ handle_inline ( rest , mark , [ << mark >> ] , [ ?\\ , Enum . reverse ( buffer ) | acc ] , options )
455
454
end
456
455
457
- defp handle_inline ( << ?\\ , ?* , ?* , rest :: binary >> , _line_starter , limit , buffer , acc , options ) do
458
- handle_inline ( rest , false , limit , [ ?* , ?* | buffer ] , acc , options )
456
+ defp handle_inline ( << ?\\ , ?\\ , rest :: binary >> , limit , buffer , acc , options ) do
457
+ handle_inline ( rest , limit , [ ?\\ | buffer ] , acc , options )
459
458
end
460
459
461
460
# An escape is not valid inside `
462
- defp handle_inline ( << ?\\ , mark , rest :: binary >> , _line_starter , limit , buffer , acc , options )
463
- when mark in [ ?_ , ?* , ?` ] and not ( mark == limit and mark == ?` ) do
464
- handle_inline ( rest , false , limit , [ mark | buffer ] , acc , options )
461
+ defp handle_inline ( << ?\\ , mark , rest :: binary >> , limit , buffer , acc , options )
462
+ when not ( mark == limit and mark == ?` ) do
463
+ handle_inline ( rest , limit , [ mark | buffer ] , acc , options )
465
464
end
466
465
467
466
# Inline end
468
- defp handle_inline ( << ?* , ?* , delimiter , rest :: binary >> , _line_starter , ?d , buffer , acc , options )
467
+
468
+ defp handle_inline ( << ?* , ?* , delimiter , rest :: binary >> , ?d , buffer , acc , options )
469
469
when delimiter in @ delimiters do
470
- handle_inline ( << delimiter , rest :: binary >> , false , nil , [ ] , [ inline_buffer ( buffer , options ) | acc ] , options )
470
+ handle_inline ( << delimiter , rest :: binary >> , nil , [ ] , [ inline_buffer ( buffer , options ) | acc ] , options )
471
471
end
472
472
473
- defp handle_inline ( << mark , delimiter , rest :: binary >> , _line_starter , mark , buffer , acc , options )
473
+ defp handle_inline ( << mark , delimiter , rest :: binary >> , mark , buffer , acc , options )
474
474
when delimiter in @ delimiters and mark in @ single do
475
- handle_inline ( << delimiter , rest :: binary >> , false , nil , [ ] , [ inline_buffer ( buffer , options ) | acc ] , options )
475
+ handle_inline ( << delimiter , rest :: binary >> , nil , [ ] , [ inline_buffer ( buffer , options ) | acc ] , options )
476
476
end
477
-
478
- defp handle_inline ( << ?\s , ?` , rest :: binary >> , _line_starter , ?` , buffer , acc , options ) do
479
- handle_inline ( rest , false , nil , [ ] , [ inline_buffer ( [ ?\s | buffer ] , options ) | acc ] , options )
477
+
478
+ defp handle_inline ( << ?* , ?* , rest :: binary >> , ?d , buffer , acc , options )
479
+ when rest == "" do
480
+ handle_inline ( << >> , nil , [ ] , [ inline_buffer ( buffer , options ) | acc ] , options )
480
481
end
481
482
482
- defp handle_inline ( << ?* , ?* , rest :: binary >> , _line_starter , ?d , buffer , acc , options )
483
- when rest in @ delimiters or rest == "" do
484
- handle_inline ( << >> , false , nil , [ ] , [ inline_buffer ( buffer , options ) | acc ] , options )
483
+ defp handle_inline ( << mark , rest :: binary >> , mark , buffer , acc , options )
484
+ when rest == "" and mark in @ single do
485
+ handle_inline ( << >> , nil , [ ] , [ inline_buffer ( buffer , options ) | acc ] , options )
485
486
end
486
487
487
- defp handle_inline ( << mark , rest :: binary >> , _line_starter , mark , buffer , acc , options )
488
- when ( rest in @ delimiters or rest == "" ) and mark in @ single do
489
- handle_inline ( << >> , false , nil , [ ] , [ inline_buffer ( buffer , options ) | acc ] , options )
488
+ defp handle_inline ( << ?` , rest :: binary >> , ?` , buffer , acc , options ) do
489
+ handle_inline ( rest , nil , [ ] , [ inline_buffer ( buffer , options ) | acc ] , options )
490
490
end
491
491
492
- defp handle_inline ( << char , rest :: binary >> , _line_starter , mark , buffer , acc , options ) do
493
- handle_inline ( rest , false , mark , [ char | buffer ] , acc , options )
492
+ # Catch all
493
+
494
+ defp handle_inline ( << char , rest :: binary >> , mark , buffer , acc , options ) do
495
+ handle_inline ( rest , mark , [ char | buffer ] , acc , options )
494
496
end
495
497
496
- defp handle_inline ( << >> , _line_starter , _mark , buffer , acc , _options ) do
498
+ defp handle_inline ( << >> , _mark , buffer , acc , _options ) do
497
499
IO . iodata_to_binary Enum . reverse ( [ Enum . reverse ( buffer ) | acc ] )
498
500
end
499
501
0 commit comments