Skip to content

Commit

Permalink
Added more methods to add elements to the document.
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhJae committed Aug 25, 2023
1 parent 0539ccd commit 7ce6d6e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ This will create redy-to-use mobiledoc like below. (click expand to see the mobi
```
- **`add_divider()`**: Adds a divider to mobiledoc.
- **`add_image(url: str, caption: str = None)`**: Adds an image to mobiledoc.
- **`add_button(text: str, url: str, alignment:str = "center")`**: Adds a button to mobiledoc.
- **`add_HTML(self, html:str):`**: Adds raw HTML card to mobiledoc.
- **`add_markdown(self, markdown:str):`**: Adds raw markdown card to mobiledoc.
- **`add_file(self, url: str, filename: str, filetitle: str, filesize: int, filecaption: str = ""):`**: Adds a file to mobiledoc.
- **`def add_callout(self, text: str, emoji: str = "", color: str = "accent"):`**: Adds a callout to mobiledoc.
- **`custom_data(name: str, value)`**: Adds custom data to mobiledoc.
### Serializing mobiledoc
Expand Down
66 changes: 65 additions & 1 deletion mobiledoc/mobiledoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,66 @@ def add_image(self, url:str, caption:str = None):
card_idx = self.cards.index(card)
self.sections.append([10, card_idx])

def add_button(self, text:str, url:str, alignment:str = "center"):
""" Method to add a button to mobiledoc """
card = ["button", {"text": text, "url": url, "alignment": alignment}]

# check if card already exists
if card not in self.cards:
self.cards.append(card)

# find the index of the card and point to it
card_idx = self.cards.index(card)
self.sections.append([10, card_idx])

def add_HTML(self, html:str):
""" Method to add HTML cardto mobiledoc """
card = ["html", {"html": html}]

# check if card already exists
if card not in self.cards:
self.cards.append(card)

# find the index of the card and point to it
card_idx = self.cards.index(card)
self.sections.append([10, card_idx])

def add_markdown(self, markdown:str):
""" Method to add markdown card to mobiledoc """
card = ["markdown", {"markdown": markdown}]

# check if card already exists
if card not in self.cards:
self.cards.append(card)

# find the index of the card and point to it
card_idx = self.cards.index(card)
self.sections.append([10, card_idx])

def add_file(self, url: str, filename: str, filetitle: str, filesize: int, filecaption: str = ""):
""" Method to add a file to mobiledoc """
card = ["file", {"src": url, "fileName": filename, "fileTitle": filetitle, "fileCaption": filecaption,
"fileSize": filesize}]

# check if card already exists
if card not in self.cards:
self.cards.append(card)

# find the index of the card and point to it
card_idx = self.cards.index(card)
self.sections.append([10, card_idx])

def add_callout(self, text: str, emoji: str = "", color: str = "accent"):
card = ["callout", {"calloutEmoji": emoji, "calloutText": text, "backgroundColor": color}]

# check if card already exists
if card not in self.cards:
self.cards.append(card)

# find the index of the card and point to it
card_idx = self.cards.index(card)
self.sections.append([10, card_idx])

def custom_data(self, name: str, value):
""" Method to add custom data to mobiledoc """
self.custom[name] = value
Expand Down Expand Up @@ -224,7 +284,11 @@ def serialize(self):
mobiledoc.add_formatted_text(["You may also add a `list of strings`.", "To ^add^ ^^multiple^^ paragraphs.",
"You can also add [hyperlinks](https://python.org)."])
mobiledoc.add_image("https://placehold.co/600x400/EEE/31343C", "You can add images!")
mobiledoc.add_image("https://placehold.co/600x400/EEE/31343C")
mobiledoc.add_button("You can also buttons!", "https://python.org")
mobiledoc.add_HTML("<ul><li>You can also add HTML</li><li>Like this</li></ul>")
mobiledoc.add_markdown("You can add **raw markdown** cards! \n1. list item one\n2. list item two")
mobiledoc.add_file("https://placehold.co/600x400/EEE/31343C", "filename", "filetitle", 1000)
mobiledoc.add_callout("You can add callouts!", "🔥", "accent")

mobiledoc = mobiledoc.serialize() # This will save the mobiledoc as a dictionary

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "mobiledoc"
version = "0.0.1"
version = "0.1.0"
authors = [
{ name="Suh Jae", email="wonyul@joseon.space" },
]
Expand Down

0 comments on commit 7ce6d6e

Please sign in to comment.