Skip to content

Commit

Permalink
Merge pull request #166 from OpenDSA/splice_embed
Browse files Browse the repository at this point in the history
Splice embed
  • Loading branch information
babz007 authored Mar 4, 2024
2 parents 679a0e3 + 1a9f17f commit ec41c5f
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions app/models/inst_book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,18 @@ def validate_configuration

def extract_av_data_from_rst
av_data = {}
lang = JSON.parse(self.options)['lang'] || 'en'
parsed_options = parse_json_options(self.options)
lang = parsed_options['lang'] || 'en'
rst_folder = File.join('public', 'OpenDSA', 'RST', lang)


# Check if rst_folder to prevent Dir.glob from failing
return av_data unless Dir.exist?(rst_folder)

Dir.glob("#{rst_folder}/**/*.rst").each do |rst_file_path|
module_name = File.basename(rst_file_path, ".rst")
av_data[module_name] = { avmetadata: {}, inlineav: [], avembed: [] }
in_metadata_block = false

File.foreach(rst_file_path) do |line|
if line.strip == '.. avmetadata::'
in_metadata_block = true
Expand All @@ -160,9 +164,9 @@ def extract_av_data_from_rst
end
end
end

av_data
end
end

private

Expand All @@ -178,7 +182,14 @@ def extract_inlineav_name_from_line(line)

def extract_avembed_data_from_line(line)
match = line.match(/\.\. avembed:: Exercises\/\w+\/(\w+)\.html/)
match[1] if match # Returns the 3rd level attribbutee or nil if no match
match[1] if match # Returns the 3rd level attribute or nil if no match
end

# helper method for extract_av_data_from_rst(), safely attempts to parse a JSON string, returning an empty hash as fallback
def parse_json_options(json_str)
JSON.parse(json_str)
rescue JSON::ParserError
{} # Return an empty hash as a fallback
end

# --------------------------------------------------------------------------------
Expand Down

0 comments on commit ec41c5f

Please sign in to comment.