Sha256: b458a2d3e3c1b6786f9c2c4a493f32f781a043a3d8971375fd950f5b9541c319

Contents?: true

Size: 1.55 KB

Versions: 6

Compression:

Stored size: 1.55 KB

Contents

#!/usr/bin/env ruby
require 'soywiki'

# Takes any wiki link that stands alone on a line and expands it

# this is different from Soywiki::WIKI_WORD in that it requires ^\s* before the
# first letter

WIKI_LINK_PATTERN =  /^\s*([a-z]\w+\.)?[A-Z][a-z]+[A-Z]\w*/

PROCESSED_FILES = []

def indent(text, level, mode)
  return text if mode == 'seamless'
  return text if level == 0
  (':' * level) + ' ' +  text

end

def expand(file, mode, level=0)
  PROCESSED_FILES << file
  lines = File.readlines(file)
  if mode == 'seamless'
    lines.shift   # strips title
  end
  lines = lines.join.strip.split("\n")
  lines.each do |line|
    # note that the wiki link must be alone on the line to be expanded
    if line =~ WIKI_LINK_PATTERN 
      link = line.strip
      if link =~ /(\A|\s)[A-Z]/ # short link in namespace (relative link)
        namespace = file.namespace
        link = [namespace, link].join('.')
      end
      if File.file?(link.to_file_path) && !PROCESSED_FILES.include?(link.to_file_path) 
        if mode == 'seamful'
          puts '.' * 10
        end
        expand(link.to_file_path, mode, level + 1) # recursive call
        if mode == 'seamful'
          puts '.' * 10
        end
      elsif PROCESSED_FILES.include?(link) 
        puts indent("#{link} [[already expanded]]", level, mode)
      elsif !File.file?(link.to_file_path)
        puts indent("#{link} [[no file found]]", level, mode)
      else
        puts indent("#{link}", level, mode)
      end
    else
      puts indent(line, level, mode)
    end
  end
end

mode, file = *ARGV

expand(file, mode)


Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
soywiki-0.1.9 bin/soywiki-expand
soywiki-0.1.6 bin/soywiki-expand
soywiki-0.1.5 bin/soywiki-expand
soywiki-0.1.4 bin/soywiki-expand
soywiki-0.1.3 bin/soywiki-expand
soywiki-0.1.2 bin/soywiki-expand