Refactoring and Rebasing (Lab 5)

A few things about my SSG script have always irked me, so this lab was a perfect opportunity to clean those up.

Improvements

The first thing I wanted to fix was this very long line of code:

# Process header markdown
headerTag = lambda s:…


This content originally appeared on DEV Community and was authored by Ahmad

A few things about my SSG script have always irked me, so this lab was a perfect opportunity to clean those up.

Improvements

The first thing I wanted to fix was this very long line of code:

# Process header markdown
headerTag = lambda s: '{endpTag}<h{size}>{regexContent}</h{size}>{pTag}'.format(endpTag="</p>\n\n" if s.group(1)=="\n" else "", size=s.group(2).count('#'), regexContent=s.group(3), pTag="\n\n<p>" if s.group(4)=="\n" else "")
content = re.sub(r'(|(?<!\n)\n|<p>)(#{1,5})\s(.*)(<\/p>|(?<!<\/p>)\n|$)', headerTag, content)

I was able to greatly simplify the lambda expression and reduce the footprint:

# Process header markdown
headerTag = lambda s: '<h{size}>{content}</h{size}>\n'.format(size=s.group(2).count('#'), content=s.group(3))
content = re.sub(r'(|(?<!\n)\n|<p>)(#{1,5})\s(.*)(<\/p>|(?<!<\/p>)\n|$)', headerTag, content)

The next thing that always bothered me was the fact that everything was in one big file. So I split ssg.py into three different classes: ssg.py, SSGParser.py, and SSGUtil.py. SSGParser.py, as the name suggests, contains everything to do with parsing, SSGUtil.py contains all the "helper" functions, and ssg.py now only consists of main. This step really cleaned up my code and I don't cringe when I see it anymore.
Image description

Rebasing

After committing all my improvements, I performed an interactive rebase to squash the commits into one, informative commit.
Image description

Outcomes

All in all, I'm very glad I learned about changing the history of a repo by rebasing and how to squash commits. In past labs, I'd have a mini heart attack whenever I realized I forgot something!


This content originally appeared on DEV Community and was authored by Ahmad


Print Share Comment Cite Upload Translate Updates
APA

Ahmad | Sciencx (2021-10-14T14:16:08+00:00) Refactoring and Rebasing (Lab 5). Retrieved from https://www.scien.cx/2021/10/14/refactoring-and-rebasing-lab-5/

MLA
" » Refactoring and Rebasing (Lab 5)." Ahmad | Sciencx - Thursday October 14, 2021, https://www.scien.cx/2021/10/14/refactoring-and-rebasing-lab-5/
HARVARD
Ahmad | Sciencx Thursday October 14, 2021 » Refactoring and Rebasing (Lab 5)., viewed ,<https://www.scien.cx/2021/10/14/refactoring-and-rebasing-lab-5/>
VANCOUVER
Ahmad | Sciencx - » Refactoring and Rebasing (Lab 5). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/14/refactoring-and-rebasing-lab-5/
CHICAGO
" » Refactoring and Rebasing (Lab 5)." Ahmad | Sciencx - Accessed . https://www.scien.cx/2021/10/14/refactoring-and-rebasing-lab-5/
IEEE
" » Refactoring and Rebasing (Lab 5)." Ahmad | Sciencx [Online]. Available: https://www.scien.cx/2021/10/14/refactoring-and-rebasing-lab-5/. [Accessed: ]
rf:citation
» Refactoring and Rebasing (Lab 5) | Ahmad | Sciencx | https://www.scien.cx/2021/10/14/refactoring-and-rebasing-lab-5/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.