Class: Soywiki::Expander

Inherits:
Object
  • Object
show all
Includes:
PathHelper
Defined in:
lib/soywiki/expander.rb

Constant Summary collapse

/^\s*([a-z0-9]\w+\.)?[A-Z][a-z]+[A-Z0-9]\w*\s*$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PathHelper

#ensure_path, #in_repo, #repo_path=, #repo_relative

Constructor Details

#initialize(repo_path, mode, file) ⇒ Expander

Returns a new instance of Expander.



15
16
17
18
19
20
21
# File 'lib/soywiki/expander.rb', line 15

def initialize(repo_path, mode, file)
  @repo_path = ensure_path(repo_path)
  @mode = mode
  @file_path = ensure_path(file)
  @file = repo_relative(file).to_s
  @processed_files = []
end

Instance Attribute Details

#expanded_textObject (readonly)

Returns the value of attribute expanded_text.



11
12
13
# File 'lib/soywiki/expander.rb', line 11

def expanded_text
  @expanded_text
end

#fileObject (readonly)

Returns the value of attribute file.



9
10
11
# File 'lib/soywiki/expander.rb', line 9

def file
  @file
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



10
11
12
# File 'lib/soywiki/expander.rb', line 10

def file_path
  @file_path
end

#modeObject (readonly)

Returns the value of attribute mode.



9
10
11
# File 'lib/soywiki/expander.rb', line 9

def mode
  @mode
end

#processed_filesObject (readonly)

Returns the value of attribute processed_files.



9
10
11
# File 'lib/soywiki/expander.rb', line 9

def processed_files
  @processed_files
end

#repo_pathObject (readonly)

Returns the value of attribute repo_path.



10
11
12
# File 'lib/soywiki/expander.rb', line 10

def repo_path
  @repo_path
end

Instance Method Details

#dividerObject



37
38
39
# File 'lib/soywiki/expander.rb', line 37

def divider
  '+' + '-' * 78 + '+'
end

#expandObject



80
81
82
83
# File 'lib/soywiki/expander.rb', line 80

def expand
  recursive_expand(file_path, file.to_page_title)
  expanded_text
end

#indent(text, level) ⇒ Object



31
32
33
34
35
# File 'lib/soywiki/expander.rb', line 31

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

#recursive_expand(file_path, name, level = 0) ⇒ Object



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
# File 'lib/soywiki/expander.rb', line 47

def recursive_expand(file_path, name, level=0)
  processed_files << file_path
  lines = file_path.readlines
  title = lines.shift # takes title
  lines = lines.join.strip.split("\n")
  if seamful?
    register_in_expansion divider unless level == 0
    register_in_expansion indent(title, level)
  end
  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)
        link = [name.namespace, link].join('.')
      end
      link_file_path = in_repo(link.to_file_path)
      if link_file_path.file? && !processed_files.include?(link_file_path)
        recursive_expand(link_file_path, link, level + 1) # recursive call
      elsif processed_files.include?(link_file_path)
        register_in_expansion indent("#{link} [[already expanded]]", level)
      elsif !link_file_path.file?
        register_in_expansion indent("#{link} [[no file found]]", level)
      else
        register_in_expansion indent("#{link}", level)
      end
    else
      register_in_expansion indent(line, level)
    end
  end
  register_in_expansion divider if seamful? && level != 0
end

#register_in_expansion(text, inline = false) ⇒ Object



41
42
43
44
45
# File 'lib/soywiki/expander.rb', line 41

def register_in_expansion(text, inline=false)
  @expanded_text ||= ''
  full_text = inline ? text : text + "\n"
  @expanded_text << full_text
end

#seamful?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/soywiki/expander.rb', line 27

def seamful?
  mode == 'seamful'
end

#seamless?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/soywiki/expander.rb', line 23

def seamless?
  mode == 'seamless'
end