Format dom as expression using css - html

I'm creating a site that uses tags and needs to perform basic tag algebra with operators not, and, or. I have a dom element that describes the expression but can't display the expression using css.
Consider the following expression:
([Green] or ((not [Blue]) and ([Red] or (not [Yellow]))))
Which is represented in the dom as:
<span class="tag-expression">
<span class="tag-or">
<span class="tag" value="green">Green</span>
<span class="tag-and">
<span class="tag-not">
<span class="tag" value="blue">Blue</span>
</span>
<span class="tag-or">
<span class="tag" value="red">Red</span>
<span class="tag-not">
<span class="tag" value="yellow">Yellow</span>
</span>
</span>
</span>
</span>
</span>
I've managed to include the parenthesis using css' :before and :after tied with the content attribute (jfiddle demo). But have no luck showing the operators ¬, &, |. I've been toying with including a <span class="operator"/> with an image background but I was wondering is there is another way to make this using the :before and :after selectors.
Any ideas?

Here you go, it works with what you provided me in the example, you should test it out in more complex expressions to make sure it is correct.
I added some complex CSS selectors at the end of your CSS script for showing your operators:
.tag-expression .tag-or > span:nth-child(2):before {
content: ' | (';
}
.tag-expression .tag-and > span:first-child:after {
content: ' ) & ';
}
.tag-expression .tag-not:before {
content: ' ( ¬ ';
}​
You can checkout this in this fiddle. Let me know if that solves your problem.

Related

How can I remove html tag with Python?

My code is working. But the only thing it's also returning the HTML tags. Is there anything I can add to my FOR loop to strip the HTML code?
Here's my code below.
addressNeeded = soup.find("h1", {"style": "font-size: inherit; font-weight: inherit;"})
for x in addressNeeded:
addressList.append(x)
the outcome is:
['\n', <label class="summary-list__label">
<span itemprop="streetAddress">95 Cooks Drive</span>
</label>, '\n', <span class="summary-list__label summary-list__label--small">
<span itemprop="addressLocality">Westside</span>,
<span itemprop="addressRegion">NY</span>
<span itemprop="postalCode">07663</span>
I thank you in advance!
I believe you should change your print(x) to print(x.string) as suggested in this answer

Parsing ONLY plain text from HTML using Kanna Swift

I am using Kanna Swift for HTML parsing.
For example:
How can I parse ONLY the highlighted English Text in this situation?
To be prone to something, usually something bad, means to have
a tendency to be affected by it or to do it.
<div class="caption hide_cn">
<a class="anchor" name="prone_1"></a>
<span class="num">1</span>
<span class="st" title="能被表示程度的副词或介词词组修饰的形容词">ADJ-GRADED </span>
<span class="tips_box">
<span class="lbl type-syntax">
<span class="span"> [</span>
verb-link <span class="hi rend-sc">ADJ</span>
</span>
<span class="lbl type-syntax">
<span class="span">, </span>
<span class="hi rend-sc">ADJ</span>
to-infinitive
<span class="span">]</span>
</span>
</span>
<span class="def_cn cn_before">
<span class="chinese-text">有(不好的)倾向的;易于</span>
…
<span class="chinese-text">的;很可能</span>
…
<span class="chinese-text">的</span>
</span>
To be <b>prone to</b> something, usually something bad, means to have a tendency to be affected by it or to do it.
<span class="def_cn cn_after">
<span class="chinese-text">有(不好的)倾向的;易于</span>
…
<span class="chinese-text">的;很可能</span>
…
<span class="chinese-text">的</span>
</span>
</div>
If I use:
doc.css("div[class='caption hide_cn']")
I get all the messy part around the sentence I want.
Maybe I am wrong but I could not find enough documentation about the usage.
e.g. I learned"span[class= 'xxx xxx']" from stackoverflow instead of the documentation from that github page.
Do we have something like "[class != 'xxx xxx'] " or !=span
After some tweaks, I found a work around solution, in case someone needs it later.
We can use the removeChild method to remove all the other sections!
// Search for nodes by CSS
for whole in doc.css("div[class='caption hide_cn']") {
if let a1 = doc.css("span[class='num']").first {
whole.removeChild(a1)
}
if let a2 = doc.css("span[class='st']").first {
whole.removeChild(a2)
}
if let a3 = doc.css("span[class='tips_box']").first {
whole.removeChild(a3)
}
if let s1 = doc.css("span[class='def_cn cn_before']").first {
whole.removeChild(s1)
}
if let s2 = doc.css("span[class='def_cn cn_after']").first {
whole.removeChild(s2)
}
print(whole.text)
}
It's a pity I could not find this in the documentation. I guess those packages/libs are powerful enough to do almost anything you want. You just need to tweak a little bit.

Use regular expressions to add new class to element using search/replace

I want to add a NewClass value to the class attribute and modify the text of the span using find/replace functionality with a pair of regular expressions.
<div>
<span class='customer' id='phone$0'>Home</span>
<br/>
<span class='customer' id='phone$1'>Business</span>
<br/>
<span class='customer' id='phone$2'>Mobile</span>
</div>
I am trying to get the following result using after search/replace:
<span class='customer NewClass' id='phone$1'>Organization</span>
Also curious to know if a single find/replace operation can been used for both tasks?
Regex can do this, but be aware the using regex to change HTML can have a lot of edge cases that you may not have accounted for.
This regex101 example shows those three <span> elements changed to add NewClass and the contents to be changed to Organization.
Other technologies, however, would be safer. jQuery, for example, could replace them regardless of the order of the attributes:
$("span#phone$1").addClass("NewClass");
$("span#phone$1").text("Organization");
So just be careful with it, and you should be fine.
EDIT
According to comments on the OP, you want to only change the span containing ID phone$1, so the regex101 link has been updated to reflect this.
EDIT 2
Permalink was too long to fit into a comment, so adding the permalink here. Click on the "Content" tab at the bottom to see the replacement.
You can use a regex like this:
'.*?' id='phone\$1'>.*?<
With substitution string:
'customer' id='phone\$1'>Organization<
Working demo
Php code
$re = "/'.*?' id='phone\\$1'>.*?</";
$str = "<div>\n <span class='customer' id='phone\$0'>Home</span>\n<br/>\n <span class='customer' id='phone\$1'>Business</span>\n<br/>\n <span class='customer' id='phone\$2'>Mobile</span>\n</div>";
$subst = "'customerNewClass' id='phone\$1'>Organization<";
$result = preg_replace($re, $subst, $str);
Result
<div>
<span class='customer' id='phone$0'>Home</span>
<br/>
<span class='customerNewClass' id='phone$1'>Organization</span>
<br/>
<span class='customer' id='phone$2'>Mobile</span>
</div>
Since your tags include preg_match and preg_replace, I think you are using PHP.
Regex is generally not a good idea to manipulate HTML or XML. See RegEx match open tags except XHTML self-contained tags SO post.
In PHP, you can use DOMDocument and DOMXPath with //span[#id="phone$1"] xpath (get all span tags with id attribute vlaue equal to phone$1):
$html =<<<DATA
<div>
<span class='customer' id='phone$0'>Home</span>
<br/>
<span class='customer' id='phone$1'>Business</span>
<br/>
<span class='customer' id='phone$2'>Mobile</span>
</div>
DATA;
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xp = new DOMXPath($dom);
$sps = $xp->query('//span[#id="phone$1"]');
foreach ($sps as $sp) {
$sp->setAttribute('class', $sp->getAttribute('class') . ' NewClass');
$sp->nodeValue = 'Organization';
}
echo $dom->saveHTML();
See IDEONE demo
Result:
<div>
<span class="customer" id="phone$0">Home</span>
<br>
<span class="customer NewClass" id="phone$1">Organization</span>
<br>
<span class="customer" id="phone$2">Mobile</span>
</div>

Remove an element's class attribute with Hpricot

How do I do it? E.g.,
<span class="selected" id="hi">HELLO</span>
should become
<span id="hi">HELLO</span>
span = Hpricot(some_html) % "span#hi"
span.remove_attribute("class")

What is a tool that will syntax highlight using only HTML with class attributes?

I'm looking for a command line tool (or Perl module or VIM script or whatever) that will take some input files (such as XML or JavaScript files) and format them in HTML. I specifically want my output not to contain stuff like <span style="color: red"> or <font color=red> according to a particular colour scheme, rather it should use CSS class names to mark up the different syntactic parts of the file.
For example, if I had this file as input:
function f(x) {
return x + 1;
}
the kind of output I would like is:
<pre><span class=keyword>function</span> <span class=ident>f</span><span class=punc>{</span>
<span class=keyword>return</span> <span class=ident>x</span> <span class=op>+</span> <span class=numliteral>1</span><span class=punc>;</span>
<span class=punc>}</span></pre>
Does anyone know of such a tool?
Something like VIM's 2html.vim script, but outputting class="" attributes with the syntax highlight group names (like "Constant", "Identifier", "Statement", etc.) would be ideal.
Thanks,
Cameron
You can feed a file into GeSHi using PHP on the command line (or cURL your own local server or some other hack)
http://qbnz.com/highlighter/geshi-doc.html#basic-usage
There is buf2html.vim. Unfortunately, it uses non-semantic class names: See http://intrepid.perlmonk.org/apropos.vim/buf2html/current/myself.html
I think this is exacly what Vim's :TOhtml does if you
:let html_use_css = 1
Original:
function f(x) {
return x + 1;
}
output:
<pre>
<span class="Identifier">function</span> f(<span class="">x</span><span class="javaScriptParens">)</span><span class=""> </span><span class="Identifier">{</span>
<span class="Statement">return</span><span class=""> x + </span>1<span class="">;</span>
<span class="Identifier">}</span>
</pre>