render.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. # THIS SOFTWARE IS A PART OF FREE COMPETITOR PROJECT
  2. # THE FOLLOWING SOURCE CODE I UNDER THE GNU
  3. # AGPL LICENSE V3 OR ANY LATER VERSION.
  4. # This project is not for simple users, but for
  5. # web-masters and a like, so we are counting on
  6. # your ability to set it up and running.
  7. import os
  8. from modules import search
  9. def html(page, json):
  10. # This function adds a rendering of the json into the page
  11. free = search.is_free(json)
  12. page = page + "\n <h1>"
  13. try:
  14. page = page + '\n<img src="'+ json["links"]["icon"] + '" alt="Logo" style="height:50px;">'
  15. except:
  16. pass
  17. name = json.get("names",["Unknown"])[0]
  18. page = page + "\n" + name
  19. page = page + "</h1>"
  20. # Few words about it
  21. comment = json.get("comment","")
  22. not_foss = ['open source', 'open-source']
  23. if "open source" or "open-source" in comment.lower():
  24. # Well... Here is a thing. Free Software, not open source.
  25. where = comment.lower().find("open source")
  26. # In case it has a slash in it.
  27. if where == -1:
  28. where = comment.lower().find("open-source")
  29. ops = comment[where:where+11]
  30. comment = comment.replace(ops,
  31. "<a href=\"https://www.gnu.org/philosophy/open-source-misses-the-point.en.html\">"+ops+"</a>")
  32. page = page + "<p>"+comment+"</p>"
  33. # I want to show nothing else from if it's proprietary
  34. issues_files = list(os.listdir("data/issues"))
  35. if "issues" in json:
  36. l = json.get("issues", [])
  37. page = page +"<h2>Anti-Features / Problems:</h2>"
  38. for i in l:
  39. if i+".html" not in issues_files:
  40. page = page + "&nbsp;&nbsp;"+i+"<br>"
  41. else:
  42. page = page + '<details title="Read about '+i+'">'
  43. page = page + "<summary>&nbsp;&nbsp"+i+"</summary>"
  44. issuefile = open("data/issues/"+i+".html")
  45. page = page + "<span><p>"+issuefile.read()+"</p></span>"
  46. page = page + "</details>"
  47. if not free:
  48. return page
  49. # Links
  50. # <table>
  51. # <tr>
  52. # <th>Company</th>
  53. # <th>Contact</th>
  54. # <th>Country</th>
  55. #</tr>
  56. page = page + """
  57. <style>
  58. table, th, td {
  59. border-right:none;
  60. border-left:none;
  61. border-bottom:none;
  62. border-top:none
  63. }</style>
  64. """
  65. page = page + "<table><tr>"
  66. linksfilter = {"git":"source"}
  67. links = json.get("links", {})
  68. for website in links:
  69. if website in ["icon"]:
  70. continue
  71. link = links[website]
  72. page = page + """
  73. <th><form action=\""""+link+"""\">
  74. <button title=\""""+link+"""\" type="submit">"""+linksfilter.get(website,website).upper()+"""</button>
  75. </form></th>
  76. """
  77. page = page + "</tr></table>"
  78. # Details
  79. categories = {"generic_name":"Features",
  80. "licenses":"License(s)",
  81. "platforms":"Platforms",
  82. "networks_read":"Accesses Data from",
  83. "networks_write":"Interacts / Publishes to",
  84. "formats_read":"Opens from File-Formats",
  85. "formats_write":"Saves to File-Formats",
  86. "interface":"Interface",
  87. "languages":"Programming Languages"}
  88. for c in categories:
  89. l = json.get(c, [])
  90. if not l:
  91. continue
  92. # I want to look whether this category has a list of files
  93. alldata = list(os.listdir("data"))
  94. allfiles = []
  95. for folder in alldata:
  96. if c.startswith(folder):
  97. try:
  98. allfiles = list(os.listdir("data/"+folder))
  99. break
  100. except:
  101. pass
  102. # Count matches
  103. matches = 0
  104. for i in l:
  105. if i.startswith("*"):
  106. matches += 1
  107. if matches:
  108. matchtext = "<i>( "+str(matches)+" )</i>"
  109. else:
  110. matchtext = ""
  111. page = page + "<details>"
  112. page = page +"<summary>"+categories[c]+": "+matchtext+"</summary>"
  113. for i in l:
  114. matchtext = ""
  115. if i.startswith("*"):
  116. i = i[1:]
  117. matchtext = "&nbsp;&nbsp;&nbsp;&nbsp;<i>( match )</i> "
  118. if i+".html" in allfiles:
  119. datapage = open("data/"+folder+"/"+i+".html")
  120. page = page + "<details><summary>&nbsp;&nbsp;&nbsp;&nbsp;"+matchtext+i+"</summary><span><p>"+datapage.read()+"<p></span></details>"
  121. else:
  122. page = page + "&nbsp;&nbsp;&nbsp;&nbsp;"+matchtext+i+"<br>"
  123. page = page + "<span><br></span></details>"
  124. return page
  125. def suggestions(page, json):
  126. # This function will render suggestions
  127. page = page + "<h1>Free Competitors:</h1>"
  128. found = search.suggest(json)
  129. biggest = 0
  130. for i in found:
  131. if i[0] > biggest:
  132. biggest = i[0]
  133. more = False
  134. for i in found:
  135. free = search.is_free(i[-1])
  136. if not i[0] or i[-1] == json or not free:
  137. continue
  138. try:
  139. frac = int(i[0]/biggest*100)
  140. except:
  141. frac = 0
  142. if frac < 20 and not more: # Below 40% features match
  143. page = page + """<hr><details>
  144. <summary><h1 title="Click to show more / less.">Problematic Competitors:</h1></summary>"""
  145. more = True
  146. page = page + "<hr><br>Suggestion score: " + str(frac) + "%"
  147. page = html(page, i[-1])
  148. if more:
  149. page = page + "</details>"
  150. return page
  151. def search_widget(page, address):
  152. # Adds a search bar to the page
  153. page = page + """
  154. <form action="""
  155. page = page + address
  156. page = page + """search method="GET">
  157. <input type="text" name="item" class="search" placeholder="Name of Software">
  158. <button type="submit">Search</button>
  159. </form>
  160. """
  161. #page = page.format(ADDRESS)
  162. return page
  163. def source_code_link(page):
  164. # Adds a source code link
  165. page = page + "<br><br><hr><p>This website is under the GNU AGPL license.</p>"
  166. page = page + """
  167. <style>
  168. table, th, td {
  169. border-right:none;
  170. border-left:none;
  171. border-bottom:none;
  172. border-top:none
  173. }</style>
  174. <table><tr>
  175. <th><form action=https://notabug.org/jyamihud/FreeCompetitors>
  176. <button title="See the full source code of the software that powers this website." type="submit">SOURCE</button>
  177. </form></th>
  178. <th><form action=/faq>
  179. <button title="Frequently Asked Questions" type="submit">FAQ</button>
  180. </form></th>
  181. <th><form action=https://notabug.org/jyamihud/FreeCompetitors/issues>
  182. <button title="Report a bug." type="submit">BUG?</button>
  183. </form></th>
  184. <th><form action=https://notabug.org/jyamihud/FreeCompetitors/issues/25>
  185. <button title="Report a piece of software that's missing from the catalogue." type="submit">MISSING?</button>
  186. </form></th>
  187. <th><form action=https://notabug.org/jyamihud/FreeCompetitors/issues/24>
  188. <button title="Report a mistake in data about software." type="submit">MISTAKE?</button>
  189. </form></th></tr></table><br><br>
  190. """
  191. return page