Compare commits
No commits in common. "48de06abc7626ec9899ca383cc649ad749eea718" and "5019b44dd50f080ff499f8c74c6bef5bc6d537d8" have entirely different histories.
48de06abc7
...
5019b44dd5
@ -113,7 +113,7 @@ BEGIN_BLOCK_RE = re.compile(r"^\s*#\+BEGIN_(?P<subtype>[^ ]+)(?P<arguments>.*)$"
|
|||||||
END_BLOCK_RE = re.compile(r"^\s*#\+END_(?P<subtype>[^ ]+)\s*$", re.I)
|
END_BLOCK_RE = re.compile(r"^\s*#\+END_(?P<subtype>[^ ]+)\s*$", re.I)
|
||||||
RESULTS_DRAWER_RE = re.compile(r"^\s*:results:\s*$", re.I)
|
RESULTS_DRAWER_RE = re.compile(r"^\s*:results:\s*$", re.I)
|
||||||
CodeSnippet = collections.namedtuple(
|
CodeSnippet = collections.namedtuple(
|
||||||
"CodeSnippet", ("name", "content", "result", "language", "arguments")
|
"CodeSnippet", ("name", "content", "result", "arguments")
|
||||||
)
|
)
|
||||||
|
|
||||||
# Groupings
|
# Groupings
|
||||||
@ -882,12 +882,6 @@ class Headline:
|
|||||||
sections = []
|
sections = []
|
||||||
arguments = None
|
arguments = None
|
||||||
|
|
||||||
names_by_line = {}
|
|
||||||
for kw in self.keywords:
|
|
||||||
if kw.key == "NAME":
|
|
||||||
names_by_line[kw.linenum] = kw.value
|
|
||||||
|
|
||||||
name = None
|
|
||||||
for delimiter in self.delimiters:
|
for delimiter in self.delimiters:
|
||||||
if (
|
if (
|
||||||
delimiter.delimiter_type == DelimiterLineType.BEGIN_BLOCK
|
delimiter.delimiter_type == DelimiterLineType.BEGIN_BLOCK
|
||||||
@ -896,12 +890,6 @@ class Headline:
|
|||||||
line_start = delimiter.linenum
|
line_start = delimiter.linenum
|
||||||
inside_code = True
|
inside_code = True
|
||||||
arguments = delimiter.arguments
|
arguments = delimiter.arguments
|
||||||
|
|
||||||
name_line = line_start - 1
|
|
||||||
if name_line in names_by_line:
|
|
||||||
name = names_by_line[name_line]
|
|
||||||
else:
|
|
||||||
name = None
|
|
||||||
elif (
|
elif (
|
||||||
delimiter.delimiter_type == DelimiterLineType.END_BLOCK
|
delimiter.delimiter_type == DelimiterLineType.END_BLOCK
|
||||||
and delimiter.type_data.subtype.lower() == "src"
|
and delimiter.type_data.subtype.lower() == "src"
|
||||||
@ -916,26 +904,14 @@ class Headline:
|
|||||||
# the content parsing must be re-thinked
|
# the content parsing must be re-thinked
|
||||||
contents = contents[:-1]
|
contents = contents[:-1]
|
||||||
|
|
||||||
language = None
|
|
||||||
if arguments is not None:
|
|
||||||
arguments = arguments.strip()
|
|
||||||
if " " in arguments:
|
|
||||||
language = arguments[: arguments.index(" ")]
|
|
||||||
arguments = arguments[arguments.index(" ") + 1 :]
|
|
||||||
else:
|
|
||||||
language = arguments
|
|
||||||
arguments = None
|
|
||||||
sections.append(
|
sections.append(
|
||||||
{
|
{
|
||||||
"line_first": start + 1,
|
"line_first": start + 1,
|
||||||
"line_last": end - 1,
|
"line_last": end - 1,
|
||||||
"content": contents,
|
"content": contents,
|
||||||
"arguments": arguments,
|
"arguments": arguments,
|
||||||
"language": language,
|
|
||||||
"name": name,
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
name = None
|
|
||||||
arguments = None
|
arguments = None
|
||||||
line_start = None
|
line_start = None
|
||||||
|
|
||||||
@ -984,18 +960,13 @@ class Headline:
|
|||||||
|
|
||||||
results = []
|
results = []
|
||||||
for section in sections:
|
for section in sections:
|
||||||
|
name = None
|
||||||
content = section["content"]
|
content = section["content"]
|
||||||
code_result = section.get("result", None)
|
code_result = section.get("result", None)
|
||||||
arguments = section.get("arguments", None)
|
arguments = section.get("arguments", None)
|
||||||
language = section.get("language", None)
|
|
||||||
name = section.get("name", None)
|
|
||||||
results.append(
|
results.append(
|
||||||
CodeSnippet(
|
CodeSnippet(
|
||||||
content=content,
|
name=name, content=content, result=code_result, arguments=arguments
|
||||||
result=code_result,
|
|
||||||
arguments=arguments,
|
|
||||||
language=language,
|
|
||||||
name=name,
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -2367,7 +2338,7 @@ class OrgDoc:
|
|||||||
yield hl
|
yield hl
|
||||||
|
|
||||||
def get_code_snippets(self):
|
def get_code_snippets(self):
|
||||||
for headline in self.getAllHeadlines():
|
for headline in self.headlines:
|
||||||
yield from headline.get_code_snippets()
|
yield from headline.get_code_snippets()
|
||||||
|
|
||||||
# Writing
|
# Writing
|
||||||
|
@ -9,7 +9,6 @@
|
|||||||
:CREATED: [2020-01-01 Wed 01:01]
|
:CREATED: [2020-01-01 Wed 01:01]
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
#+NAME: first-code-name
|
|
||||||
#+BEGIN_SRC shell :results verbatim
|
#+BEGIN_SRC shell :results verbatim
|
||||||
echo "This is a test"
|
echo "This is a test"
|
||||||
echo "with two lines"
|
echo "with two lines"
|
||||||
|
@ -480,22 +480,20 @@ class TestSerde(unittest.TestCase):
|
|||||||
|
|
||||||
snippets = list(doc.get_code_snippets())
|
snippets = list(doc.get_code_snippets())
|
||||||
self.assertEqual(len(snippets), 3)
|
self.assertEqual(len(snippets), 3)
|
||||||
self.assertEqual(snippets[0].name, "first-code-name")
|
|
||||||
self.assertEqual(snippets[0].language, "shell")
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
snippets[0].content,
|
snippets[0].content,
|
||||||
'echo "This is a test"\n'
|
'echo "This is a test"\n'
|
||||||
+ 'echo "with two lines"\n'
|
+ 'echo "with two lines"\n'
|
||||||
+ "exit 0 # Exit successfully",
|
+ "exit 0 # Exit successfully",
|
||||||
)
|
)
|
||||||
self.assertEqual(snippets[0].arguments.split(), [":results", "verbatim"])
|
self.assertEqual(
|
||||||
|
snippets[0].arguments.split(), ["shell", ":results", "verbatim"]
|
||||||
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
snippets[0].result,
|
snippets[0].result,
|
||||||
"This is a test\n" + "with two lines",
|
"This is a test\n" + "with two lines",
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertEqual(snippets[1].name, None)
|
|
||||||
self.assertEqual(snippets[1].language, "shell")
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
snippets[1].content,
|
snippets[1].content,
|
||||||
'echo "This is another test"\n'
|
'echo "This is another test"\n'
|
||||||
@ -506,8 +504,6 @@ class TestSerde(unittest.TestCase):
|
|||||||
snippets[1].result, "This is another test\n" + "with two lines too"
|
snippets[1].result, "This is another test\n" + "with two lines too"
|
||||||
)
|
)
|
||||||
|
|
||||||
self.assertEqual(snippets[2].name, None)
|
|
||||||
self.assertEqual(snippets[2].language, "c")
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
snippets[2].content,
|
snippets[2].content,
|
||||||
"/* This code has to be escaped to\n"
|
"/* This code has to be escaped to\n"
|
||||||
|
Loading…
Reference in New Issue
Block a user