Skip to content

Commit

Permalink
Merge pull request #47 from entangled/issue/46
Browse files Browse the repository at this point in the history
create an alias for code blocks that have both file and id tags; fix #46
  • Loading branch information
jhidding authored Jul 9, 2024
2 parents a0ce301 + fba03c4 commit 2f924b5
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
6 changes: 6 additions & 0 deletions entangled/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,17 @@ class ReferenceMap:
default_factory=lambda: defaultdict(list)
)
targets: set[str] = field(default_factory=set)
alias: dict[str, str] = field(default_factory=dict)

def names(self) -> Iterable[str]:
return self.index.keys()

def by_name(self, n: str) -> Iterable[CodeBlock]:
if n not in self.index and n not in self.alias:
raise AttributeError(name=n, obj=self)
if n not in self.index:
return self.by_name(self.alias[n])

return (self.map[r] for r in self.index[n])

def new_id(self, filename: str, name: str) -> ReferenceId:
Expand Down
6 changes: 6 additions & 0 deletions entangled/markdown_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ def on_close_codeblock(self, m: re.Match):
self.reference_map[ref] = code
if target_file is not None:
self.reference_map.targets.add(target_file)

# when both target_file and block_id are given,
# make an alias
if target_file is not None and block_id is not None:
self.reference_map.alias[target_file] = block_id

self.content.append(ref)
self.current_content = []

Expand Down
3 changes: 3 additions & 0 deletions entangled/tangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ def tangle_ref(
if annotation is None:
annotation = config.annotation

if ref_name in refs.alias:
ref_name = refs.alias[ref_name]

if ref_name not in refs:
raise KeyError(ref_name)
v = _visited or Visitor()
Expand Down
70 changes: 70 additions & 0 deletions test/test_id_and_file_target.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from entangled.markdown_reader import MarkdownReader
from entangled.tangle import tangle_ref, AnnotationMethod

md_in = """
``` {.rust file=test1.rs #blockid1}
fn main() {
println!("Hello, World!");
}
```
``` {.rust #blockid2 file=test2.rs}
fn main() {
println!("Hello, World!");
}
```
``` {.rust #blockid3 file=test3.rs}
fn fac(i: u64) -> u64 {
result: u64 = 1;
for j in 1..i {
result *= j;
}
return result;
}
```
``` {.rust #blockid3}
fn main() {
println!("42! = {}", fac(42));
}
```
"""

result1_out = result2_out = """
fn main() {
println!("Hello, World!");
}
"""

result3_out = """
fn fac(i: u64) -> u64 {
result: u64 = 1;
for j in 1..i {
result *= j;
}
return result;
}
fn main() {
println!("42! = {}", fac(42));
}
"""

def test_id_and_file_target():
mr = MarkdownReader("-")
mr.run(md_in)

content1, _ = tangle_ref(mr.reference_map, "blockid1", AnnotationMethod.NAKED)
assert content1.strip() == result1_out.strip()
content2, _ = tangle_ref(mr.reference_map, "blockid2", AnnotationMethod.NAKED)
assert content2.strip() == result2_out.strip()
content3, _ = tangle_ref(mr.reference_map, "blockid3", AnnotationMethod.NAKED)
assert content3.strip() == result3_out.strip()

assert mr.reference_map.targets == {"test1.rs", "test2.rs", "test3.rs"}
for i in range(1, 4):
ref = f"test{i}.rs"
bid = f"blockid{i}"
c, _ = tangle_ref(mr.reference_map, ref, AnnotationMethod.NAKED)
d, _ = tangle_ref(mr.reference_map, ref, AnnotationMethod.NAKED)
assert c == d

0 comments on commit 2f924b5

Please sign in to comment.