Skip to content

Commit

Permalink
Merge pull request #91 from venikx/master
Browse files Browse the repository at this point in the history
feat(parse): add switches and params for src-block
  • Loading branch information
rasendubi authored Nov 2, 2023
2 parents c47bbd1 + 041eb97 commit a3f058b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/nasty-scissors-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'uniorg-parse': minor
---

Add support for switches and parameters in src-blocks.
59 changes: 59 additions & 0 deletions packages/uniorg-parse/src/__snapshots__/parser.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ children:
- type: "src-block"
affiliated:
NAME: "hi"
switches: null
parameters: null
value: ""
`;
Expand Down Expand Up @@ -94,6 +96,8 @@ children:
affiliated:
NAME: "source"
language: "org"
switches: null
parameters: null
value: "some paragraph\\n"
`;
Expand Down Expand Up @@ -128,6 +132,8 @@ children:
- type: "src-block"
affiliated: {}
language: "org"
switches: null
parameters: null
value: "#+ escaped\\n"
`;
Expand Down Expand Up @@ -158,6 +164,8 @@ children:
- type: "src-block"
affiliated: {}
language: "org"
switches: null
parameters: null
value: ",,* two commas escaped\\n"
`;
Expand All @@ -179,6 +187,8 @@ children:
- type: "src-block"
affiliated: {}
language: "c"
switches: null
parameters: null
value: "*a = 0;\\n"
`;
Expand Down Expand Up @@ -288,6 +298,8 @@ children:
- type: "src-block"
affiliated: {}
language: "org"
switches: null
parameters: null
value: ",# nont escaped\\n"
`;
Expand All @@ -299,6 +311,8 @@ children:
- type: "src-block"
affiliated: {}
language: "org"
switches: null
parameters: null
value: "* not a headline;\\n"
`;
Expand Down Expand Up @@ -370,9 +384,50 @@ contentsEnd: 27
children:
- type: "src-block"
affiliated: {}
switches: null
parameters: null
value: "hello\\n"
`;
exports[`org/parser blocks src block with parameters 1`] = `
type: "org-data"
contentsBegin: 0
contentsEnd: 52
children:
- type: "src-block"
affiliated: {}
language: "js"
switches: null
parameters: ":exports none"
value: "const t = 10\\n"
`;
exports[`org/parser blocks src block with switches 1`] = `
type: "org-data"
contentsBegin: 0
contentsEnd: 44
children:
- type: "src-block"
affiliated: {}
language: "js"
switches: "+n 10"
parameters: null
value: "const t = 10\\n"
`;
exports[`org/parser blocks src block with switches and parameters 1`] = `
type: "org-data"
contentsBegin: 0
contentsEnd: 58
children:
- type: "src-block"
affiliated: {}
language: "js"
switches: "+n 10"
parameters: ":exports none"
value: "const t = 10\\n"
`;
exports[`org/parser blocks src in list 1`] = `
type: "org-data"
contentsBegin: 0
Expand Down Expand Up @@ -402,6 +457,8 @@ children:
value: "example:\\n"
- type: "src-block"
affiliated: {}
switches: null
parameters: null
value: " blah\\n"
`;
Expand Down Expand Up @@ -3200,6 +3257,8 @@ children:
- type: "src-block"
affiliated: {}
language: "c"
switches: null
parameters: null
value: " x\\n\\n\\n y\\n"
`;
Expand Down
24 changes: 24 additions & 0 deletions packages/uniorg-parse/src/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,30 @@ hello
#+end_src`
);

itParses(
'src block with switches',
`
#+begin_src js +n 10
const t = 10
#+end_src`
);

itParses(
'src block with parameters',
`
#+begin_src js :exports none
const t = 10
#+end_src`
);

itParses(
'src block with switches and parameters',
`
#+begin_src js +n 10 :exports none
const t = 10
#+end_src`
);

itParses(
'quote block',
`#+begin_quote
Expand Down
9 changes: 8 additions & 1 deletion packages/uniorg-parse/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,14 @@ class Parser {
this.parseEmptyLines();
const _end = this.r.offset();

return u('src-block', { affiliated, language, value });
return u('src-block', {
affiliated,
language,
switches: switches?.trim() ?? null,
// using || to convert empty strings to null as well
parameters: parameters.trim() || null,
value,
});
}

private parseExampleBlock(
Expand Down

0 comments on commit a3f058b

Please sign in to comment.