-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add first draft of CL search #2051
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e97acd6
add first draft of CL search
teovin 42575a5
truncate name like other sources
teovin 776bb60
map additional fields to legal doc
teovin a040698
enable advanced search for cl source
teovin c22eadc
clean up headmatter html
teovin 1d7be58
fix tag
teovin 041007a
feed headmatter into xml as well, add jack's script
teovin 7bc82ff
remove placeholder fn
teovin 1a1f7d2
linting
teovin 5aaf88c
default non existing attrs to none, remove a bad attr
teovin 1cb9e47
account for cases without case_name
teovin 09d79c0
use empty string
teovin 7c20b0a
remove namespace
teovin ef85d09
update fn name, update import
teovin 4393e2e
store html source, use shorthand
teovin c26f0a9
add cl template
teovin f522cf2
grab additional fields to map to cl header template
teovin 6562520
remove div with unexisting method from template, remove spaces, sort …
teovin 6afd2c2
update the way case texts are aggregated
teovin e05079e
add all case text types to the check
teovin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
""" | ||
Convert between XML and HTML versions of CAP's formatted case data. | ||
""" | ||
|
||
import lxml.sax | ||
import lxml.html | ||
import xml.sax | ||
|
||
from lxml import etree | ||
|
||
# sax functions passed to render_sax_tags | ||
sax_start = lxml.sax.ElementTreeContentHandler.startElement | ||
sax_end = lxml.sax.ElementTreeContentHandler.endElement | ||
sax_chars = lxml.sax.ElementTreeContentHandler.characters | ||
|
||
mapping = { | ||
"casebody": "section", | ||
"parties": "h4", | ||
"docketnumber": "p", | ||
"court": "p", | ||
"decisiondate": "p", | ||
"otherdate": "p", | ||
"attorneys": "p", | ||
"opinion": "article", | ||
"author": "p", | ||
"page-number": "a", | ||
"extracted-citation": "a", | ||
"bracketnum": "a", | ||
"footnotemark": "a", | ||
} | ||
|
||
|
||
def render_sax_tags(tag_stack): | ||
# run all of our commands, like "sax_start(*args)", to actually build the xml tree | ||
handler = lxml.sax.ElementTreeContentHandler() | ||
for method, args in tag_stack: | ||
method(handler, *args) | ||
return handler._root | ||
|
||
|
||
class XmlToHtmlHandler(xml.sax.ContentHandler): | ||
def __init__(self, case_id): | ||
self.tag_stack = [] | ||
self.case_id = case_id | ||
self.head_matter_open = False | ||
|
||
def startElement(self, name, attrs): | ||
|
||
if name == "casebody": | ||
self.tag_stack.append( | ||
( | ||
sax_start, | ||
( | ||
"section", | ||
{ | ||
"class": "casebody", | ||
"data-case-id": self.case_id, | ||
"data-firstpage": attrs["firstpage"], | ||
"data-lastpage": attrs["lastpage"], | ||
}, | ||
), | ||
) | ||
) | ||
self.tag_stack.append((sax_chars, ("\n ",))) | ||
self.tag_stack.append((sax_start, ("section", {"class": "head-matter"}))) | ||
self.head_matter_open = True | ||
elif name == "opinion": | ||
if self.head_matter_open: | ||
self.close_head_matter() | ||
# set opinion type to 'none' for opinions that don't have 'type' in source xml | ||
attr_type = attrs.get("type", "none") | ||
self.tag_stack.append( | ||
(sax_start, ("article", {"class": "opinion", "data-type": attr_type})) | ||
) | ||
elif name == "page-number": | ||
label = attrs["label"] | ||
self.tag_stack.append( | ||
( | ||
sax_start, | ||
( | ||
"a", | ||
{ | ||
"id": "p" + label, | ||
"href": f"#p{label}", | ||
"data-label": label, | ||
"data-citation-index": attrs["citation-index"], | ||
"class": "page-label", | ||
}, | ||
), | ||
) | ||
) | ||
elif name == "extracted-citation": | ||
new_attrs = {"href": attrs["url"], "class": "citation", "data-index": attrs["index"]} | ||
if "case-ids" in attrs: | ||
new_attrs["data-case-ids"] = attrs["case-ids"] | ||
self.tag_stack.append((sax_start, ("a", new_attrs))) | ||
elif name in ("footnotemark", "bracketnum"): | ||
new_attrs = {"class": name} | ||
if "href" in attrs: | ||
new_attrs["href"] = attrs["href"] | ||
if "id" in attrs: | ||
new_attrs["id"] = attrs["id"] | ||
self.tag_stack.append((sax_start, ("a", new_attrs))) | ||
elif name in ( | ||
"parties", | ||
"docketnumber", | ||
"court", | ||
"decisiondate", | ||
"otherdate", | ||
"attorneys", | ||
"author", | ||
"p", | ||
"blockquote", | ||
): | ||
# content element | ||
# set id to 'none' for elements that don't have 'id' in source xml | ||
attrs_id = attrs.get("id", "none") | ||
attrs = {"id": attrs_id} | ||
if "data-blocks" in attrs: | ||
attrs["data-blocks"] = attrs["data-blocks"] | ||
if name not in ("p", "blockquote"): | ||
attrs["class"] = name | ||
new_name = "h4" if name == "parties" else "blockquote" if name == "blockquote" else "p" | ||
if self.head_matter_open: | ||
self.tag_stack.append((sax_chars, (" ",))) | ||
self.tag_stack.append((sax_start, (new_name, attrs))) | ||
else: | ||
# passthrough | ||
self.tag_stack.append((sax_start, (name, attrs))) | ||
|
||
def characters(self, text): | ||
if self.head_matter_open and text == " ": | ||
text = " " | ||
self.tag_stack.append((sax_chars, (text,))) | ||
|
||
def endElement(self, name): | ||
if name == "casebody" and self.head_matter_open: | ||
self.close_head_matter() | ||
self.tag_stack.append((sax_end, (mapping.get(name, name),))) | ||
|
||
def close_head_matter(self): | ||
self.tag_stack.append((sax_end, ("section",))) | ||
self.tag_stack.append((sax_chars, ("\n ",))) | ||
self.head_matter_open = False | ||
|
||
|
||
def xml_to_html(input, case_id): | ||
handler = XmlToHtmlHandler(case_id) | ||
xml.sax.parseString(input, handler) | ||
tree = render_sax_tags(handler.tag_stack) | ||
return etree.tostring(tree, encoding=str, method="html") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
web/main/templates/includes/legal_doc_sources/cap_header.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
web/main/templates/includes/legal_doc_sources/court_listener_header.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<header class="case-header legal-doc-header" > | ||
{% with md=legal_doc.metadata %} | ||
<div class="court" data-custom-style="Case Header">{{ md.court.name }}</div> | ||
<div class="citation" data-custom-style="Case Header">{{ legal_doc.cite_string }}</div> | ||
{% if md.docket_number %}<div class="docketnumber" data-custom-style="Case Header">{{ md.docket_number }}</div>{% endif %} | ||
{% if md.date_filed %}<div class="decisiondate" data-custom-style="Case Header">{{ md.date_filed }}</div>{% endif %} | ||
{% endwith %} | ||
</header> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!