Skip to content

Commit 050d441

Browse files
authored
Merge pull request #79 from revbayes/checked_snippet
Add a snippet tag that checks the snippet against a file.
2 parents 862df17 + e6e1924 commit 050d441

File tree

11 files changed

+80
-16
lines changed

11 files changed

+80
-16
lines changed

_plugins/snippet.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
module Liquid
2+
class Snippet < Block
3+
def initialize(tag_name, markup, options)
4+
super
5+
@filename = markup.strip
6+
end
7+
8+
def render(context)
9+
code_block = super.strip
10+
code_lines = code_block.lines.map{ |l| l.strip }
11+
12+
# Site source directory (so we don't need absolute paths)
13+
current_page_path = context.registers[:page]["path"] # e.g., "docs/example.md"
14+
page_dir = File.dirname(current_page_path) # => "docs"
15+
16+
# Resolve snippet file relative to the document
17+
site_source = context.registers[:site].source
18+
file_path = File.expand_path(File.join(site_source, page_dir, @filename))
19+
20+
unless File.exist?(file_path)
21+
# return "❌ Error: File '#{@filename}' not found at #{file_path}."
22+
raise IOError, "❌ Error: File '#{@filename}' not found at #{file_path}."
23+
end
24+
25+
file_lines = File.readlines(file_path).map{ |l| l.strip }
26+
27+
# Search for exact match of code_lines in file_lines
28+
match_found = false
29+
(0..(file_lines.length - code_lines.length)).each do |start_idx|
30+
if file_lines[start_idx, code_lines.length] == code_lines
31+
match_found = true
32+
break
33+
end
34+
end
35+
36+
unless match_found
37+
# return "❌ Error: Code block not found in file `#{@filename}`:\n\n#{code_block}"
38+
raise RuntimeError, "❌ Error: Code block not found in file `#{@filename}`:\n\n#{code_block}"
39+
end
40+
41+
<<~MARKDOWN
42+
43+
44+
```
45+
#{code_block}
46+
```
47+
48+
MARKDOWN
49+
end
50+
end
51+
end
52+
53+
Liquid::Template.register_tag('snippet', Liquid::Snippet)

tutorials/biogeo/tests.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ run_simple.Rev
22
run_simple_phy.Rev
33
run_epoch.Rev
44
run_epoch_phy.Rev
5-
mcmc_primates_bg_simple.Rev
5+

tutorials/chromo/index.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -281,25 +281,29 @@ model, run the MCMC analysis, and summarize the results.
281281
First, we'll read in the phylogeny. In this example the phylogeny is
282282
assumed known. In further examples we'll jointly estimate chromosome
283283
evolution and the phylogeny.
284-
```
284+
285+
{% snippet scripts/ChromEvol_simple.Rev %}
285286
phylogeny <- readBranchLengthTrees("data/aristolochia.tree")[1]
286-
```
287+
{% endsnippet %}
288+
287289
We need to limit the maximum number of chromosomes allowed in our model,
288290
so here we use the largest observed chromosome count plus 10. This is an
289291
arbitrary limit on the size of the state space that could be increased
290292
if necessary.
291-
```
293+
294+
{% snippet scripts/ChromEvol_simple.Rev %}
292295
max_chromo = 26
293-
```
296+
{% endsnippet %}
297+
294298
Now we get the observed chromosome counts from a tab-delimited file.
295-
```
296-
chromo_data = readCharacterDataDelimited("data/aristolochia_chromosome_counts.tsv", stateLabels=(max_chromo + 1), type="NaturalNumbers", delimiter="\t", headers=FALSE)
297-
```
299+
{% snippet scripts/ChromEvol_simple.Rev %}
300+
chromo_data = readCharacterDataDelimited("data/aristolochia_chromosome_counts.tsv", stateLabels=(max_chromo + 1), type="NaturalNumbers", delimiter="\t", header=FALSE)
301+
{% endsnippet %}
298302
Finally, we initialize a variable for our vector of moves and monitors.
299-
```
303+
{% snippet scripts/ChromEvol_simple.Rev %}
300304
moves = VectorMoves()
301305
monitors = VectorMonitors()
302-
```
306+
{% endsnippet %}
303307

304308
### The Chromosome Evolution Model
305309

tutorials/chromo/scripts/BiChroM.Rev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ phylogeny <- readBranchLengthTrees("data/aristolochia.tree")[1]
5252
max_chromo = 26
5353

5454
# Get the chromosome counts from a tab-delimited file.
55-
chromo_data = readCharacterDataDelimited("data/aristolochia_bichrom_counts.tsv", stateLabels=2*(max_chromo + 1), type="NaturalNumbers", delimiter="\t", headers=FALSE)
55+
chromo_data = readCharacterDataDelimited("data/aristolochia_bichrom_counts.tsv", stateLabels=2*(max_chromo + 1), type="NaturalNumbers", delimiter="\t", header=FALSE)
5656

5757

5858
#########################

tutorials/chromo/scripts/ChromEvol_clado.Rev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ phylogeny <- readBranchLengthTrees("data/aristolochia.tree")[1]
3737
max_chromo = 26
3838

3939
# Get the chromosome counts from a tab-delimited file.
40-
chromo_data = readCharacterDataDelimited("data/aristolochia_chromosome_counts.tsv", stateLabels=(max_chromo + 1), type="NaturalNumbers", delimiter="\t", headers=FALSE)
40+
chromo_data = readCharacterDataDelimited("data/aristolochia_chromosome_counts.tsv", stateLabels=(max_chromo + 1), type="NaturalNumbers", delimiter="\t", header=FALSE)
4141

4242

4343
#########################

tutorials/chromo/scripts/ChromEvol_joint.Rev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ monitors = VectorMonitors()
3535
max_chromo = 26
3636

3737
# Get the chromosome counts from a tab-delimited file.
38-
chromo_data = readCharacterDataDelimited("data/aristolochia_chromosome_counts.tsv", stateLabels=(max_chromo + 1), type="NaturalNumbers", delimiter="\t", headers=FALSE)
38+
chromo_data = readCharacterDataDelimited("data/aristolochia_chromosome_counts.tsv", stateLabels=(max_chromo + 1), type="NaturalNumbers", delimiter="\t", header=FALSE)
3939

4040
# Get the DNA sequence alignment for the matK chloroplast gene.
4141
dna_seq = readDiscreteCharacterData("data/aristolochia_matK.fasta")

tutorials/chromo/scripts/ChromEvol_simmap.Rev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ phylogeny <- readBranchLengthTrees("data/aristolochia.tree")[1]
3434
max_chromo = 26
3535

3636
# Get the chromosome counts from a tab-delimited file.
37-
chromo_data = readCharacterDataDelimited("data/aristolochia_chromosome_counts.tsv", stateLabels=(max_chromo + 1), type="NaturalNumbers", delimiter="\t", headers=FALSE)
37+
chromo_data = readCharacterDataDelimited("data/aristolochia_chromosome_counts.tsv", stateLabels=(max_chromo + 1), type="NaturalNumbers", delimiter="\t", header=FALSE)
3838

3939

4040
#########################

tutorials/chromo/scripts/ChromEvol_simple.Rev

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ phylogeny <- readBranchLengthTrees("data/aristolochia.tree")[1]
3535
max_chromo = 26
3636

3737
# Get the chromosome counts from a tab-delimited file.
38-
chromo_data = readCharacterDataDelimited("data/aristolochia_chromosome_counts.tsv", stateLabels=(max_chromo + 1), type="NaturalNumbers", delimiter="\t", headers=FALSE)
38+
chromo_data = readCharacterDataDelimited("data/aristolochia_chromosome_counts.tsv", stateLabels=(max_chromo + 1), type="NaturalNumbers", delimiter="\t", header=FALSE)
3939

4040

4141
#########################
@@ -133,3 +133,4 @@ ancestralStateTree(phylogeny, anc_state_trace, "output/ChromEvol_simple_final.tr
133133
# using the RevGadgets R package.
134134
#
135135
#########################################################################################################
136+
q()

tutorials/chromo/scripts/ChromoSSE_simple.Rev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ phylogeny <- readTrees("data/aristolochia-bd.tree")[1]
3535
max_chromo = 26
3636

3737
# Get the chromosome counts from a tab-delimited file.
38-
chromo_data = readCharacterDataDelimited("data/aristolochia_chromosome_counts.tsv", stateLabels=(max_chromo + 1), type="NaturalNumbers", delimiter="\t", headers=FALSE)
38+
chromo_data = readCharacterDataDelimited("data/aristolochia_chromosome_counts.tsv", stateLabels=(max_chromo + 1), type="NaturalNumbers", delimiter="\t", header=FALSE)
3939

4040

4141
#########################

tutorials/chromo/tests.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ChromEvol_simple.Rev

0 commit comments

Comments
 (0)