Sha256: 630e6b1d22f7ac522f7e437b34a60873d3600b283e08a4551f93cb84d071a591

Contents?: true

Size: 1.59 KB

Versions: 57

Compression:

Stored size: 1.59 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*\s*$/

PROCESSED_FILES = []

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

end

def divider
  puts '+' + '-' * 78 + '+'
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'
          divider
        end
        expand(link.to_file_path, mode, level + 1) # recursive call
        if mode == 'seamful'
          divider
        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

57 entries across 57 versions & 1 rubygems

Version Path
soywiki-0.9.0 bin/soywiki-expand
soywiki-0.8.5 bin/soywiki-expand
soywiki-0.8.4 bin/soywiki-expand
soywiki-0.8.2 bin/soywiki-expand
soywiki-0.8.0 bin/soywiki-expand
soywiki-0.7.9 bin/soywiki-expand
soywiki-0.7.8 bin/soywiki-expand
soywiki-0.7.7 bin/soywiki-expand
soywiki-0.7.6 bin/soywiki-expand
soywiki-0.7.5 bin/soywiki-expand
soywiki-0.7.4 bin/soywiki-expand
soywiki-0.7.3 bin/soywiki-expand
soywiki-0.7.2 bin/soywiki-expand
soywiki-0.7.1 bin/soywiki-expand
soywiki-0.7.0 bin/soywiki-expand
soywiki-0.6.7 bin/soywiki-expand
soywiki-0.6.6 bin/soywiki-expand
soywiki-0.6.5 bin/soywiki-expand
soywiki-0.6.4 bin/soywiki-expand
soywiki-0.6.3 bin/soywiki-expand