Module: StickyFlag::Tags::MMD

Defined in:
lib/stickyflag/tags/mmd.rb

Class Method Summary collapse

Class Method Details

.clear(file_name) ⇒ Object



167
168
169
# File 'lib/stickyflag/tags/mmd.rb', line 167

def clear(file_name)
  write_tags(file_name, nil)
end

.extensionsObject



9
10
11
# File 'lib/stickyflag/tags/mmd.rb', line 9

def extensions
  [ '.md', '.mmd', '.markdown', '.text' ]
end

.get(file_name) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
# File 'lib/stickyflag/tags/mmd.rb', line 98

def get(file_name)
  counter = 0
  key = ''
  value = ''

  File.open(file_name, 'r:UTF-8').each_line do |line|
    if counter == 0
      # Check to see if there's any metadata at all
      m = line.match()
      return [] unless m
    
      # Start capturing values
      key = m[1]
      value = m[2]
    else
      # Check to see if the metadata is over, or if there's another
      # key/value pair starting on this line
      m = line.match()
  
      if m || line.strip.empty?
        # We're done capturing this value, check if it's 'tags' (after
        # removing whitespace and lowercasing, as per the spec)
        key.gsub!(/\s/, '')
        key.downcase!
      
        if key == 'tags'
          return [] if value.nil? || value.empty?
          return value.split(',').map { |t| t.empty? ? nil : t.strip }.compact
        end
      end
  
      # Quit if there's no more metadata
      return if line.strip.empty?
  
      # Start grabbing a new key/value if there is one, or else just
      # add to the current value
      if m
        key = m[1]
        value = m[2]
      else
        # This doesn't strip the indentation off of the values, but
        # that's okay, since we always strip whitespace off
        # individual tags
        value << line
      end
    end
  
    counter += 1
  end

  []
end

.metadata_key_regexObject

We duplicate the source-code module here because we want to have cute support for the indentation of tag contents in MMD, and because MMD metadata blocks have slightly weird syntax rules.



17
18
19
# File 'lib/stickyflag/tags/mmd.rb', line 17

def 
  /([A-Za-z0-9][A-Za-z0-9\-_ ]*):\s*(.*)/
end

.set(file_name, tag) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/stickyflag/tags/mmd.rb', line 151

def set(file_name, tag)
  tags = get(file_name)
  return if tags.include? tag

  tags << tag
  write_tags(file_name, tags)
end

.tag_line_for(tags, last_line = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/stickyflag/tags/mmd.rb', line 24

def tag_line_for(tags, last_line = nil)
  padding = ''

  if last_line
    match = last_line.match(/([A-Za-z0-9][A-Za-z0-9\-_ ]*:\s*|\s*).*/)
    if match
      num = match[1].length - 'Tags: '.length
      if num > 0
        padding = ' ' * num
      end
    end
  end

  "Tags: #{padding}#{tags.join(', ')}  "
end

.tag_line_regexObject



20
21
22
# File 'lib/stickyflag/tags/mmd.rb', line 20

def tag_line_regex
  /[Tt]\s*[Aa]\s*[Gg]\s*[Ss]\s*:.*/
end

.unset(file_name, tag) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/stickyflag/tags/mmd.rb', line 159

def unset(file_name, tag)
  tags = get(file_name)
  return unless tags.include? tag

  tags.delete(tag)
  write_tags(file_name, tags)
end

.write_tags(file_name, tags) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/stickyflag/tags/mmd.rb', line 40

def write_tags(file_name, tags)
  set_tags = false
  eating_tags = false
  last_line = ''
  counter = 0

  outpath = File.tmpnam
  File.open(outpath, 'w:UTF-8') do |outfile|
    File.open(file_name, 'r:UTF-8').each_line do |line|
      if counter == 0
        if line !~ 
          # If we're on the first line and there's no metadata at all,
          # then we have to add a metadata block
          outfile.puts tag_line_for(tags) if tags
          outfile.puts ''
          set_tags = true
        end
      else
        # Not on the first line
        if set_tags == true
          # If we've already set tags, this is easy
          outfile.puts line
        else
          # We're somewhere in the metadta, and we've yet to set any
          # tags.
          if line =~ tag_line_regex
            # We want to eat the tag block, which could theoretically
            # be extended onto multiple lines
            eating_tags = true
          elsif eating_tags
            # We're currently eating tags; if this line is blank or a
            # key/value pair, we're done eating tags, time to print
            if line =~  || line.strip.empty?
              eating_tags = false
              outfile.puts tag_line_for(tags, last_line) if tags
              set_tags = true
            end
          else
            # Not eating the tags key, just keep going, checking to see
            # if the metadata block is over
            if line.strip.empty?
              outfile.puts tag_line_for(tags, last_line) if tags
              set_tags = true
            end

            outfile.puts line
          end
        end
      end
    
      counter += 1
      last_line = line
    end
  end

  FileUtils.mv(outpath, file_name)
end