Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AST のノードの付け替えを実装 #147

Merged
merged 5 commits into from
Aug 6, 2024
Merged

AST のノードの付け替えを実装 #147

merged 5 commits into from
Aug 6, 2024

Conversation

abap34
Copy link
Owner

@abap34 abap34 commented Aug 6, 2024

fix #143

ノードの付け替えと必要な AST操作のインターフェースを実装した。

この PR で脚注の実装に必要な全ての機能が実装完了 🎉

import almo


def pretty_print(ast, indent=0, last=False):
    print("  " * indent + f"{'└──' if last else '├──'} {ast}")
    for i, child in enumerate(ast.childs):
        pretty_print(child, indent + 1, i == len(ast.childs) - 1)


md = """
# Footnote Example

**On my way to the store**[^1], I saw a duck.

[^1]: Oatmeal is better than cereal.

Quack.

"""

ast = almo.parse(md)

print("=== AST ===")


pretty_print(ast)


footnote_defs = ast.nodes_byclass("FootnoteDefinition")


print("=== Move Footnote to the end ===")

ast.pushback_child(
    almo.DivBlock("footnote"),
)

ast.move_node(
    footnote_defs[0],
    ast.childs[-1],
)

pretty_print(ast)
=== AST ===
├── <almo.Markdown (uuid: 0)>
  ├── <almo.NewLine (uuid: 1)>
  ├── <almo.Header (uuid: 2)>
    └── <almo.RawText (uuid: 3)>
  ├── <almo.NewLine (uuid: 4)>
  ├── <almo.InlineStrong (uuid: 5)>
    └── <almo.RawText (uuid: 6)>
  ├── <almo.InlineFootnoteReference (uuid: 7)>
  ├── <almo.RawText (uuid: 8)>
  ├── <almo.NewLine (uuid: 9)>
  ├── <almo.FootnoteDefinition (uuid: 10)>
    └── <almo.RawText (uuid: 11)>
  ├── <almo.NewLine (uuid: 12)>
  ├── <almo.RawText (uuid: 13)>
  ├── <almo.NewLine (uuid: 14)>
  └── <almo.NewLine (uuid: 15)>

=== Move Footnote to the end ===
├── <almo.Markdown (uuid: 0)>
  ├── <almo.NewLine (uuid: 1)>
  ├── <almo.Header (uuid: 2)>
    └── <almo.RawText (uuid: 3)>
  ├── <almo.NewLine (uuid: 4)>
  ├── <almo.InlineStrong (uuid: 5)>
    └── <almo.RawText (uuid: 6)>
  ├── <almo.InlineFootnoteReference (uuid: 7)>
  ├── <almo.RawText (uuid: 8)>
  ├── <almo.NewLine (uuid: 9)>
  ├── <almo.NewLine (uuid: 12)>
  ├── <almo.RawText (uuid: 13)>
  ├── <almo.NewLine (uuid: 14)>
  ├── <almo.NewLine (uuid: 15)>
  └── <almo.DivBlock (uuid: 16)>
    └── <almo.FootnoteDefinition (uuid: 10)>
      └── <almo.RawText (uuid: 11)>

@abap34 abap34 changed the title ノードの付け替えを実装 AST のノードの付け替えを実装 Aug 6, 2024
@abap34 abap34 merged commit c7091f9 into main Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ASTのノードの付け替えを追加
1 participant