module Asciidoctor::Substitutors
def resolve_lines_to_highlight source, spec, start = nil
start - The line number of the first line (optional, default: false)
spec - The lines specifier (e.g., "1-5, !2, 10" or "1..5;!2;10")
source - The String source.
e.g., highlight="1-5, !2, 10" or highlight=1-5;!2,10
Public: Resolve the line numbers in the specified source to highlight from the provided spec.
def resolve_lines_to_highlight source, spec, start = nil lines = [] spec = spec.delete ' ' if spec.include? ' ' ((spec.include? ',') ? (spec.split ',') : (spec.split ';')).map do |entry| if entry.start_with? '!' entry = entry.slice 1, entry.length negate = true end if (delim = (entry.include? '..') ? '..' : ((entry.include? '-') ? '-' : nil)) from, _, to = entry.partition delim to = (source.count LF) + 1 if to.empty? || (to = to.to_i) < 0 if negate lines -= (from.to_i..to).to_a else lines |= (from.to_i..to).to_a end elsif negate lines.delete entry.to_i elsif !lines.include?(line = entry.to_i) lines << line end end # If the start attribute is defined, then the lines to highlight specified by the provided spec should be relative to the start value. unless (shift = start ? start - 1 : 0) == 0 lines = lines.map {|it| it - shift } end lines.sort end