Module: Relaton::Bib::Sanitizer
- Defined in:
- lib/relaton/bib/sanitizer.rb
Overview
Strips inline markup not in the basicdoc PureTextElement set (plus
,
OPAQUE elements (currently
Constant Summary collapse
- ALLOWED =
%w[ em strong sub sup tt underline strike smallcap br stem p eref xref fn link ].freeze
- OPAQUE =
Elements whose children are non-basicdoc inline notation (MathML, AsciiMath, LaTeX, …) and must be preserved verbatim rather than sanitised against ALLOWED.
%w[stem].freeze
- RENAME =
{ "italic" => "em", }.freeze
- TAG_RX =
%r{<[a-zA-Z/!?]}- NS_PREFIX_RX =
Captures a namespace prefix, on a tag or on an attribute: the "jats" of jats:p and </jats:italic>, and the "xlink" of xlink:href.
%r{(?:</?|\s)([A-Za-z_][\w.-]*):(?=[A-Za-z_])}- NS_PLACEHOLDER =
Namespace that declares a prefix which the content leaves undeclared. The sanitiser removes it again before it serialises.
"urn:x-relaton-undeclared:%s".freeze
- NS_WRAPPER =
Element that carries the placeholder declarations. Its children are the sanitised content, so the element itself never reaches the output.
"relaton-sanitizer-root".freeze
- NS_RESERVED =
Reserved prefixes. XML declares both, so the content must not.
%w[xml xmlns].freeze
- SAVE_OPTS =
Serialise without the FORMAT option, so the sanitiser keeps the shape of element-only content instead of adding newlines and indent.
Nokogiri::XML::Node::SaveOptions::AS_XML
Class Method Summary collapse
Class Method Details
.sanitize(content) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/relaton/bib/sanitizer.rb', line 67 def self.sanitize(content) return content unless sanitizable?(content) node = parse(content) return content if node.nil? sanitize_children(node) node.children.map do |c| c.to_xml(encoding: "UTF-8", save_with: SAVE_OPTS) end.join end |