Sha256: fd5cf5a18e581001a1360f072f65928b393fe938c3988c6ecc3f4523aac3c678
Contents?: true
Size: 1.88 KB
Versions: 4
Compression:
Stored size: 1.88 KB
Contents
require_relative 'processor' require 'ssmd/annotations' module SSMD::Processors class AnnotationProcessor < Processor attr_reader :text, :annotations def result @text, annotations_text = match.captures if annotations_text @annotations = combine_annotations parse_annotations(annotations_text) @annotations.inject(text) do |text, a| a.wrap(text) end end end def self.annotations a = SSMD::Annotations [a::LanguageAnnotation, a::PhonemeAnnotation] end def ok? !@text.nil? && Array(@annotations).size > 0 end def error? !ok? end private def parse_annotations(text) text.split(/, ?/).flat_map do |annotation_text| annotation = find_annotation annotation_text if annotation.nil? @warnings.push "Unknown annotation: #{text}" end [annotation].compact end end def find_annotation(text) self.class.annotations.lazy .map { |a| a.try text } .find { |a| !a.nil? } end def combine_annotations(annotations) annotations .group_by { |a| a.class } .values .map { |as| as.reduce { |a, b| a.combine b } } end ## # Matches explicitly annotated sections. # For example: # # [Guardians of the Galaxy](en-GB, v: +4dB, p: -3%) def regex %r{ \[ # opening text ([^\]]+) # annotated text \] # closing text \( # opening annotations ((?: (?: #{annotations_regex} )(?:,\s?)? )+) \) # closing annotations }x end def annotations_regex self.class.annotations.map(&:regex).join("|") end end end
Version data entries
4 entries across 4 versions & 1 rubygems