[code] Re: Improve the strong and emphasis of markdown lexer

From: Alexander Misel <alexander_misel.att.live.cn>
Date: Wed, 8 May 2019 06:36:28 +0000

Minor improvement for cases like _aa_bb_cc_:
local punct_space = lexer.punct + lexer.space

function flanking_between(s, not_inword)
  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 - (not_inword and (s * #(punct_space + P(-1))) or s))^0 * right_fl
end

lex:add_rule('strong', token('strong', flanking_between('**') + (B(punct_space) + #lexer.starts_line('_')) *
                                       flanking_between('__', true) * #(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('_', true) * #(punct_space + P(-1))))
lex:add_style('em', 'italics')
________________________________
From: Alexander Misel <alexander_misel.att.live.cn>
Sent: Wednesday, May 8, 2019 11:37
To: code.att.foicica.com
Subject: [code] Improve the strong and emphasis of markdown lexer

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 Wed 08 May 2019 - 02:36:28 EDT

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