Skip to content

Commit

Permalink
fixes #6 and fixes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Mar 19, 2023
1 parent 3ae7c09 commit 01353c6
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ceurspt/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__="0.0.2"
__version__="0.0.3"
44 changes: 43 additions & 1 deletion ceurspt/ceurws.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,61 @@
'''
import ceurspt.ceurws_base
import os
import re
from bs4 import BeautifulSoup

class Paper(ceurspt.ceurws_base.Paper):
"""
a CEUR-WS Paper with it's behavior
"""

def getPdf(self):
"""
get the PDF file for this paper
"""
volume=self.volume
pdf=f"{volume.vm.base_path}/Vol-{volume.number}/paper{self.paper_number}.pdf"
return pdf

class Volume(ceurspt.ceurws_base.Volume):
"""
a CEUR-WS Volume with it's behavior
"""

def getHtml(self)->str:
def getHtml(self,fixLinks:bool=True)->str:
"""
get my HTML content
Args:
fixLinks(bool): if True fix the links
"""
index_path=f"{self.vol_dir}/index.html"
with open(index_path, 'r') as index_html:
content = index_html.read()
if fixLinks:
soup = BeautifulSoup(content, 'html.parser')
for a in soup.findAll(['link','a']):
ohref=a['href']
# .replace("google", "mysite")
href=ohref.replace("http://ceur-ws.org/","/")
href=href.replace("../ceur-ws.css","/ceur-ws.css")
href=re.sub(r'paper([0-9]+).pdf', fr"/Vol-{self.number}/paper\g<1>.pdf", href)
pass
a['href']=href
content=soup.prettify( formatter="html" )
return content

def getPaper(self,paper_number:int):
"""
get the paper with the given number
Args:
paper_number(int): the number of the paper
"""
paper=Paper()
paper.paper_number=paper_number
paper.volume=self
return paper

class VolumeManager():
"""
Expand Down Expand Up @@ -46,6 +87,7 @@ def getVolume(self,number:int)->Volume:
vol_dir=f"{self.base_path}/Vol-{number}"
if os.path.isdir(vol_dir):
vol=Volume(number=number)
vol.number=int(vol.number)
vol.vm=self
vol.vol_dir=vol_dir
return vol
Expand Down
25 changes: 22 additions & 3 deletions ceurspt/webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
@author: wf
"""
from fastapi import FastAPI
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.responses import FileResponse, HTMLResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from ceurspt.ceurws import VolumeManager
import dataclasses

Expand All @@ -13,14 +14,32 @@ class WebServer:
the webserver
"""

def __init__(self,vm:VolumeManager):
def __init__(self,vm:VolumeManager,static_directory:str="static"):
"""
constructor
Args:
vm(VolumeManager): the volume manager to use
static_directory(str): the directory for static html files to use
"""
self.app = FastAPI()
#https://fastapi.tiangolo.com/tutorial/static-files/
self.app.mount("/static", StaticFiles(directory=static_directory), name="static")
self.vm=vm


@self.app.get("/Vol-{number:int}/paper{paper_number:int}.pdf")
async def paperPdf(number:int,paper_number:int):
"""
get the PDF for the given paper
"""
vol=self.vm.getVolume(number)
paper=vol.getPaper(paper_number)
pdf=paper.getPdf()
return FileResponse(pdf)



@self.app.get("/Vol-{number:int}.json")
async def volumeJson(number: int):
"""
Expand All @@ -40,7 +59,7 @@ async def volumeHtml(number:int):
"""
vol=self.vm.getVolume(number)
if vol:
content=vol.getHtml()
content=vol.getHtml(fixLinks=True)
return HTMLResponse(content=content, status_code=200)
else:
return HTMLResponse(content=f"unknown volume number {number}",status_code=404)
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ dependencies = [
# https://pypi.org/project/uvicorn/
"uvicorn",
# https://pypi.org/project/linkml-runtime/
"linkml-runtime"
"linkml-runtime",
# https://pypi.org/project/beautifulsoup4/
"beautifulsoup4"
]

requires-python = ">=3.8"
Expand Down
Binary file added static/CEUR-WS-logo-blue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/CEUR-WS-logo-xmas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/CEUR-WS-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/cc-by_100x35.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
135 changes: 135 additions & 0 deletions static/ceur-ws.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/* ceur-ws.css defines how links are displayed and */
/* possibly some other details of page rendering within */
/* CEUR-WS.org. */
/* Copyright 2002-2015 by Manfred Jeusfeld */
/* Use permitted within CEUR-WS.org. */
/* Modification permitted for members of the CEUR-WS TEAM.*/
/* */
/* THIS STYLE FILE IS FOR EXCLUSIVE USE WITHIN */
/* CEUR-WS.ORG! DO NOT COPY IT AS A WHOLE OR AS PARTS. */
/* IN PARTICULAR, THE TAGS CEUR* DEFINED BELOW MAY NOT BE */
/* REUSED OR OVERRIDDEN. */
/* All references to ceur-ws.css in HTML files must be */
/* relative to ceur-ws.org, e.g. "../ceur-ws.ccs". */
/* Absolute references like "ceur-ws.org/ceur-ws.css" are */
/* not permitted. */
/* */
/* Created: 5-Nov-2002/M.Jeusfeld */
/* Modified: 5-Sep-2005/M.Jeusfeld */
/* Modified: 22-Nov-2006/M.Jeusfeld (hover darker) */
/* Modified: 17-Dec-2008/M.Jeusfeld (specific font fam.) */
/* Modified: 15-Jan-2010/M.Jeusfeld (CEUR class tags) */
/* Modified: 14-Apr-2010/M.Jeusfeld (adapt link colors) */
/* Modified: 7-Jul-2010/M.Jeusfeld (AUX tags) */
/* Modified: 13-Oct-2011/M.Jeusfeld (ceur-ws.org) */
/* Modified: 20-Jun-2012/M.Jeusfeld (color of VOLNR,URN) */
/* Modified: 02-Jul-2012/M.Jeusfeld (square bullet LI) */
/* Modified: 08-Jul-2013/C.Lange (.unobtrusive and .copyright, to get rid of <font>) */
/* Modified: 16-Oct-2013/M.Jeusfeld (use grey for blue) */
/* Modified: 11-Dec-2013/M.Jeusfeld (CEURWSLOGO div) */
/* Modified: 24-Mar-2015/M.Jeusfeld (paper directories) */
/* Modified: 16-Jul-2015/C.Lange (more accessible link colors) */
/* Modified: 08-Feb-2016/M.Jeusfeld (distinguish AUX from CEURTITLES) */
/* Modified: 05-Oct-2019/M.Jeusfeld (CEURCCBY tag) */
/* Modified: 30-Oct-2019/M.Jeusfeld (write before Volnr disabled) */
/* Modified: 03-Dec-2019/M.Jeusfeld (print bg images) */
/* Modified: 07-Nov-2022/M.Jeusfeld (use https) */


body {
font-family: arial, sans-serif;
font-size: medium;
background-image: url(/static/ceurws-bg.png);
margin-left: +32px;
margin-right: +8px;
}

/* 2015-04-21/MJ: for mobile-friendly layout */
@-ms-viewport{
width: device-width;
initial-scale: 1.0;
}

@media print {
body > header address {
display:none;
}
body > header #document-license {
width:19em;
clear:right;
float:right;
margin-top:1em !important;
margin-right:0;
}
#content {
page-break-before:always;
}
}

A:link {color: #404080; text-decoration: none;} /* display of links */
A:visited {color: #404080; text-decoration: none;} /* visited links */
A:hover {color: #000080; text-decoration: none;} /* links under the cursor */
:target {background-color:#FFFFFF;} /* background for current fragment */

H1 {color:#000000; font-family:arial, cursive, sans-serif;}

P,TABLE,TR,TD {font-family:arial,sans-serif;}

UL {list-style-type: square; list-style-image: url(/ceurws-bullet-6x12.png);}

LI {margin-bottom:7px;} /* separates paper elements */

PRE {color:#228120}

.CEURPAGES {float:right; padding: 2px; font-size:smaller;}
.AUXPAGES {float:right; padding: 2px; font-size:smaller;}

.CEURTOC {width:90%; page-break-before:always;} /* table of contents a bit narrower than the total page width */

.CEURAUTHOR {font-style:italic;}
.CEURAUTHORS {font-style:italic;}
.AUXAUTHOR {font-style:normal;}
.AUXAUTHORS {font-style:normal;}

.CEURTITLE {font-weight:normal;}
.AUXTITLE {font-weight:normal;}
.CEURTITLE:hover {color: #006040;} /* proper CEURTITLEs get highlighted */

.CEURVOLNR {font-weight:bold; color:#404040; font-size:large;}

/* write "CEUR-WS.org/" with background color before the VOLNR */
/* will show up when printing the page */
/* .CEURVOLNR:before{content:"CEUR-WS.org/"; color:#F5F4EF; font-size:large;} */

.CEURURN {font-weight:bold; color:#404040;}

/* for paper directories */
.CEURVOLURL {font-weight:bold; color:#000080;}
.CEURPAPERID {font-weight:bold; color:#404040;}

.CEURABSTRACT {/* border-style: solid; border-width: 1px; padding: 5px 10px 5px 10px; */
word-wrap: break-word; margin-left: +42px; margin-right: +68px; }

.unobtrusive {color: #777777; font-size: smaller;} /* unobtrusive text; for copyright notes */

.copyright {font-size: x-small;} /* even smaller text in copyright notes; x-small replaces &lt;font size="-1"&gt; when base size is medium */


#CEURWSLOGO {
background-image: url('/static/CEUR-WS-logo.png');
height: 100px;
width: 390px;
color-adjust: exact !important; /*Firefox*/
}
#CEURWSLOGO:hover {
background-image: url('/static/CEUR-WS-logo-blue.png');
/* background-image: url('/static/CEUR-WS-logo-xmas.png'); */
}


#CEURCCBY {
background-image: url('/static/cc-by_100x35.png');
width: 100px;
height: 35px;
color-adjust: exact !important; /*Firefox*/
}
Binary file added static/ceurws-bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/ceurws-bullet-6x12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 11 additions & 2 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ def setUp(self, debug=False, profile=True):
Basetest.setUp(self, debug=debug, profile=profile)
script_path=Path(__file__)
base_path=f"{script_path.parent.parent}/ceur-ws"
static_directory=f"{script_path.parent.parent}/static"
vm=VolumeManager(base_path=base_path)
self.ws = WebServer(vm)
self.ws = WebServer(vm,static_directory=static_directory)
self.client = TestClient(self.ws.app)

def checkResponse(self,path:str,status_code:int)->'Response':
Expand Down Expand Up @@ -65,7 +66,7 @@ def test_read_volume(self):
debug=self.debug
#debug=True
for ext,expected,equal in [
(".json",{'number': 3262.0, 'title':None},True),
(".json",{'number': 3262, 'title':None},True),
("","CEURVERSION=2020-0",False),
(".html","CEURVERSION=2020-0",False)
]:
Expand All @@ -81,4 +82,12 @@ def test_read_volume(self):
self.assertEqual(expected,result)
else:
self.assertTrue(expected in result)

def test_read_paper(self):
"""
test reading the pdf for a paper
"""
response = self.checkResponse(f"/Vol-3262/paper1.pdf",200)
self.assertEqual(2509257,response.num_bytes_downloaded)
pass

25 changes: 23 additions & 2 deletions tests/test_ceurws.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,38 @@
@author: wf
'''
from ceurspt.ceurws import Volume
from ceurspt.ceurws import Volume,VolumeManager
from tests.basetest import Basetest
from pathlib import Path

class Test_CEURWS(Basetest):
"""
Test the ceur-ws concepts
"""

def setUp(self, debug=False, profile=True):
Basetest.setUp(self, debug=debug, profile=profile)
script_path=Path(__file__)
base_path=f"{script_path.parent.parent}/ceur-ws"
self.vm=VolumeManager(base_path=base_path)

def test_volume(self):
"""
test instantiating a volume
"""
vol=Volume()
print(vol)
debug=self.debug
if debug:
print(vol)

def test_fix_links(self):
"""
test fixing the links
"""
vol=self.vm.getVolume(3262)
html=vol.getHtml()
debug=self.debug
debug=True
if debug:
print(html)

0 comments on commit 01353c6

Please sign in to comment.