Class: Soywiki::Expander
- Inherits:
-
Object
- Object
- Soywiki::Expander
- Includes:
- PathHelper
- Defined in:
- lib/soywiki/expander.rb
Constant Summary collapse
- WIKI_LINK_PATTERN =
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
/^\s*([a-z0-9]\w+\.)?[A-Z][a-z]+[A-Z0-9]\w*\s*$/
Instance Attribute Summary collapse
-
#expanded_text ⇒ Object
readonly
Returns the value of attribute expanded_text.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#processed_files ⇒ Object
readonly
Returns the value of attribute processed_files.
-
#repo_path ⇒ Object
readonly
Returns the value of attribute repo_path.
Instance Method Summary collapse
- #divider ⇒ Object
- #expand ⇒ Object
- #indent(text, level) ⇒ Object
-
#initialize(repo_path, mode, file) ⇒ Expander
constructor
A new instance of Expander.
- #recursive_expand(file_path, name, level = 0) ⇒ Object
- #register_in_expansion(text, inline = false) ⇒ Object
- #seamful? ⇒ Boolean
- #seamless? ⇒ Boolean
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_text ⇒ Object (readonly)
Returns the value of attribute expanded_text.
11 12 13 |
# File 'lib/soywiki/expander.rb', line 11 def @expanded_text end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
9 10 11 |
# File 'lib/soywiki/expander.rb', line 9 def file @file end |
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
10 11 12 |
# File 'lib/soywiki/expander.rb', line 10 def file_path @file_path end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
9 10 11 |
# File 'lib/soywiki/expander.rb', line 9 def mode @mode end |
#processed_files ⇒ Object (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_path ⇒ Object (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
#divider ⇒ Object
37 38 39 |
# File 'lib/soywiki/expander.rb', line 37 def divider '+' + '-' * 78 + '+' end |
#expand ⇒ Object
80 81 82 83 |
# File 'lib/soywiki/expander.rb', line 80 def (file_path, file.to_page_title) 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 (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) (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
27 28 29 |
# File 'lib/soywiki/expander.rb', line 27 def seamful? mode == 'seamful' end |
#seamless? ⇒ Boolean
23 24 25 |
# File 'lib/soywiki/expander.rb', line 23 def seamless? mode == 'seamless' end |