Class: CodeInclusion::Args
- Inherits:
-
Object
- Object
- CodeInclusion::Args
- Defined in:
- lib/polytexnic/code_inclusion.rb
Overview
Converts the CodeInclusion line to a set of arguments, i.e.: ‘%= <<(file.rb, git: repo_path/.git, tag: 1.0, lang: tex, options: “hl_lines”: [5]))’ becomes:
Args#retrieval
{ filename: "file.rb",
line_numbers: "6-14,37",
git: { tag: '1.0', repo: 'repo_path/.git' }
}
Args#render
{ custom_language: "tex",
highlight: ', options: "hl_lines": [5])'}
and ‘%= <<(file.rb, git: 1.0’ becomes:
Args#retrieval
{ filename: "file.rb",
section: "section_x",
git: { tag: '1.0'}
}
Args#render
{ }
Notes for retrieval args
filename is required
line_numbers/section names are specified in [] following the filename,
foo.rb[1,4-6,8,14] or foo.rb[section_x]
Thus, line_numbers and section names are mutually exclusive.
keyword git: is optional and within it
keyword tag: is optional
If present tag refers to an existing tag in the repo.
Tags may optionally quoted.
keyword repo: is optional
If present it contains the full path to a git repo,
full/path/to/repo/.git
If absent, git commands default to the current repository.
Repos may be optionally quoted.
TODO we’re dying for a real parser here.
Constant Summary collapse
- CODE_REGEX =
/^\s*%=\s+<<\s*\( # opening \s*([^\s]+?) # path to file (?:\[(.+?)\])? # optional section, line numbers (?:,\s*git:\s*([ ,"'\w\.\/\-\{\}:]+?))? # optional git tag, repo (?:,\s*lang:\s*(\w+))? # optional lang (,\s*options:\s*.*)? # optional options \s*\) # closing paren /x
Instance Attribute Summary collapse
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#match ⇒ Object
readonly
Returns the value of attribute match.
-
#render ⇒ Object
readonly
Returns the value of attribute render.
-
#retrieval ⇒ Object
readonly
Returns the value of attribute retrieval.
Instance Method Summary collapse
- #code_inclusion_line? ⇒ Boolean
- #extract ⇒ Object
- #extract_git(git_args) ⇒ Object
- #extract_git_option(keyword, args) ⇒ Object
-
#finish ⇒ Object
comma or } (with optional leading space) ends things.
- #git_option_regex(keyword) ⇒ Object
-
#initialize(input) ⇒ Args
constructor
A new instance of Args.
- #parse ⇒ Object
-
#quote ⇒ Object
single or double quotes are optional but permitted.
- #space ⇒ Object
- #specifies_line_numbers? ⇒ Boolean
- #specifies_section? ⇒ Boolean
- #start ⇒ Object
- #whitespace_digits_dashes_and_commas ⇒ Object
Constructor Details
#initialize(input) ⇒ Args
Returns a new instance of Args.
64 65 66 67 68 69 70 71 |
# File 'lib/polytexnic/code_inclusion.rb', line 64 def initialize(input) @input = input @retrieval = {} @render = {} @match = parse extract end |
Instance Attribute Details
#input ⇒ Object (readonly)
Returns the value of attribute input.
62 63 64 |
# File 'lib/polytexnic/code_inclusion.rb', line 62 def input @input end |
#match ⇒ Object (readonly)
Returns the value of attribute match.
62 63 64 |
# File 'lib/polytexnic/code_inclusion.rb', line 62 def match @match end |
#render ⇒ Object (readonly)
Returns the value of attribute render.
62 63 64 |
# File 'lib/polytexnic/code_inclusion.rb', line 62 def render @render end |
#retrieval ⇒ Object (readonly)
Returns the value of attribute retrieval.
62 63 64 |
# File 'lib/polytexnic/code_inclusion.rb', line 62 def retrieval @retrieval end |
Instance Method Details
#code_inclusion_line? ⇒ Boolean
94 95 96 |
# File 'lib/polytexnic/code_inclusion.rb', line 94 def code_inclusion_line? !match.nil? end |
#extract ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/polytexnic/code_inclusion.rb', line 77 def extract return unless code_inclusion_line? retrieval[:filename] = match[1] if specifies_line_numbers? retrieval[:line_numbers] = match[2] elsif specifies_section? retrieval[:section] = match[2] end retrieval[:git] = extract_git(match[3]) if match[3] render[:custom_language] = match[4] render[:highlight] = match[5] end |
#extract_git(git_args) ⇒ Object
106 107 108 109 |
# File 'lib/polytexnic/code_inclusion.rb', line 106 def extract_git(git_args) { tag: extract_git_option('tag', git_args), repo: extract_git_option('repo', git_args)} end |
#extract_git_option(keyword, args) ⇒ Object
111 112 113 114 115 |
# File 'lib/polytexnic/code_inclusion.rb', line 111 def extract_git_option(keyword, args) if (match = git_option_regex(keyword).match(args)) match[1] end end |
#finish ⇒ Object
comma or } (with optional leading space) ends things
135 136 137 |
# File 'lib/polytexnic/code_inclusion.rb', line 135 def finish "\s?(,|})" end |
#git_option_regex(keyword) ⇒ Object
117 118 119 |
# File 'lib/polytexnic/code_inclusion.rb', line 117 def git_option_regex(keyword) /#{start}#{keyword}:#{space}#{quote}(.*?)#{quote}#{finish}/ end |
#parse ⇒ Object
73 74 75 |
# File 'lib/polytexnic/code_inclusion.rb', line 73 def parse CODE_REGEX.match(input) end |
#quote ⇒ Object
single or double quotes are optional but permitted
130 131 132 |
# File 'lib/polytexnic/code_inclusion.rb', line 130 def quote %q[(?:"|')?] end |
#space ⇒ Object
125 126 127 |
# File 'lib/polytexnic/code_inclusion.rb', line 125 def space "\s?" end |
#specifies_line_numbers? ⇒ Boolean
102 103 104 |
# File 'lib/polytexnic/code_inclusion.rb', line 102 def specifies_line_numbers? whitespace_digits_dashes_and_commas.match(match[2]) end |
#specifies_section? ⇒ Boolean
98 99 100 |
# File 'lib/polytexnic/code_inclusion.rb', line 98 def specifies_section? match[2] && !specifies_line_numbers? end |
#start ⇒ Object
121 122 123 |
# File 'lib/polytexnic/code_inclusion.rb', line 121 def start "\.*?" end |
#whitespace_digits_dashes_and_commas ⇒ Object
139 140 141 |
# File 'lib/polytexnic/code_inclusion.rb', line 139 def whitespace_digits_dashes_and_commas /^\s*\d[-,\d\s]*$/ end |