Class: PointBlank::Parsing::EmphInline

Inherits:
NullInline
  • Object
show all
Defined in:
lib/mmmd/blankshell.rb

Overview

Emphasis and strong emphasis inline parser

Constant Summary collapse

INFIX_TOKENS =
/^[^\p{S}\p{P}\p{Zs}_]_++[^\p{S}\p{P}\p{Zs}_]$/

Class Method Summary collapse

Methods inherited from NullInline

build, check_contents, check_unescaped, construct_literal, construct_text, find_unescaped, forward_walk, iterate_tokens

Class Method Details

.break_into_elements(token_inner, token, left, right, matched) ⇒ Object

Break token string into elements

Parameters:

  • token_inner (String)
  • token (String)
  • left (Boolean)
  • right (Boolean)
  • matched (Boolean)


1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
# File 'lib/mmmd/blankshell.rb', line 1354

def self.break_into_elements(token_inner, token, left, right, matched)
  return token_inner[0] unless matched

  star_token = token_inner.match?(/^\*+$/)
  infix_token = token.match(INFIX_TOKENS)
  return token_inner if !star_token && infix_token

  if left && right
    [token_inner, self, :open, :close]
  elsif left
    [token_inner, self, :open]
  elsif right
    [token_inner, self, :close]
  else
    token_inner
  end
end

.build_emph(children, strong) ⇒ ::PointBlank::DOM::DOMObject

Build strong or normal emphasis depending on the boolean flag

Parameters:

Returns:



1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
# File 'lib/mmmd/blankshell.rb', line 1415

def self.build_emph(children, strong)
  obj = if strong
          ::PointBlank::DOM::InlineStrong
        else
          ::PointBlank::DOM::InlineEmphasis
        end.new
  tokens = children.map do |child|
    child.is_a?(Array) ? child.first : child
  end
  scanner = StackScanner.new(obj, init_tokens: tokens)
  scanner.scan
  obj
end

.left_token?(bfr, _token, afr) ⇒ Boolean

Is this token, given these surrounding characters, left-flanking?

Parameters:

  • bfr (String)
  • token (String)
  • afr (String)

Returns:

  • (Boolean)


1328
1329
1330
1331
1332
1333
1334
# File 'lib/mmmd/blankshell.rb', line 1328

def self.left_token?(bfr, _token, afr)
  bfr_white = bfr.match?(/[\p{Zs}\n\r]/) || bfr.empty?
  afr_white = afr.match?(/[\p{Zs}\n\r]/) || afr.empty?
  bfr_symbol = bfr.match?(/[\p{P}\p{S}]/)
  afr_symbol = afr.match?(/[\p{P}\p{S}]/)
  !afr_white && (!afr_symbol || (afr_symbol && (bfr_symbol || bfr_white)))
end

.reverse_walk(backlog, **_doc) ⇒ Object



1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
# File 'lib/mmmd/blankshell.rb', line 1373

def self.reverse_walk(backlog, **_doc)
  until backlog.last.first.empty?
    capture = []
    before = []
    closer = backlog.last
    star = closer.first.match?(/^\*+$/)
    open = true
    backlog[..-2].reverse_each do |blk|
      open = false if blk.is_a?(Array) && blk[2] == :open &&
                      blk.first.match?(/^\*+$/) == star &&
                      blk[1] == self &&
                      ((blk.first.length + closer.first.length) % 3 != 0 ||
                         ((blk.first.length % 3).zero? &&
                          (closer.first.length % 3).zero?))
      (open ? capture : before).prepend(blk)
      next unless blk.is_a?(Array)
      return backlog unless blk[1].check_contents(capture)
    end
    return backlog if open

    opener = before[-1]
    strong = if closer.first.length > 1 && opener.first.length > 1
               # Strong emphasis
               closer[0] = closer.first[2..]
               opener[0] = opener.first[2..]
               true
             else
               # Emphasis
               closer[0] = closer.first[1..]
               opener[0] = opener.first[1..]
               false
             end
    before = before[..-2] if opener.first.empty?
    backlog = before + [build_emph(capture, strong)] + [closer]
  end
  backlog
end

.right_token?(bfr, _token, afr) ⇒ Boolean

Is this token, given these surrounding characters, reft-flanking?

Parameters:

  • bfr (String)
  • token (String)
  • afr (String)

Returns:

  • (Boolean)


1340
1341
1342
1343
1344
1345
1346
# File 'lib/mmmd/blankshell.rb', line 1340

def self.right_token?(bfr, _token, afr)
  bfr_white = bfr.match?(/[\p{Z}\n\r]/) || bfr.empty?
  afr_white = afr.match?(/[\p{Z}\n\r]/) || afr.empty?
  bfr_symbol = bfr.match?(/[\p{P}\p{S}]/)
  afr_symbol = afr.match?(/[\p{P}\p{S}]/)
  !bfr_white && (!bfr_symbol || (bfr_symbol && (afr_symbol || afr_white)))
end

.tokenize(string) ⇒ Object



1314
1315
1316
1317
1318
1319
1320
1321
1322
# File 'lib/mmmd/blankshell.rb', line 1314

def self.tokenize(string)
  iterate_tokens(string, /(?:_++|\*++)/) do |bfr, text, matched|
    token, afr = text.match(/^(_++|\*++)(.?)/)[1..2]
    left = left_token?(bfr[-1] || "", token, afr)
    right = right_token?(bfr[-1] || "", token, afr)
    break_into_elements(token, [bfr[-1] || "", token, afr].join(''),
                        left, right, matched)
  end
end