<commit_before># frozen_string_literal: true

module Asciidoctor
  module PDF
    module FormattedText
      class Transform
        LF = ?\n
        ZeroWidthSpace = ?\u200b
        CharEntityTable = { amp: '&', apos: ?', gt: '>', lt: '<', nbsp: ?\u00a0, quot: '"' }
        CharRefRx = /&(?:(#{CharEntityTable.keys.join '|'})|#(?:(\d\d\d{0,4})|x(\h\h\h{0,3})));/
        HexColorRx = /^#\h\h\h\h{0,3}$/
        TextDecorationTable = { 'underline' => :underline, 'line-through' => :strikethrough }
        ThemeKeyToFragmentProperty = {
          'background_color' => :background_color,
          'border_color' => :border_color,
          'border_offset' => :border_offset,
          'border_radius' => :border_radius,
          'border_width' => :border_width,
          'font_color' => :color,
          'font_family' => :font,
          'font_size' => :size,
          'text_decoration_color' => :text_decoration_color,
          'text_decoration_width' => :text_decoration_width,
        }
        #DummyText = ?\u0000

        def initialize options = {}
          @merge_adjacent_text_nodes = options[:merge_adjacent_text_nodes]
          # TODO: add support for character spacing
          if (theme = options[:theme])
            @theme_settings = {
              button: {
                color: theme.button_font_color,
                font: theme.button_font_family,
                size: theme.button_font_size,
                styles: (to_styles theme.button_font_style),
                background_color: (button_bg_color = theme.button_background_color),
                border_width: (button_border_width = theme.button_border_width),
                border_color: button_border_width && (theme.button_border_color || theme.base_border_color),
                border_offset: (button_border_offset = (button_bg_or_border = button_bg_color || button_border_width) && theme.button_border_offset),
                border_radius: button_bg_or_border && theme.button_border_radius,
                align: button_border_offset && :center,
                callback: button_bg_or_border && [TextBackgroundAndBorderRenderer],
              }.compact,
              code: {
                color: theme.codespan_font_color,
                font: theme.codespan_font_family,
                size: theme.codespan_font_size,
                styles: (to_styles theme.codespan_font_style),
                background_color: (mono_bg_color = theme.codespan_background_color),
                border_width: (mono_border_width = theme.codespan_border_width),
                border_color: mono_border_width && (theme.codespan_border_color || theme.base_border_color),
                border_offset: (mono_border_offset = (mono_bg_or_border = mono_bg_color || mono_border_width) && theme.codespan_border_offset),
                border_radius: mono_bg_or_border && theme.codespan_border_radius,
                align: mono_border_offset && :center,
                callback: mono_bg_or_border && [TextBackgroundAndBorderRenderer],
              }.compact,
              kbd: {
                color: theme.kbd_font_color,
                font: theme.kbd_font_family || theme.codespan_font_family,
                size: theme.kbd_font_size,
                styles: (to_styles theme.kbd_font_style),
                background_color: (kbd_bg_color = theme.kbd_background_color),
                border_width: (kbd_border_width = theme.kbd_border_width),
                border_color: kbd_border_width && (theme.kbd_border_color || theme.base_border_color),
                border_offset: (kbd_border_offset = (kbd_bg_or_border = kbd_bg_color || kbd_border_width) && theme.kbd_border_offset),
                border_radius: kbd_bg_or_border && theme.kbd_border_radius,
                align: kbd_border_offset && :center,
                callback: kbd_bg_or_border && [TextBackgroundAndBorderRenderer],
              }.compact,
              link: {
                color: theme.link_font_color,
                font: theme.link_font_family,
                size: theme.link_font_size,
                styles: (to_styles theme.link_font_style, theme.link_text_decoration),
                text_decoration_color: theme.link_text_decoration_color,
                text_decoration_width: theme.link_text_decoration_width,
                background_color: (link_bg_color = theme.link_background_color),
                border_offset: (link_border_offset = link_bg_color && theme.link_border_offset),
                align: link_border_offset && :center,
                callback: link_bg_color && [TextBackgroundAndBorderRenderer],
              }.compact,
              mark: {
                color: theme.mark_font_color,
                styles: (to_styles theme.mark_font_style),
                background_color: (mark_bg_color = theme.mark_background_color),
                border_offset: (mark_border_offset = mark_bg_color && theme.mark_border_offset),
                align: mark_border_offset && :center,
                callback: mark_bg_color && [TextBackgroundAndBorderRenderer],
              }.compact,
              menu: {
                color: theme.menu_font_color,
                font: theme.menu_font_family,
                size: theme.menu_font_size,
                styles: (to_styles theme.menu_font_style),
              }.compact,
            }
            @theme_settings.tap do |accum|
              revise_roles = [].to_set
              theme.each_pair do |key, val|
                next unless (key = key.to_s).start_with? 'role_'
                role, key = (key.slice 5, key.length).split '_', 2
                if (prop = ThemeKeyToFragmentProperty[key])
                  (accum[role] ||= {})[prop] = val
                  if key == 'border_width' && val && !(theme[%(role_#{role}_border_color)])
                    accum[role][:border_color] = theme.base_border_color
                  end
                #elsif key == 'font_kerning'
                #  unless (resolved_val = val == 'none' ? false : (val == 'normal' ? true : nil)).nil?
                #    (accum[role] ||= {})[:kerning] = resolved_val
                #  end
                elsif key == 'font_style' || key == 'text_decoration'
                  revise_roles << role
                end
              end
              revise_roles.each do |role|
                (accum[role] ||= {})[:styles] = to_styles theme[%(role_#{role}_font_style)], theme[%(role_#{role}_text_decoration)]<commit_msg>use more self-explanatory variable name in text transform [no ci]<commit_after># frozen_string_literal: true

module Asciidoctor
  module PDF
    module FormattedText
      class Transform
        LF = ?\n
        ZeroWidthSpace = ?\u200b
        CharEntityTable = { amp: '&', apos: ?', gt: '>', lt: '<', nbsp: ?\u00a0, quot: '"' }
        CharRefRx = /&(?:(#{CharEntityTable.keys.join '|'})|#(?:(\d\d\d{0,4})|x(\h\h\h{0,3})));/
        HexColorRx = /^#\h\h\h\h{0,3}$/
        TextDecorationTable = { 'underline' => :underline, 'line-through' => :strikethrough }
        ThemeKeyToFragmentProperty = {
          'background_color' => :background_color,
          'border_color' => :border_color,
          'border_offset' => :border_offset,
          'border_radius' => :border_radius,
          'border_width' => :border_width,
          'font_color' => :color,
          'font_family' => :font,
          'font_size' => :size,
          'text_decoration_color' => :text_decoration_color,
          'text_decoration_width' => :text_decoration_width,
        }
        #DummyText = ?\u0000

        def initialize options = {}
          @merge_adjacent_text_nodes = options[:merge_adjacent_text_nodes]
          # TODO: add support for character spacing
          if (theme = options[:theme])
            @theme_settings = {
              button: {
                color: theme.button_font_color,
                font: theme.button_font_family,
                size: theme.button_font_size,
                styles: (to_styles theme.button_font_style),
                background_color: (button_bg_color = theme.button_background_color),
                border_width: (button_border_width = theme.button_border_width),
                border_color: button_border_width && (theme.button_border_color || theme.base_border_color),
                border_offset: (button_border_offset = (button_bg_or_border = button_bg_color || button_border_width) && theme.button_border_offset),
                border_radius: button_bg_or_border && theme.button_border_radius,
                align: button_border_offset && :center,
                callback: button_bg_or_border && [TextBackgroundAndBorderRenderer],
              }.compact,
              code: {
                color: theme.codespan_font_color,
                font: theme.codespan_font_family,
                size: theme.codespan_font_size,
                styles: (to_styles theme.codespan_font_style),
                background_color: (mono_bg_color = theme.codespan_background_color),
                border_width: (mono_border_width = theme.codespan_border_width),
                border_color: mono_border_width && (theme.codespan_border_color || theme.base_border_color),
                border_offset: (mono_border_offset = (mono_bg_or_border = mono_bg_color || mono_border_width) && theme.codespan_border_offset),
                border_radius: mono_bg_or_border && theme.codespan_border_radius,
                align: mono_border_offset && :center,
                callback: mono_bg_or_border && [TextBackgroundAndBorderRenderer],
              }.compact,
              kbd: {
                color: theme.kbd_font_color,
                font: theme.kbd_font_family || theme.codespan_font_family,
                size: theme.kbd_font_size,
                styles: (to_styles theme.kbd_font_style),
                background_color: (kbd_bg_color = theme.kbd_background_color),
                border_width: (kbd_border_width = theme.kbd_border_width),
                border_color: kbd_border_width && (theme.kbd_border_color || theme.base_border_color),
                border_offset: (kbd_border_offset = (kbd_bg_or_border = kbd_bg_color || kbd_border_width) && theme.kbd_border_offset),
                border_radius: kbd_bg_or_border && theme.kbd_border_radius,
                align: kbd_border_offset && :center,
                callback: kbd_bg_or_border && [TextBackgroundAndBorderRenderer],
              }.compact,
              link: {
                color: theme.link_font_color,
                font: theme.link_font_family,
                size: theme.link_font_size,
                styles: (to_styles theme.link_font_style, theme.link_text_decoration),
                text_decoration_color: theme.link_text_decoration_color,
                text_decoration_width: theme.link_text_decoration_width,
                background_color: (link_bg_color = theme.link_background_color),
                border_offset: (link_border_offset = link_bg_color && theme.link_border_offset),
                align: link_border_offset && :center,
                callback: link_bg_color && [TextBackgroundAndBorderRenderer],
              }.compact,
              mark: {
                color: theme.mark_font_color,
                styles: (to_styles theme.mark_font_style),
                background_color: (mark_bg_color = theme.mark_background_color),
                border_offset: (mark_border_offset = mark_bg_color && theme.mark_border_offset),
                align: mark_border_offset && :center,
                callback: mark_bg_color && [TextBackgroundAndBorderRenderer],
              }.compact,
              menu: {
                color: theme.menu_font_color,
                font: theme.menu_font_family,
                size: theme.menu_font_size,
                styles: (to_styles theme.menu_font_style),
              }.compact,
            }
            @theme_settings.tap do |accum|
              roles_with_styles = [].to_set
              theme.each_pair do |key, val|
                next unless (key = key.to_s).start_with? 'role_'
                role, key = (key.slice 5, key.length).split '_', 2
                if (prop = ThemeKeyToFragmentProperty[key])
                  (accum[role] ||= {})[prop] = val
                  if key == 'border_width' && val && !(theme[%(role_#{role}_border_color)])
                    accum[role][:border_color] = theme.base_border_color
                  end
                #elsif key == 'font_kerning'
                #  unless (resolved_val = val == 'none' ? false : (val == 'normal' ? true : nil)).nil?
                #    (accum[role] ||= {})[:kerning] = resolved_val
                #  end
                elsif key == 'font_style' || key == 'text_decoration'
                  roles_with_styles << role
                end
              end
              roles_with_styles.each do |role|
                (accum[role] ||= {})[:styles] = to_styles theme[%(role_#{role}_font_style)], theme[%(role_#{role}_text_decoration)]
{'fraction_non_alphanumeric': 0.10425124473381846, 'fraction_numerical': 0.0027575641516660284, 'mean_word_length': 2.1004511992400854, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1077506', 'n_tokens_mistral': 3901, 'n_tokens_neox': 3679, 'n_words': 622}