Using markdown with IBM Connections Blogs

Created:
Last Update:

Author: Christoph Stoettner
Read in about 3 min · 634 words

Fountain pen and a notebook

Photo by Aaron Burden | Unsplash

I use markdown since years to create my articles or most of documentation stuff.

It’s really cool to edit the text files on all of my devices, synchronize them fast through various internet services and create HTML, PDF or even DOCX files out of this sources.

Until last week i converted the markdown sources with pandoc and pasted the html code to the different pages, where i need them. So i copy and paste it to IBM Connections, Evernote or WordPress . On WordPress i use a plugin now to render the markdown textes, but for IBM Connections i didn’t found a way.

So why not using the IBM Connections API to post html, or even convert the file on the fly and put the html automatically?

Posting to IBM Connections Blogs with Python

You can read a lot on the IBM Connections API in the [official documentation](http://www-10.lotus.com/ldd/lcwiki.nsf/xpAPIViewer.xsp?lookupName=IBM Connections 5.0 API Documentation#action=openDocument&res_title=IBM_Connections_API_overview_ic50&content=apicontent), i’m not a developer so i need normally practical examples to use this.

How to connect to IBM Connections REST API with Python?

First we need the URL to connect to the Blogs API:

url = 'https://greenhouse.lotus.com/blogs/stoeps/api/entries'

In this case “stoeps” is the blog handle, the blog where i want to add my post. You find the blog handle when you open the blog through your browser and check the url:

getBlogHandle

Now we need to authenticate, Connections API can be used with basic authentication, so we create a base64 encoded string of username and password:

import base64
encodedstring = base64.encodestring('christoph.stoettner@stoeps.de'+":"+'myCoolPassword')[:1]
auth = "Basic %s" % encodedstring

To post we have to use a XML File or XML String, which contains a title and body of our post, the REST service needs a Content-type of application/atom+xml:

xml = '''<?xml version="1.0" encoding="UTF-8"?>
      <entry xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns:snx="http://www.ibm.com/xmlns/prod/sn">
        <title type="text">That's the title</title>
        <content type="html"><![CDATA[<p>and some text for the body.</p>]]></content>
      </entry>'''

Now let’s connect to the REST service with urllib2 and post the xml string.

import urllib2
request = urllib2.Request( url )
request.add_data( xml ) # add_data changes Method to POST
request.add_header( 'Content-type', 'application/atom+xml' )
request.add_header( 'Authorization', auth )

response = urllib2.urlopen( request )

# let's check return code
if response.code = 201:
    print 'Post successfully created'

Some more

We have seen that we can post a xml with title and body. There are more possible ways to get a html string for our post.

I changed the xml variable to fill two variables with title and body.

postTitle = 'A cool API post to blogs'
postBody = '''<p>This is the body of our Blogs post, we can use html tags here</p>
              <h2>Fill text to show more content</h2>
              <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In commodo posuere ante, ut faucibus enim sagittis ut. Fusce scelerisque arcu quis mattis fringilla. Pellentesque a est enim. Nam congue sem eget augue porttitor semper. Fusce luctus sit amet ligula sit amet viverra. Cras pulvinar arcu eget velit volutpat, in condimentum nunc luctus. Vivamus vestibulum ante et nisl venenatis consequat. Aliquam ut augue nec lectus sodales eleifend eleifend nec orci.</p>'''
xml = '''<?xml version="1.0" encoding="UTF-8"?>
          <entry xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns:snx="http://www.ibm.com/xmlns/prod/sn">
            <title type="text">' + postTitle + '</title>
            <content type="html"><![CDATA[' + postBody + ']]></content>
          </entry>'''

You see you can add html code to the variable postBody. You can read from a html file with:

postBody = open('post.html').read()

Or you can convert a markdown file to html:

import markdown2
markdownFile = 'post.md'
postBody = markdown2.markdown_path( markdownFile )

In my script here i added a function to read the first line of the markdown to set it as title and i added a function to find images integrated in the article, upload them to stoeps/api/media and replace the img src= tag.

So this post is completely written in markdown and i posted it through python to Greenhouse. Here is the link to the post .

Author
Add a comment
Error
There was an error sending your comment, please try again.
Thank you!
Your comment has been submitted and will be published once it has been approved.

Your email address will not be published. Required fields are marked with *

Suggested Reading
Aaron Burden: Fountain pen and a notebook
Today i thought about creating and deleting some Communities with the REST API. Works really good and i need to post my findings sometimes. First cool thing is that you can create restricted Communities with external feature through REST. After deleting these Communities i checked the Community overview through my browser and found following view: So you mustn’t use wsadmin to restore Communities from trash, you can use this view. Never heared of it and the documentation still mentions wsadmin as the only way to restore them.
Created:
Last Update:
Read in about 1 min
Card image cap

Last week I played around with the HCL Connections documentation to backup Elasticsearch in the article Backup Elasticsearch Indices in Component Pack .

In the end I found that I couldn’t get the snapshot restored and that I have to run a command outside of my Kubernetes cluster to get a snapshot on a daily basis. That’s not what I want.

Created: Read in about 4 min
Card image cap

During a migration from Cognos Metrics to Elasticsearch Metrics, I had some issues with the index. So I wanted to create a backup of the already migrated data and start over from scratch.

The official documentation has an article on the topic: Backing up and restoring data for Elasticsearch-based components , but I had to slightly adjust the commands to get a successful snapshot.

Created:
Last Update:
Read in about 6 min