line_width = 200
lipsum =  [[

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur eget dolor a dui interdum rhoncus. Aenean congue nunc quis sem ultrices, vel fringilla tellus dignissim. Vivamus vitae purus ligula. Quisque ante elit, ultrices id aliquam a, porttitor quis risus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nunc elementum nisl nec efficitur mattis. Donec viverra tempor enim nec dictum. Suspendisse quam neque, posuere eu magna sit amet, rhoncus finibus risus. Phasellus tristique ac lacus eu ultricies. Quisque varius purus at eros rutrum hendrerit. Donec efficitur justo eu scelerisque mollis. Fusce dictum aliquam convallis. Phasellus eget nunc lacus. Nunc urna dui, tempor pharetra consectetur non, elementum sed purus. Phasellus consectetur quis libero et semper.

Pellentesque condimentum sem quis diam commodo convallis. Morbi et ligula sagittis, venenatis tortor ut, molestie dui. Nam egestas, purus eu efficitur mollis, ex neque convallis felis, at egestas nisi dolor sed enim. Pellentesque a felis facilisis orci dapibus porttitor. Aenean et viverra nulla, interdum eleifend neque. Nunc sem justo, vulputate et arcu eget, dignissim lobortis dui. Vivamus laoreet feugiat elit.

Fusce euismod nibh vitae orci gravida pretium. Quisque feugiat lacinia tortor eget sagittis. Morbi in mauris sit amet dui vehicula egestas. Curabitur sit amet facilisis lectus. Donec id turpis eleifend, hendrerit turpis eu, tristique velit. Curabitur pulvinar facilisis tincidunt. In hac habitasse platea dictumst. Donec convallis erat id neque pretium, non gravida felis imperdiet. Vivamus hendrerit, nunc id rutrum tristique, arcu leo auctor sapien, id fringilla mauris erat sed justo. Etiam pharetra quis enim sit amet imperdiet. Vestibulum condimentum massa vel ullamcorper malesuada. Duis libero eros, facilisis in suscipit in, varius id ipsum.

Donec in orci ac leo dapibus maximus sed eget tortor. Sed blandit eros lectus, et venenatis erat aliquam sed. Vestibulum risus ex, laoreet at malesuada non, egestas ut lacus. Quisque sed malesuada leo. Ut id ligula accumsan, commodo massa eget, porta mauris. Mauris a lectus ac tellus ornare aliquet quis at arcu. Curabitur nec sem sodales, mattis velit non, tempor arcu. Morbi sit amet maximus lacus, nec venenatis est. Curabitur et congue dui. Nullam lacinia augue non quam hendrerit facilisis.

Curabitur molestie mauris eget tempor mattis. Donec velit arcu, iaculis quis leo et, sodales venenatis erat. Maecenas in malesuada erat, vitae iaculis odio. Praesent id ultrices sem. In hac habitasse platea dictumst. In aliquet, nisl laoreet bibendum sodales, justo lacus aliquam mi, non elementum urna odio et nunc. Nulla interdum, ante in vestibulum pellentesque, nunc enim iaculis urna, eget convallis lectus ex sed tortor. Morbi suscipit malesuada felis at aliquam. 
]]
text = [[
<p>
<H2>The string library</H2>
<p>
Lua supplies a range of useful functions for processing and manipulating strings in its standard library. More details are supplied in the <a href="/wiki/StringLibraryTutorial" >StringLibraryTutorial</a>. Below are a few examples of usage of the string library.
<DL>
<dt><dd><pre class="code">
&gt; = <span class="library">string.byte</span>(<span class="string">"ABCDE"</span>, 2) <span class="comment">-- return the ASCII value of the second character</span>
66
&gt; = <span class="library">string.char</span>(65,66,67,68,69) <span class="comment">-- return a string constructed from ASCII values</span>
ABCDE
&gt; = <span class="library">string.find</span>(<span class="string">"hello Lua user"</span>, <span class="string">"Lua"</span>) <span class="comment">-- find substring "Lua"</span>
7       9
&gt; = <span class="library">string.find</span>(<span class="string">"hello Lua user"</span>, <span class="string">"l+"</span>) <span class="comment">-- find one or more occurrences of "l"</span>
3       4
&gt; = <span class="library">string.format</span>(<span class="string">"%.7f"</span>, <span class="library">math.pi</span>) <span class="comment">-- format a number</span>
3.1415927
&gt; = <span class="library">string.format</span>(<span class="string">"%8s"</span>, <span class="string">"Lua"</span>) <span class="comment">-- format a string</span>
     Lua
</pre>

</DL>
<p>
<H2>Coercion</H2>
<p>
Lua performs automatic conversion of numbers to strings and vice versa where it is appropriate. This is called <em>coercion</em>.
<DL>
<dt><dd><pre class="code">
&gt; = <span class="string">"This is Lua version "</span> .. 5.1 .. <span class="string">" we are using."</span>
This is Lua version 5.1 we are using.
&gt; = <span class="string">"Pi = "</span> .. <span class="library">math.pi</span>
Pi = 3.1415926535898
&gt; = <span class="string">"Pi = "</span> .. 3.1415927
Pi = 3.1415927
</pre>

</DL>
As shown above, during coercion, we do not have full control over the formatting of the conversion. To format the number as a string as we would like we can use the <code>string.format()</code> function. e.g.,
<DL>
<dt><dd><pre class="code">
&gt; = <span class="library">string.format</span>(<span class="string">"%.3f"</span>, 5.1)
5.100
&gt; = <span class="string">"Lua version "</span> .. <span class="library">string.format</span>(<span class="string">"%.1f"</span>, 5.1)
Lua version 5.1
</pre>

]]
text = text:gsub("\n"," ")
function indent_entry(contents,init,break_at)
    if contents:len() <= break_at then
        return contents -- Done
    end

    local begin_tag,end_tag = contents:find("<(.-)>",init)
    if not begin_tag or begin_tag > break_at then
        -- No tag exists or next tag is behind preferred break
        -- Find last space before break, if no space break word at break_at
        for i=break_at,init,-1 do -- Iterate backwards over string
            if contents:sub(i,i) == " " then
                break_at = i
                break;
            end
        end
        contents = contents:sub(1,break_at) .. "\n" .. contents:sub(break_at+1)
        init = break_at+1 -- Not necessarily same value as passed to the function
        break_at = break_at + line_width
        debug = "No tag or tag behind break."
    elseif end_tag < break_at then
        -- Next tag is before preferred break
        -- Increase break_at by length of tag and iterate (init = end_tag)
        init = end_tag
        break_at = break_at + 1 + end_tag - begin_tag
        debug = "Tag entirely before break"
    else
        -- Case break_at in the middle of tag:
        -- Increase break at by length of tag
        init = end_tag + 1
        break_at = break_at + 1 + end_tag - begin_tag
        debug = "Tag in the middle of break"
    end
    return indent_entry(contents,init,break_at)
end
function parse_paragraph(par)
    return indent_entry(par:gsub("\n", " "),1,line_width)
end
function substitute_tags(content)
    return content:gsub("<code>",("${color4}${font %s}"):format(font_i(9)))
            :gsub("<b>",("${font_b %s}"):format(font_b(9)))
            :gsub("</(.-)>","${color}${font}")
            :gsub("<a(.-)>","")
end
function parse_entry(contents)
    local ret = ""
    for paragraph in contents:gmatch("<p>(.-)</p>") do
        if ret ~= "" then
            ret = ret .. "\n\n" -- Add line break on all except first entry
        end
        ret = ret .. parse_paragraph(paragraph)
    end
    return substitute_tags(ret)
end
function format_entry(entry)
    local entry_contents = parse_entry(entry.summary)
    local res = ('${color2}${font %s}%s\n'):format(font(12),entry.title)
    res = ('%s${color}${font}%s'):format(res,entry_contents)
    res = ('%s${color3}${font %s}${alignr} Uppdated: %s\n\n'):format(res,font(8),os.date("%x",entry.updated_parsed))
    return res
end
function conky_print()
    local f = assert(get_file_handle('rb'))
    local content = f:read("*all")
    f:close()
    return content
end
function fetch_feed()
    -- Fetch over http
    local http_request = require "http.request"
    local url = "https://www.archlinux.org/feeds/news/"
    local headers, stream = assert(http_request.new_from_uri(url):go())
    local body = assert(stream:get_body_as_string())
    if headers:get ":status" ~= "200" then
        error(body)
    end
    a,s,c = headers:geti(3)
    p="%a+, (%d+) (%a+) (%d+) (%d+):(%d+):(%d+) GMT"
    day,month,year,hour,min,sec=s:match(p)
    MON={Jan=1,Feb=2,Mar=3,Apr=4,May=5,Jun=6,Jul=7,Aug=8,Sep=9,Oct=10,Nov=11,Dec=12}
    month=MON[month]
    offset=os.time()-os.time(os.date("!*t"))
    retrieved = os.date("%x",os.time({day=day,month=month,year=year,hour=hour,min=min,sec=sec})+offset)

    -- Parse 
    local feedparser = require("feedparser")
    entries = feedparser.parse(body).entries
    s = entries[2].summary
    local res = ('${alignr}${font %s}Retrieved: %s\n'):format(font(8),retrieved)
    for key,entry in pairs(entries) do
        res = res .. format_entry(entry)
    end

    -- Save to cache
    local file = get_file_handle("w")
    io.output(file)
    io.write(res)
    io.close(file)
    return parsed
end
function font(size)
    return ('DejaVu Sans:size=%d'):format(size)
end
function font_b(size)
    return ('DejaVu Sans:bold:size=%d'):format(size)
end
function font_i(size)
    return ('DejaVu Sans:italic:size=%d'):format(size)
end
function get_file_handle(opt)
    local filename = '/home/kuba/.conky/arch/cache'
    return io.open(filename, opt)
end
s = fetch_feed()
