Skip to content

Commit

Permalink
feat: devto hardwraps
Browse files Browse the repository at this point in the history
Added hardwrap to softwrap support for devto posts
  • Loading branch information
Mr-Destructive committed Jul 23, 2022
1 parent 0b1a308 commit 19be8dc
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 20 deletions.
30 changes: 18 additions & 12 deletions crossposter/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import sys
import json
import requests
import frontmatter
import argparse
from rich import print
from pathlib import Path
from .publications.dev import devto
from .publications.codenewbie import codenewbie
Expand All @@ -18,11 +18,12 @@ def get_default_or_input(dictionary, keys):


def main():
parser = argparse.ArgumentParser()
if len(sys.argv)<2:
print("[bold green]Crossposter[/ bold green]")
if len(sys.argv) < 2:
file_markdown = input("Enter the filename: ")
else:
file_markdown = sys.argv[1]

while not file_markdown:
print(f"No File Found with name {file_markdown}!")
file_markdown = input("Enter the filename: ")
Expand All @@ -31,14 +32,21 @@ def main():
break
else:
continue

parser = argparse.ArgumentParser()
parser.add_argument('Path', metavar='path', type=str, nargs='?', const=1, default=file_markdown, help='the path to file')
parser.add_argument("--dev", action="store_true", help='Post to dev.to')
parser.add_argument("--med", action="store_true", help='Post to medium.com')
parser.add_argument("--cdb", action="store_true", help='Post to codenewbie')
parser.add_argument(
"Path",
metavar="path",
type=str,
nargs="?",
const=1,
default=file_markdown,
help="the path to file",
)
parser.add_argument("--dev", action="store_true", help="Post to dev.to")
parser.add_argument("--med", action="store_true", help="Post to medium.com")
parser.add_argument("--cdb", action="store_true", help="Post to codenewbie")
args = parser.parse_args()

post = frontmatter.load(file_markdown)

with open("config.json", "r") as out:
Expand All @@ -55,7 +63,7 @@ def main():
article["title"] = get_default_or_input(post, ["title"])
article["description"] = get_default_or_input(post, ["subtitle", "description"])
slug = get_default_or_input(post, ["slug", "canonical_url"])
#while True:
# while True:
# if validators.url(slug):
# break
# else:
Expand All @@ -78,7 +86,6 @@ def main():
if "series" in post:
article["series"] = post["series"]


key_file = Path("keys.txt")
if not key_file.exists():
key_file.touch(exist_ok=True)
Expand All @@ -91,7 +98,6 @@ def main():
lines.append("codenewbie:\n")
f.writelines(lines)


if args.dev:
devto(article, output)
elif args.med:
Expand Down
1 change: 0 additions & 1 deletion crossposter/publications/codenewbie.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def codenewbie(article, output):
codenewbie_keys = input("Enter the Codenewbie API Key: ")
replace_line("keys.txt", 4, f"dev.to: {codenewbie_keys}\n")


post = {}

for key in article:
Expand Down
13 changes: 8 additions & 5 deletions crossposter/publications/dev.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import requests
import json
import sys
from crossposter.utils import replace_line
from crossposter.utils import hard_to_soft_wraps, replace_line


def devto(article, output):
Expand Down Expand Up @@ -31,12 +30,16 @@ def devto(article, output):
else:
dev_frontmatter += f"{key}: {post[key]}\n"


filename = post["title"].replace(" ", "_").lower()
output_file = output / f"{filename}_dev_post.md"

with open(output_file, "w") as file:
file.write(dev_frontmatter)
import re

lines = hard_to_soft_wraps(dev_frontmatter)

with open(output_file, "w") as f:
f.writelines(lines)
print("The DEV frontmatter is generated in the file -> ", output_file)

API_ENDPOINT = "https://dev.to/api/articles"

Expand Down
2 changes: 1 addition & 1 deletion crossposter/publications/hashnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def hashnode(article, output):
if hashnode_id != "\n":
hashnode_id = hashnode_id.strip()
else:
hashnode_id= input("Enter your Hashnode ID: ")
hashnode_id = input("Enter your Hashnode ID: ")
replace_line("keys.txt", 3, f"hashnode_id: {hashnode_id}\n")

title = str(article["title"])
Expand Down
1 change: 1 addition & 0 deletions crossposter/publications/medium.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
from crossposter.utils import replace_line


def medium(article, output):

print("Cross-Posting on medium.com")
Expand Down
36 changes: 36 additions & 0 deletions crossposter/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re


def replace_line(file_name, line_num, text):
with open(file_name, "r+") as f:
Expand All @@ -6,3 +8,37 @@ def replace_line(file_name, line_num, text):
f.truncate(0)
f.seek(0)
f.writelines(lines)


def hard_to_soft_wraps(content):
# pre= re.findall(r'\n---\n(.*?)\n```.*$', content, re.DOTALL)
# mid = re.findall(r'\n```\n(.*?)\n```(.*?)',content, re.DOTALL)
# post = re.findall(r'\n```\n\n(.*$)', content, re.DOTALL)

# get all fenced code block
fences = re.findall(r"\n```.*?```\n", content, re.DOTALL)

# get the frontmatter data
frontmatter = re.findall(r"---.*?---\n", content, re.DOTALL)

for fence in fences:
# set the new line character as some value //r to identify later
content = content.replace(fence, fence.replace("\n", "\\r"))
for attrib in frontmatter:
content = content.replace(attrib, attrib.replace("\n", "\\r"))

# Replace the new paragraph as a special character \\r
content = content.replace("\n\n", "\\r\\r")

# Replace the new heading with the special character
content = content.replace("\n#", "\\r\\r")

# Split the text for iterating over the contents
content = re.split("\\r\\r", content)
for w in content:
# replace the newline character which is a hard wrap into a whitespace
# then replace the special character with new line character
content = w.replace("\n", " ").replace("\\r", "\n")

# returned soft wrapped content
return content
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
certifi==2022.5.18.1
charset-normalizer==2.0.12
commonmark==0.9.1
idna==3.3
Pygments==2.12.0
python-frontmatter==1.0.0
PyYAML==5.1
requests==2.27.1
rich==12.5.1
urllib3==1.26.9
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

HERE = pathlib.Path(__file__).parent

VERSION = "0.4.5"
VERSION = "0.5.0"
PACKAGE_NAME = "crossposter"
AUTHOR = "Meet Gor"
AUTHOR_EMAIL = "[email protected]"
Expand Down

0 comments on commit 19be8dc

Please sign in to comment.