@@ -194,7 +194,7 @@ defmodule IO.ANSI.Docs do
194
194
lines
195
195
|> Enum . join ( " " )
196
196
|> handle_links
197
- |> handle_inline ( nil , [ ] , [ ] , options )
197
+ |> handle_inline ( true , nil , [ ] , [ ] , 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 ( nil , [ ] , [ ] , options )
265
+ |> handle_inline ( true , nil , [ ] , [ ] , options )
266
266
{ col , length_without_escape ( col , 0 ) }
267
267
end
268
268
@@ -398,61 +398,102 @@ defmodule IO.ANSI.Docs do
398
398
# ` does not require space in between
399
399
@ spaced [ ?_ , ?* ]
400
400
401
+ # Characters that can mark the beginning or the end of a word.
402
+ # Only support the most common ones at this moment.
403
+ @ delimiters [ ?\s , ?' , ?" , ?! , ?@ , ?# , ?$ , ?% , ?^ , ?& , ?- , ?+ , ?( , ?) , ?[ , ?] , ?{ , ?} , ?< , ?> ]
404
+
401
405
# Clauses for handling spaces
402
- defp handle_inline ( << ?* , ?* , ?\s , rest :: binary >> , nil , buffer , acc , options ) do
403
- handle_inline ( rest , nil , [ ?\s , ?* , ?* | buffer ] , acc , options )
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
409
+
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 )
412
+ end
413
+
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 )
404
418
end
405
419
406
- defp handle_inline ( << mark , ?\s , rest :: binary >> , nil , buffer , acc , options ) when mark in @ spaced do
407
- handle_inline ( rest , nil , [ ?\s , mark | buffer ] , acc , options )
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 )
408
423
end
409
424
410
- defp handle_inline ( << ?\s , ? *, ?* , rest :: binary >> , limit , buffer , acc , options ) do
411
- handle_inline ( rest , limit , [ ?* , ?* , ?\s | buffer ] , acc , options )
425
+ defp handle_inline ( << ?* , ?* , rest :: binary >> , true , nil , buffer , acc , options ) do
426
+ handle_inline ( rest , false , ?d , [ "**" ] , [ Enum . reverse ( buffer ) | acc ] , options )
412
427
end
413
428
414
- defp handle_inline ( << ?\s , mark , rest :: binary >> , limit , buffer , acc , options ) when mark in @ spaced do
415
- handle_inline ( rest , limit , [ mark , ?\s | buffer ] , acc , options )
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 )
431
+ end
432
+
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 )
436
+ end
437
+
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 )
416
440
end
417
441
418
442
# Clauses for handling escape
419
- defp handle_inline ( << ?\\ , ?\\ , rest :: binary >> , limit , buffer , acc , options ) do
420
- handle_inline ( rest , limit , [ ?\\ | buffer ] , acc , options )
443
+ defp handle_inline ( << ?\\ , ?\\ , ?* , ?* , rest :: binary >> , _line_starter , nil , buffer , acc , options )
444
+ when rest != "" do
445
+ handle_inline ( rest , false , ?d , [ "**" ] , [ ?\\ , Enum . reverse ( buffer ) | acc ] , options )
421
446
end
422
447
423
- defp handle_inline ( << ?\\ , ?* , ?* , rest :: binary >> , limit , buffer , acc , options ) do
424
- handle_inline ( rest , limit , [ ?* , ?* | buffer ] , acc , options )
448
+ defp handle_inline ( << ?\\ , ?\\ , mark , rest :: binary >> , _line_starter , nil , buffer , acc , options )
449
+ when rest != "" and mark in @ single do
450
+ handle_inline ( rest , false , mark , [ << mark >> ] , [ ?\\ , Enum . reverse ( buffer ) | acc ] , options )
425
451
end
426
452
427
- # A escape is not valid inside `
428
- defp handle_inline ( << ?\\ , mark , rest :: binary >> , limit , buffer , acc , options )
429
- when mark in [ ?_ , ?* , ?` ] and not ( mark == limit and mark == ?` ) do
430
- handle_inline ( rest , limit , [ mark | buffer ] , acc , options )
453
+ defp handle_inline ( << ?\\ , ?\\ , rest :: binary >> , _line_starter , limit , buffer , acc , options ) do
454
+ handle_inline ( rest , false , limit , [ ?\\ | buffer ] , acc , options )
431
455
end
432
456
433
- # Inline start
434
- defp handle_inline ( << ?* , ?* , rest :: binary >> , nil , buffer , acc , options ) when rest != "" do
435
- handle_inline ( rest , ?d , [ "**" ] , [ Enum . reverse ( buffer ) | acc ] , options )
457
+ defp handle_inline ( << ?\\ , ?* , ?* , rest :: binary >> , _line_starter , limit , buffer , acc , options ) do
458
+ handle_inline ( rest , false , limit , [ ?* , ?* | buffer ] , acc , options )
436
459
end
437
460
438
- defp handle_inline ( << mark , rest :: binary >> , nil , buffer , acc , options ) when rest != "" and mark in @ single do
439
- handle_inline ( rest , mark , [ << mark >> ] , [ Enum . reverse ( buffer ) | acc ] , options )
461
+ # 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 )
440
465
end
441
466
442
467
# Inline end
443
- defp handle_inline ( << ?* , ?* , rest :: binary >> , ?d , buffer , acc , options ) do
444
- handle_inline ( rest , nil , [ ] , [ inline_buffer ( buffer , options ) | acc ] , options )
468
+ defp handle_inline ( << ?* , ?* , delimiter , rest :: binary >> , _line_starter , ?d , buffer , acc , options )
469
+ when delimiter in @ delimiters do
470
+ handle_inline ( << delimiter , rest :: binary >> , false , nil , [ ] , [ inline_buffer ( buffer , options ) | acc ] , options )
471
+ end
472
+
473
+ defp handle_inline ( << mark , delimiter , rest :: binary >> , _line_starter , mark , buffer , acc , options )
474
+ when delimiter in @ delimiters and mark in @ single do
475
+ handle_inline ( << delimiter , rest :: binary >> , false , nil , [ ] , [ inline_buffer ( buffer , options ) | acc ] , options )
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 )
480
+ end
481
+
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 )
445
485
end
446
486
447
- defp handle_inline ( << mark , rest :: binary >> , mark , buffer , acc , options ) when mark in @ single do
448
- handle_inline ( rest , nil , [ ] , [ inline_buffer ( buffer , options ) | acc ] , options )
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 )
449
490
end
450
491
451
- defp handle_inline ( << char , rest :: binary >> , mark , buffer , acc , options ) do
452
- handle_inline ( rest , mark , [ char | buffer ] , acc , options )
492
+ defp handle_inline ( << char , rest :: binary >> , _line_starter , mark , buffer , acc , options ) do
493
+ handle_inline ( rest , false , mark , [ char | buffer ] , acc , options )
453
494
end
454
495
455
- defp handle_inline ( << >> , _mark , buffer , acc , _options ) do
496
+ defp handle_inline ( << >> , _line_starter , _mark , buffer , acc , _options ) do
456
497
IO . iodata_to_binary Enum . reverse ( [ Enum . reverse ( buffer ) | acc ] )
457
498
end
458
499
0 commit comments