Module: MarkdownRecord::PathUtilities
- Included in:
- Association, Associations, ContentAssociations, ContentFragment, FileSaver, JsonRenderer, ModelInflator, Rendering::Nodes::HtmlBase, Rendering::Nodes::JsonBase, Validator
- Defined in:
- lib/markdown_record/path_utilities.rb
Instance Method Summary collapse
- #base_content_root_name ⇒ Object
- #base_rendered_path ⇒ Object
- #base_rendered_root ⇒ Object
- #clean_path(path) ⇒ Object
- #erb_locals_from_path(full_path) ⇒ Object
- #fragment_attributes_from_path(full_path) ⇒ Object
- #full_path_to_parts(full_path) ⇒ Object
- #path_to_fragment_id(full_path) ⇒ Object
- #remove_prefix(filename_or_id) ⇒ Object
- #rendered_path(full_path) ⇒ Object
- #scoped_id_to_parts(scoped_id) ⇒ Object
- #to_scoped_id(scope, id) ⇒ Object
Instance Method Details
#base_content_root_name ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/markdown_record/path_utilities.rb', line 38 def base_content_root_name basename = ::MarkdownRecord.config.content_root.basename # must use "/" so that `.parent` returns correctly # for top level paths. Pathname.new("/").join(basename) end |
#base_rendered_path ⇒ Object
79 80 81 |
# File 'lib/markdown_record/path_utilities.rb', line 79 def base_rendered_path ::MarkdownRecord.config.rendered_content_root.join(::MarkdownRecord.config.content_root.basename) end |
#base_rendered_root ⇒ Object
83 84 85 |
# File 'lib/markdown_record/path_utilities.rb', line 83 def base_rendered_root clean_path(::MarkdownRecord.config.rendered_content_root.to_s) end |
#clean_path(path) ⇒ Object
75 76 77 |
# File 'lib/markdown_record/path_utilities.rb', line 75 def clean_path(path) path.to_s.gsub("_fragments", "").gsub(/(\.concat|\.md\.erb|\.md|\.json|\.html)/,"").delete_prefix("/") end |
#erb_locals_from_path(full_path) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/markdown_record/path_utilities.rb', line 46 def erb_locals_from_path(full_path) filename, subdirectory = *full_path_to_parts(full_path) frag_id = path_to_fragment_id(full_path) fragment = ::MarkdownRecord::ContentFragment.force_render_find(frag_id) { filename: filename, subdirectory: subdirectory, frag_id: frag_id, fragment: fragment, scope: fragment.__scope__ } end |
#fragment_attributes_from_path(full_path) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/markdown_record/path_utilities.rb', line 62 def fragment_attributes_from_path(full_path) filename, subdirectory = *full_path_to_parts(full_path) frag_id = path_to_fragment_id(full_path) { id: frag_id, type: ::MarkdownRecord::ContentFragment.name.underscore, subdirectory: subdirectory, filename: filename }.stringify_keys end |
#full_path_to_parts(full_path) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/markdown_record/path_utilities.rb', line 6 def full_path_to_parts(full_path) rendered_path = Pathname.new("/").join(full_path) filename = clean_path(rendered_path.basename) filename = remove_prefix(filename) subdirectory = clean_path(rendered_path.parent) subdirectory = remove_prefix(subdirectory) [filename, subdirectory, full_path.to_s.split(".").last] end |
#path_to_fragment_id(full_path) ⇒ Object
24 25 26 |
# File 'lib/markdown_record/path_utilities.rb', line 24 def path_to_fragment_id(full_path) rendered_path(full_path).to_s end |
#remove_prefix(filename_or_id) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/markdown_record/path_utilities.rb', line 87 def remove_prefix(filename_or_id) if MarkdownRecord.config.ignore_numeric_prefix MarkdownRecord.config.filename_sorter.remove_prefix(filename_or_id) else filename_or_id end end |
#rendered_path(full_path) ⇒ Object
18 19 20 21 22 |
# File 'lib/markdown_record/path_utilities.rb', line 18 def rendered_path(full_path) rendered_path = clean_path(full_path.to_s) rendered_path = remove_prefix(rendered_path) Pathname.new(rendered_path) end |
#scoped_id_to_parts(scoped_id) ⇒ Object
34 35 36 |
# File 'lib/markdown_record/path_utilities.rb', line 34 def scoped_id_to_parts(scoped_id) scoped_id.split(":s:") end |
#to_scoped_id(scope, id) ⇒ Object
28 29 30 31 32 |
# File 'lib/markdown_record/path_utilities.rb', line 28 def to_scoped_id(scope, id) return id.to_s unless scope.present? "#{scope}:s:#{id}" end |