Skip to content

Commit

Permalink
Add support for lists in html2md
Browse files Browse the repository at this point in the history
  • Loading branch information
alexg-axis committed Dec 2, 2021
1 parent d85247d commit 4cf5848
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
32 changes: 28 additions & 4 deletions 2021/2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ Now, you need to figure out how to pilot this thing.

It seems like the submarine can take a series of commands like `forward 1`, `down 2`, or `up 3`:

`forward X``X``down X`__increases__`X``up X`__decreases__`X`Note that since you're on a submarine, `down` and `up` affect your __depth__, and so they have the opposite result of what you might expect.
- `forward X` increases the horizontal position by `X` units.
- `down X` __increases__ the depth by `X` units.
- `up X` __decreases__ the depth by `X` units.

Note that since you're on a submarine, `down` and `up` affect your __depth__, and so they have the opposite result of what you might expect.

The submarine seems to already have a planned course (your puzzle input). You should probably figure out where it's going. For example:

Expand All @@ -19,7 +23,14 @@ forward 2

Your horizontal position and depth both start at `0`. The steps above would then modify them as follows:

`forward 5``5``5``down 5``5``5``forward 8``8``13``up 3``3``2``down 8``8``10``forward 2``2``15`After following these instructions, you would have a horizontal position of `15` and a depth of `10`. (Multiplying these together produces `150`.)
- `forward 5` adds `5` to your horizontal position, a total of `5`.
- `down 5` adds `5` to your depth, resulting in a value of `5`.
- `forward 8` adds `8` to your horizontal position, a total of `13`.
- `up 3` decreases your depth by `3`, resulting in a value of `2`.
- `down 8` adds `8` to your depth, resulting in a value of `10`.
- `forward 2` adds `2` to your horizontal position, a total of `15`.

After following these instructions, you would have a horizontal position of `15` and a depth of `10`. (Multiplying these together produces `150`.)

Calculate the horizontal position and depth you would have after following the planned course. __What do you get if you multiply your final horizontal position by your final depth?__

Expand All @@ -29,11 +40,24 @@ Based on your calculations, the planned course doesn't seem to make any sense. Y

In addition to horizontal position and depth, you'll also need to track a third value, __aim__, which also starts at `0`. The commands also mean something entirely different than you first thought:

`down X`__increases__`X``up X`__decreases__`X``forward X``X`__multiplied by__`X`Again note that since you're on a submarine, `down` and `up` do the opposite of what you might expect: "down" means aiming in the positive direction.
- `down X` __increases__ your aim by `X` units.
- `up X` __decreases__ your aim by `X` units.
- `forward X` does two things:
- It increases your horizontal position by `X` units.
- It increases your depth by your aim __multiplied by__ `X`.

Again note that since you're on a submarine, `down` and `up` do the opposite of what you might expect: "down" means aiming in the positive direction.

Now, the above example does something different:

`forward 5``5``5``0``down 5``5``5``forward 8``8``13``5``8*5=40``up 3``3``2``down 8``8``10``forward 2``2``15``10``2*10=20``60`After following these new instructions, you would have a horizontal position of `15` and a depth of `60`. (Multiplying these produces `900`.)
- `forward 5` adds `5` to your horizontal position, a total of `5`. Because your aim is `0`, your depth does not change.
- `down 5` adds `5` to your aim, resulting in a value of `5`.
- `forward 8` adds `8` to your horizontal position, a total of `13`. Because your aim is `5`, your depth increases by `8*5=40`.
- `up 3` decreases your aim by `3`, resulting in a value of `2`.
- `down 8` adds `8` to your aim, resulting in a value of `10`.
- `forward 2` adds `2` to your horizontal position, a total of `15`. Because your aim is `10`, your depth increases by `2*10=20` to a total of `60`.

After following these new instructions, you would have a horizontal position of `15` and a depth of `60`. (Multiplying these produces `900`.)

Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. __What do you get if you multiply your final horizontal position by your final depth?__

25 changes: 25 additions & 0 deletions scripts/html2md.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def __init__(self, *, convert_charrefs: bool = ...) -> None:
self.tags: list[tuple[str, HTMLAttributes]] = []
# Whether or not we're inside the main article
self.is_in_article = False
# The ul depth
self.list_depth = 0
# Whether the last seen ul was nested
self.list_was_nested = False

def handle_starttag(self, tag: str, attrs: HTMLAttributes) -> None:
self.tags.append((tag, attrs))
Expand All @@ -40,6 +44,15 @@ def handle_starttag(self, tag: str, attrs: HTMLAttributes) -> None:
print("```")
else:
print("`", end="")
elif tag == "li":
print(" " * (self.list_depth - 1) + "- ", end="")
elif tag == "ul":
self.list_depth += 1
if self.list_depth == 1:
self.list_was_nested = False
elif self.list_depth > 1:
self.list_was_nested = True
print()

def handle_endtag(self, tag: str) -> None:
self.tags.pop()
Expand All @@ -60,6 +73,16 @@ def handle_endtag(self, tag: str) -> None:
print("```\n")
else:
print("`", end="")
elif tag == "li":
print()
elif tag == "ul":
self.list_depth -= 1
# As a nested ul resides within a li tag two newlines will be printed
# for the last li. As we don't have a lookahead, best we can do is to
# keep track of last seen nested ul and add another newline when we
# leave a non-nested ul
if not self.list_was_nested and self.list_depth == 0:
print()

@property
def current_tag(self) -> tuple[str, HTMLAttributes] | None:
Expand Down Expand Up @@ -92,6 +115,8 @@ def handle_data(self, data: str) -> None:
print(data, end="")
elif tag == "span":
print(data, end="")
elif tag == "li":
print(data, end="")

def main():
parser = CustomParser()
Expand Down

0 comments on commit 4cf5848

Please sign in to comment.