Module: RuVim::Lang::Markdown

Defined in:
lib/ruvim/lang/markdown.rb

Defined Under Namespace

Classes: FenceState

Constant Summary collapse

HEADING_RE =

--- Regex patterns ---

/\A(\s*)(\#{1,6})\s/
FENCE_RE =
/\A(`{3,}|~{3,})/
HR_RE =
/\A(\-{3,}|\*{3,}|_{3,})\s*\z/
BLOCK_QUOTE_RE =
/\A\s*> /
TABLE_LINE_RE =
/\A\s*\|.*\|\s*\z/
TABLE_SEPARATOR_RE =
/\A\|[\s\-:|]+\|\z/
BOLD_RE =
/\*\*([^*]+)\*\*/
ITALIC_RE =
/(?<!\*)\*([^*]+)\*(?!\*)/
INLINE_CODE_RE =
/`([^`]+)`/
/\[([^\]]+)\]\(([^)]+)\)/
CHECKBOX_CHECKED_RE =
/^(\s*-\s*)\[x\]/
CHECKBOX_UNCHECKED_RE =
/^(\s*-\s*)\[ \]/
HEADING_COLORS =

--- Heading styles (for color_columns) ---

{
  1 => "\e[1;33m",   # bold yellow
  2 => "\e[1;36m",   # bold cyan
  3 => "\e[1;32m",   # bold green
  4 => "\e[1;35m",   # bold magenta
  5 => "\e[1;34m",   # bold blue
  6 => "\e[1;90m"    # bold dim
}.freeze

Class Method Summary collapse

Class Method Details

.block_quote?(line) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/ruvim/lang/markdown.rb', line 96

def block_quote?(line)
  line.to_s.match?(BLOCK_QUOTE_RE)
end

.color_columns(text) ⇒ Object

--- Syntax highlight: color_columns ---



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ruvim/lang/markdown.rb', line 117

def color_columns(text)
  cols = {}
  return cols if text.nil? || text.empty?

  stripped = text.strip

  # Fence line: entire line dim
  if fence_line?(stripped)
    fill_line(cols, text, "\e[90m")
    return cols
  end

  # HR: entire line dim
  if horizontal_rule?(stripped)
    fill_line(cols, text, "\e[90m")
    return cols
  end

  # Heading: entire line colored by level
  if (m = text.match(HEADING_RE))
    level = m[2].length
    color = HEADING_COLORS[level] || HEADING_COLORS[6]
    fill_line(cols, text, color)
    return cols
  end

  # Block quote marker
  if (m = text.match(/\A(\s*>)/))
    Highlighter.apply_regex(cols, text, /\A\s*>/, "\e[36m")
  end

  # Inline elements
  Highlighter.apply_regex(cols, text, CHECKBOX_CHECKED_RE, "\e[32m")
  Highlighter.apply_regex(cols, text, CHECKBOX_UNCHECKED_RE, "\e[90m")
  Highlighter.apply_regex(cols, text, BOLD_RE, "\e[1m")
  Highlighter.apply_regex(cols, text, ITALIC_RE, "\e[3m")
  Highlighter.apply_regex(cols, text, INLINE_CODE_RE, "\e[33m")
  Highlighter.apply_regex(cols, text, LINK_RE, "\e[4m")

  cols
end

.fence_line?(stripped) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/ruvim/lang/markdown.rb', line 88

def fence_line?(stripped)
  stripped.to_s.match?(FENCE_RE)
end

.fill_line(cols, text, color) ⇒ Object



161
162
163
# File 'lib/ruvim/lang/markdown.rb', line 161

def self.fill_line(cols, text, color)
  text.length.times { |i| cols[i] = color }
end

.heading_level(line) ⇒ Object



83
84
85
86
# File 'lib/ruvim/lang/markdown.rb', line 83

def heading_level(line)
  m = line.to_s.match(HEADING_RE)
  m ? m[2].length : 0
end

.horizontal_rule?(stripped) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/ruvim/lang/markdown.rb', line 92

def horizontal_rule?(stripped)
  stripped.to_s.match?(HR_RE)
end

.parse_table_cells(line) ⇒ Object



109
110
111
112
113
# File 'lib/ruvim/lang/markdown.rb', line 109

def parse_table_cells(line)
  stripped = line.to_s.strip
  inner = stripped[1...-1] || ""
  inner.split("|", -1).map(&:strip)
end

.table_line?(line) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
# File 'lib/ruvim/lang/markdown.rb', line 100

def table_line?(line)
  stripped = line.to_s.strip
  stripped.start_with?("|") && stripped.end_with?("|") && stripped.length > 1
end

.table_separator?(stripped) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/ruvim/lang/markdown.rb', line 105

def table_separator?(stripped)
  stripped.to_s.match?(TABLE_SEPARATOR_RE)
end