mirror of https://git.sr.ht/~sircmpwn/hub.sr.ht
Add support for different named READMEs
This commit is contained in:
parent
030624ca8d
commit
d24e508cd4
|
@ -7,10 +7,8 @@ from hubsrht.services import git, hg
|
|||
from hubsrht.types import Feature, Event, EventType
|
||||
from hubsrht.types import Project, RepoType, Visibility
|
||||
from hubsrht.types import SourceRepo, MailingList, Tracker
|
||||
from jinja2 import Markup
|
||||
from srht.database import db
|
||||
from srht.flask import paginate_query
|
||||
from srht.markdown import markdown
|
||||
from srht.oauth import current_user, loginrequired
|
||||
from srht.validation import Validation, valid_url
|
||||
|
||||
|
@ -26,17 +24,11 @@ def summary_GET(owner, project_name):
|
|||
repo = project.summary_repo
|
||||
try:
|
||||
if repo.repo_type == RepoType.git:
|
||||
summary = git.get_readme(owner, repo.name)
|
||||
link_prefix = repo.url() + "/blob/refs/heads/master/"
|
||||
summary = git.get_readme(owner, repo.name, repo.url())
|
||||
elif repo.repo_type == RepoType.hg:
|
||||
summary = hg.get_readme(owner, repo.name)
|
||||
link_prefix = repo.url() + "/raw/"
|
||||
summary = hg.get_readme(owner, repo.name, repo.url())
|
||||
else:
|
||||
assert False
|
||||
# TODO: Pick from default branch
|
||||
summary = markdown(summary, ["h1", "h2", "h3", "h4", "h5"],
|
||||
link_prefix=link_prefix)
|
||||
summary = Markup(summary)
|
||||
except:
|
||||
summary = None
|
||||
summary_error = True
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
import os.path
|
||||
import requests
|
||||
from abc import ABC
|
||||
from flask import url_for
|
||||
from jinja2 import Markup, escape
|
||||
from srht.api import ensure_webhooks, get_authorization, get_results
|
||||
from srht.markdown import markdown
|
||||
from srht.config import get_origin
|
||||
|
||||
_gitsrht = get_origin("git.sr.ht", external=True, default=None)
|
||||
|
@ -10,6 +13,18 @@ _listsrht = get_origin("lists.sr.ht", external=True, default=None)
|
|||
_todosrht = get_origin("todo.sr.ht", external=True, default=None)
|
||||
origin = get_origin("hub.sr.ht")
|
||||
|
||||
readme_names = ["README.md", "README.markdown", "README"]
|
||||
|
||||
def format_readme(content, filename="", link_prefix=None):
|
||||
markdown_exts = ['.md', '.markdown']
|
||||
basename, ext = os.path.splitext(filename)
|
||||
if ext in markdown_exts:
|
||||
html = markdown(content, ["h1", "h2", "h3", "h4", "h5"],
|
||||
link_prefix=link_prefix)
|
||||
else:
|
||||
html = f"<pre>{escape(content)}</pre>"
|
||||
return Markup(html)
|
||||
|
||||
class SrhtService(ABC):
|
||||
def __init__(self):
|
||||
self.session = requests.Session()
|
||||
|
@ -40,16 +55,19 @@ class GitService(SrhtService):
|
|||
raise Exception(r.text)
|
||||
return r.json()
|
||||
|
||||
def get_readme(self, user, repo_name):
|
||||
def get_readme(self, user, repo_name, repo_url):
|
||||
# TODO: Cache?
|
||||
# TODO: Use default branch
|
||||
r = self.session.get(f"{_gitsrht}/api/repos/{repo_name}/blob/master/README.md",
|
||||
headers=get_authorization(user))
|
||||
if r.status_code == 404:
|
||||
return ""
|
||||
elif r.status_code != 200:
|
||||
raise Exception(r.text)
|
||||
return r.text
|
||||
link_prefix = repo_url + "/blob/refs/heads/master/"
|
||||
for readme_name in readme_names:
|
||||
r = self.session.get(f"{_gitsrht}/api/repos/{repo_name}/blob/master/{readme_name}",
|
||||
headers=get_authorization(user))
|
||||
if r.status_code == 404:
|
||||
continue
|
||||
elif r.status_code != 200:
|
||||
raise Exception(r.text)
|
||||
return format_readme(r.text, readme_name, link_prefix)
|
||||
return format_readme("")
|
||||
|
||||
def create_repo(self, user, valid):
|
||||
name = valid.require("name")
|
||||
|
@ -111,15 +129,18 @@ class HgService(SrhtService):
|
|||
raise Exception(r.text)
|
||||
return r.json()
|
||||
|
||||
def get_readme(self, user, repo_name):
|
||||
def get_readme(self, user, repo_name, repo_url):
|
||||
# TODO: Cache?
|
||||
r = self.session.get(f"{_hgsrht}/api/repos/{repo_name}/raw/README.md",
|
||||
headers=get_authorization(user))
|
||||
if r.status_code == 404:
|
||||
return ""
|
||||
elif r.status_code != 200:
|
||||
raise Exception(r.text)
|
||||
return r.text
|
||||
link_prefix = repo_url + "/raw/"
|
||||
for readme_name in readme_names:
|
||||
r = self.session.get(f"{_hgsrht}/api/repos/{repo_name}/raw/{readme_name}",
|
||||
headers=get_authorization(user))
|
||||
if r.status_code == 404:
|
||||
continue
|
||||
elif r.status_code != 200:
|
||||
raise Exception(r.text)
|
||||
return format_readme(r.text, readme_name, link_prefix)
|
||||
return format_readme("")
|
||||
|
||||
def create_repo(self, user, valid):
|
||||
name = valid.require("name")
|
||||
|
|
|
@ -204,7 +204,7 @@
|
|||
class="pull-right btn btn-primary btn-lg"
|
||||
>Use README.md {{ icon("caret-right") }}</button>
|
||||
<a
|
||||
href="{{repo.url()}"
|
||||
href="{{repo.url()}}"
|
||||
target="_blank"
|
||||
rel="noopener"
|
||||
>{{ repo.name }}</a>
|
||||
|
|
Loading…
Reference in New Issue