[code] Improve the strong and emphasis of markdown lexer

From: Alexander Misel <alexander_misel.att.live.cn>
Date: Wed, 8 May 2019 03:37:08 +0000

The following code is my implementation of strong and emphasis of markdown, where B is lpeg.B, which is more conformant to markdown spec. It passes most examples shown in the github markdown spec, with my understanding of the left(right) flanking delimiter run.
local punct_space = lexer.punct + lexer.space

function flanking_between(s)
  local fl_char = lexer.any - s - lexer.space
  local left_fl = B(punct_space - s) * s * #fl_char + s * #(fl_char - lexer.punct)
  local right_fl = B(lexer.punct) * s * #(punct_space - s) + B(fl_char) * s
  return left_fl * (lexer.any - s)^0 * right_fl
end

lex:add_rule('strong', token('strong', flanking_between('**') + (B(punct_space) + #lexer.starts_line('_')) *
                                       flanking_between('__') * #(punct_space + P(-1))))
lex:add_style('strong', 'bold')

lex:add_rule('em', token('em', flanking_between('*') + (B(punct_space) + #lexer.starts_line('_')) *
                               flanking_between('_') * #(punct_space + P(-1))))
lex:add_style('em', 'italics')

-- 
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Tue 07 May 2019 - 23:37:08 EDT

This archive was generated by hypermail 2.2.0 : Wed 08 May 2019 - 06:28:41 EDT