Coverage for pyquickhelper/sphinxext/build_rss.py: 100%

29 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-03 02:21 +0200

1# coding:utf-8 

2""" 

3@file 

4@brief Buid the RSS stream. 

5""" 

6import datetime 

7import os 

8from xml.sax.saxutils import escape 

9 

10modelForARSSFeed = """<rss version="2.0"> 

11 <channel> 

12 <title>{0.blogtitle}</title> 

13 <link>{0.root}/blog/main_0000.html</link> 

14 <description>{0.description}</description> 

15 """.replace(" ", "") 

16 

17modelForARSSRow = """<item> 

18 <title>{0.title}</title> 

19 <link>{0.root}/blog/{0.year}/{0.name}.html</link> 

20 <guid isPermaLink="true">{0.root}/blog/{0.year}/{0.name}.html</guid> 

21 <description>{0.decription}</description> 

22 <pubDate>{0.date}</pubDate> 

23 </item>""" 

24 

25modelForARSSChannel = """\n</channel>\n</rss>\n""" 

26 

27 

28def build_rss(posts, 

29 blog_title="__BLOG_TITLE__", 

30 blog_root="__BLOG_ROOT__", 

31 blog_description="__BLOG_DESCRIPTION__", 

32 now=datetime.datetime.now(), 

33 model_feed=modelForARSSFeed, 

34 model_row=modelForARSSRow, 

35 model_channel=modelForARSSChannel): 

36 """ 

37 Build a RSS file, the function keeps the blog post (HTML format) from the last month. 

38 The summary will only contains the part included in those two comments. 

39 

40 @param posts list of posts to include 

41 @param blog_title title of the blog 

42 @param blog_description description of the blog 

43 @param blog_root url root 

44 @param now date to use as a final date, only blog post between one month now and now will be kept 

45 @param model_feed see model_channel 

46 @param model_row see model_row 

47 @param model_channel the part related to a post in the rss stream is composed 

48 by the concatenation of the three stream: 

49 *model_feed*, *model_row*, *model_channel*. 

50 You should see the default value to see how you can replace them. 

51 @return 2-uple: content of the file 

52 """ 

53 

54 class EmptyClass: 

55 pass 

56 

57 obj = EmptyClass() 

58 obj.blogtitle = blog_title 

59 obj.root = blog_root 

60 obj.description = blog_description 

61 

62 rows = ["<?xml version=\"1.0\" encoding=\"utf-8\"?>"] 

63 rows.append(modelForARSSFeed.format(obj)) 

64 

65 for post in posts: 

66 obj = EmptyClass() 

67 obj.title = escape(post.Title) 

68 obj.date = post.Date 

69 obj.year = post.Date[:4] 

70 obj.name = escape( 

71 os.path.splitext(os.path.split(post.FileName)[-1])[0]) 

72 obj.decription = escape("\n".join(post.Content)) 

73 obj.root = blog_root 

74 row = modelForARSSRow.format(obj) 

75 rows.append(row) 

76 

77 rows.append(modelForARSSChannel) 

78 content = "\n".join(rows) 

79 

80 return content