Skip to content

Commit

Permalink
Carefully takes care of newline at the end of line
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Gladkov <[email protected]>
  • Loading branch information
legionus committed Sep 13, 2023
1 parent 3bab342 commit a11e3fb
Showing 1 changed file with 20 additions and 27 deletions.
47 changes: 20 additions & 27 deletions jiramail/change.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,12 @@ def args(self) -> List[str]:
return self.words[1:]
return []

def add_raw(self, line: str) -> None:
if line.endswith("\n"):
self.raw.append(line[:-1])
else:
self.raw.append(line)

def __str__(self) -> str:
return f"{self.__class__}: {self.name()} {self.args()}"

Expand All @@ -368,52 +374,39 @@ def parse_commands(fd: TextIO) -> Tuple[List[Command], List[str]]:
commands: List[Command] = []
content: List[str] = []

while True:
value = fd.readline()
if not value:
break

line = str(value[:-1])

m = re.match(r'^\s*jira\s+(.*)\s*$', line)
for value in fd:
m = re.match(r'^\s*jira\s+(.*)\s*$', value)
if not m:
content.append(line)
content.append(value[:-1])
continue

command = Command()
command.raw.append(line)
command.add_raw(value)
command.words += get_words(m.group(1))

while True:
if len(command.words) == 0 or command.words[-1] != "\\":
break
command.words.pop()

while len(command.words) > 0 and command.words[-1] == "\\":
value = fd.readline()
line = str(value[:-1])
if not value:
break

command.words += get_words(line)
command.raw.append(line)
command.words.pop()
command.add_raw(value)
command.words += get_words(command.raw[-1])

for i, word in enumerate(command.words):
if word.startswith("<<"):
token = word[2:]
token_found = False
heredoc = []

while True:
value = fd.readline()
if not value:
break

line = str(value[:-1])
command.raw.append(line)
for value in fd:
command.add_raw(value)

token_found = line == token
token_found = command.raw[-1] == token
if token_found:
break

heredoc.append(line)
heredoc.append(command.raw[-1])

if not token_found:
command.error = jiramail.Error(f"enclosing token '{token}' not found")
Expand Down

0 comments on commit a11e3fb

Please sign in to comment.