In [1]:
!date
Mon 02 Aug 2021 10:41:14 PM EDT
In [1]:
print('hello fun world')
# because we're lazy (you're partial to this to now) we'll put it in a function
hello cool world
In [2]:
# in beta. 

# today i had to write a ton of html for this one thing. 

Okay it wasn't a ton but it was like this confusing piece i have to write to post a blog so i can make my blog roll all drop down menu-y

so im going to write a thingy that make that bit of code for me so i can copy paste it in. in future developments maybe i'll be an adult about the whole thing and use javascript.

for now tho.

yolo.

In [3]:
blog_post_string = """
<a href="./blog/just_a_little_bit.html" target="_blank"><li>this one isn't about python</li></a> 
"""

# it's a doozy

admittedly i just started whipping it together up there but i figure maybe it's better to show workflow. makes for better content at least. let's make it official.

workflow

that up there is a link to my latest blog post. if you haven't read it you should go back and read it right now before you keep going in this one because there's spoilers. the spoiler is that they have nothing to do with each other.

oh great your stuck here now.

we can change things to make that link work for us.

In [5]:
html_file = 'one_here.html'
slug = 'and one here'

blog_post_string = '<a href="./blog/{html_file}" target="_blank"><li>{slug}</li></a> '.format(html_file=html_file, slug=slug)
In [15]:
print(blog_post_string)
<a href="./blog/one_here.html" target="_blank"><li>and one here</li></a> 
In [13]:
import pyperclip # this makes it easy to copy stuff to the clipboard.

def make_blog_html_post_thing_that_makes_my_life_easy(html_file, slug):
    # the wonders of tab finish
    # always document children
    '''
    this function takes the name of an html file or the path to one in string type
    and the slug, a small descriptive text in string type and outputs the link
    to copy and paste in new_blog.html
    '''
    
    # that's as plain as i can put it. will accept criticism for coffee
    
    blog_post_string = '<a href="./blog/{html_file}" target="_blank"><li>{slug}</li></a> '.format(html_file=html_file, slug=slug)
    
   
    pyperclip.copy(blog_post_string)
    
In [17]:
# always test
make_blog_html_post_thing_that_makes_my_life_easy('something.html', 
                                                  'something')

<a href="./blog/something.html" target="_blank"><li>something</li></a>

what u can't tell here is that i pasted after i ran the function and it worked.

hooray

That's it

there ya go. i made a thing. as far as thing's go, that's pretty simple. now to actually make it effective i will have to write it into it's own python file that does things and then probably make a command line alias for it. this also is still manual to a point, where i have to go still click into the right place in the blog roll html file.

obviously this is already getting out of hand.

the command line argument can come later.

next time...follow me as i

build the thing that finds the place in the html file that is my blog roll where new posts should go and places the link from the function i made earlier into that spot and saves the file

below is a little bit

bye

In [9]:
from bs4 import BeautifulSoup as bs
from bs4 import Comment



html = '../new_blog.html'
html = open(html, 'r')
a = []

soup = bs(html, 'html.parser')
comments = soup.find_all(string=lambda text: isinstance(text, Comment))
# lambdas is what you get when you copy paste stack over flow code
for c in comments:
    print(c)
    print("=============")
    a = c.extract()
    
put new posts above this line
=============
<ul>
              <li>Pachyderms
             </ul>
            <li class="last">Mouse</li>
           </ul>
          </li>
          <li class="last">Reptiles</li>
         </ul>
        </li>
        <li class="last">Plants
         <ul>
          <li>Flowers
           <ul>
            <li>Rose</li>
            <li class="last">Tulip</li>
           </ul>
          </li>
          <li class="last">Trees</li>
         </ul>
        </li>
       </ul> 
=============
ul class="tree"> #100DaysOfcode
        <li>This
         <ul>
          <li>Tree</li>
          <li>List
           <ul>
            <li>Is
             <ul>
              <li>Just
             </ul>
            <li class="last">A</li>
           </ul>
          </li>
          <li class="last">Placeholder</li>
         </ul>
        </li>
        <li class="last">Until
         <ul>
          <li>I
           <ul>
            <li>Figure</li>
            <ul>
            <li class="last">Out</li>
            </ul>
           </ul>
          </li>
          <li>How</li>
         <ul>
             <li>To
             
             </li>
             <li>Scrape</li>
             <li>My</li>
             <li>Old</li>
             <li>Site</li>
             </ul>
         </ul>
         </ul>
        </li>
       </ul>
    </div>
    <div class="col-md-4"></div>
=============

that's right. u see sublists. O_o

pyperclip is an external library that this guy wrote

BeautifulSoup has nothing to do with soup and helps programmers take apart html files and stuff can be found here about it.

the code from above was copied from here. i kind of understand it, but :P