Looking for CSS parser in Scala [closed] - css

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I am working on a tool that generates HTML from an XML description. I am looking for a library in scala that can help me generate CSS styles for the html elements.

Not sure I understand exactly what you are looking for, but I know http://liftweb.net/ has many tools for dealing with xml, html, and css. You can use the lift libraries without using the full framework.

you are looking for something like this : https://github.com/axiak/scala-css-parser
Run it like that
java -jar CSSRewriter.jar sources/input.css -t where/this/will/go
https://github.com/axiak/scala-css-parser/blob/master/src/SimpleCSSParser.scala
import annotation.tailrec
import collection.mutable.ArrayBuffer
import util.parsing.combinator._
import java.io.File
// See http://www.w3.org/TR/css3-syntax/#grammar0
class SimpleCSSParser extends JavaTokenParsers {
protected override val whiteSpace = """(\s|//.*|(?m)/\*(\*(?!/)|[^*])+\*/)+""".r
// Lexical symbols
def h = "[0-9a-fA-F]".r
def nonascii = "[\200-\377]"
def unicode = "\\[0-9a-fA-F]{1,6}".r
def escape = unicode | "\\[ -~\200-\377]".r
def nmstart = "[a-zA-Z]" | nonascii | escape
def nmchar = "[a-zA-Z0-9-]" | nonascii | escape
override def stringLiteral = ("\""+"""([^"\p{Cntrl}\\]|\\[\\/bfnrt"]|\\u[a-fA-F0-9]{4})*"""+"\"").r | ("\'"+"""([^'\p{Cntrl}\\]|\\[\\/bfnrt']|\\u[a-fA-F0-9]{4})*"""+"\'").r
override def ident = """[*#_]?-?[a-zA-Z_][a-zA-Z0-9_-]*""".r
def name = rep1(nmchar)
def CDO = "<!--"
def CDC = "-->"
def INCLUDES = "~="
def DASHMATCH = "|="
def url = rep("[!#$%&*-~".r | nonascii | escape)
def IMPORT_SYM = "(?i)#import".r
def PAGE_SYM = "(?i)#page".r
def MEDIA_SYM = "(?i)#media".r
def FONT_FACE_SYM = "(?i)#font-face".r
def CHARSET_SYM = "(?i)#charset".r
def NAMESPACE_SYM = "(?i)#namespace".r
def IMPORTANT_SYM = "!important" | ("!" ~ "important")
def EMS = decimalNumber ~ "em"
def EXS = decimalNumber ~ "ex"
def RESOLUTION = decimalNumber ~ "(?i)dpi".r
def LENGTH = decimalNumber ~ "(?i)(?:px|cm|mm|in|pt|pc)".r
def ANGLE = decimalNumber ~ "(?i)(?:deg|rad|grad)".r
def TIME = decimalNumber ~ "(?i)(?:s|ms)".r
def FREQ = decimalNumber ~ "(?i)(?:Hz|kHz)".r
def DIMEN = decimalNumber ~ ident
def PERCENTAGE = decimalNumber ~ "%"
def NUMBER = decimalNumber | "\\" ~ decimalNumber
def URI = "url(" ~ ((stringLiteral | "[^)]+".r) ^^ (URL(_))) ~ ")"
def hexcolor = "#(?:[0-9A-Fa-f]{3}){1,2}".r
def function = "[a-zA-Z:._0-9-]+\\(".r ~ funcexpr ~ ")"
def unary_operator = "-" | "+"
def term: Parser[Any] = unary_operator | ((PERCENTAGE | LENGTH | EMS | EXS | ANGLE | RESOLUTION |
TIME | FREQ | URI | hexcolor | stringLiteral | NUMBER | ie_expression | function | ident) ^^ (NeedsSpace(_)))
def expr = rep1(term ~ opt(operator))
def ie_expression_no_paren = "[^\\(\\)]+".r
def ie_expression_paren: Parser[Any] = "(" ~ rep(ie_expression_no_paren | ie_expression_paren) ~ ")"
def ie_expression = "expression" ~ ie_expression_paren
// This is an extension of the css spec to allow filter: alpha(opacity=xx) syntax (kwargs).
def funcexpr = rep(opt(ident ~ "=") ~ term ~ opt(operator))
def operator = "/" | ","
def combinator = "+" | ">" | "~"
def prio = IMPORTANT_SYM
def declaration = property ~ ":" ~ expr ~ opt(prio)
def transform_declaration = """(?i)(?:from|to)""".r ~ "{" ~ rep1(declaration ~ rep(";")) ~ "}"
def nth_expr = ("\\d+".r ~ "n" ~ opt(("+" | "-") ~ "\\d+".r)) | (opt("\\d+".r ~ "n") ~ ("+" | "-") ~ "\\d+".r) | "\\d+".r
def pseudo = ":" ~ opt((ident ~ "(" ~ (HASH | class_ | ident | nth_expr | (":" ~ ident)) ~ ")") | ident)
def attrib = "[" ~ ident ~ opt(opt("=" | INCLUDES | DASHMATCH) ~ (ident | stringLiteral)) ~ "]"
def element_name = "*" | ident | "/**/"
def class_ = "." ~ ident
def HASH = "#" ~ ident
def selector_modifier = HASH | class_ | attrib | pseudo
def simple_selector = (element_name ~ rep(selector_modifier)) | (rep1(selector_modifier))
def selector = simple_selector ~ opt(combinator | ",")
def declaration_body = "{" ~ rep(transform_declaration | declaration ~ rep(";")) ~ "}"
def ruleset = rep1(selector ^^ (NeedsSpace(_))) ~ declaration_body
def property = ident
def font_face = FONT_FACE_SYM ~ declaration_body
def moz_document = ("(?i)#-moz-document".r ^^ (NeedsSpace(_))) ~ opt(function) ~ "{" ~ rep(ruleset) ~ "}"
def pseudo_page = ":" ~ ident
def medium = ident
def media_qualifier = "(" ~ ident ~ ":" ~ term ~ ")"
def media_term = (ident | media_qualifier) ~ opt(",")
def page = (PAGE_SYM ^^ (NeedsSpace(_))) ~ opt(ident) ~ opt(pseudo_page) ~ "{" ~ rep1sep(declaration, ",") ~ "}"
def media = (MEDIA_SYM ^^ (NeedsSpace(_))) ~ rep1(media_term) ~ "{" ~ rep(ruleset) ~ "}"
def namespace_prefix = ident
def namespace = (NAMESPACE_SYM ^^ (NeedsSpace(_))) ~ opt(namespace_prefix) ~ opt(stringLiteral | URI) ~ ";"
def import_ = (IMPORT_SYM ^^ (NeedsSpace(_))) ~ (stringLiteral | URI) ~ repsep(medium, ",") ~ ";"
def stylesheet = opt((CHARSET_SYM^^ (NeedsSpace(_))) ~ stringLiteral ~ ";") ~
rep(import_) ~ rep(namespace) ~
rep(media | page | font_face | moz_document | ruleset)
}
case class URL(url: String) {
val AbsolutePattern = """^(?i)(?:/|(?:http|ftp|https|spdy)://).*""".r
val InsideQuote = """^(['\"]?)(.+)\1$""".r
def rewrite(prefix: String): String = url match {
case InsideQuote(quote, content) => {
quote + rewriteInside(content, prefix) + quote
}
case _ => rewriteInside(url, prefix)
}
private def rewriteInside(inside: String, prefix: String): String = inside match {
case AbsolutePattern() => inside
case _ => prefix + inside
}
}
case class NeedsSpace(token: Any)
object Main extends SimpleCSSParser {
var prefix: String = ""
def main(args: Array[String]) {
val noSpace = Set(";", "}", ")", "{", "(", ",", ">", "<", "+")
val targetIndex = args.zipWithIndex filter {case (arg, idx) => arg == "-t"}
val target = if (targetIndex.length > 0)
Some(args(targetIndex(0)._2 + 1))
else
None
val sourceIndex = args.zipWithIndex filter {case (arg, idx) => arg == "-s"}
val sourcePath = if (sourceIndex.length > 0)
Some(args(sourceIndex(0)._2 + 1))
else
None
var originalSourcePath = if (sourcePath.isDefined)
Some(sourcePath.get)
else if (args.length > 0 && args(0) != "-t" && args(0) != "-s")
Some(args(0))
else
None
if (originalSourcePath.isDefined && target.isDefined) {
val parent = new File(originalSourcePath.get).getParent
val sourceList = if (parent == null || parent == "")
Nil
else
parent.split(File.separator).toList
val destParent = new File(target.get).getParent
val destList = if (destParent == null || destParent == "")
Nil
else
destParent.split(File.separator).toList
this.prefix = computePrefix(sourceList, destList).mkString("/")
if (!this.prefix.isEmpty)
this.prefix += "/"
}
val input = if (args.length > 0 && args(0) != "-t" && args(0) != "-s") {
io.Source.fromFile(args(0))
} else {
io.Source.stdin
}
val result = parseAll(stylesheet, input.getLines().mkString("\n"))
try {
val flatResult = flatResultList(result)
// Beautify by removing needless spaces.
flatResult.zipWithIndex foreach { case (value, idx) => if (idx > 0 && noSpace.contains(value) && flatResult(idx - 1) == " ") flatResult(idx - 1) = "" }
print(flatResult.mkString(""))
} catch {
case e: Exception => System.err.println(result)
}
}
#tailrec
def computePrefix(sourceDir: List[String], target: List[String], acc: List[String] = List()):
List[String] = (sourceDir, target) match {
case (shead :: srest, thead :: trest) =>
if (shead == thead)
computePrefix(srest, trest, acc)
else
computePrefix(srest, trest, ".." :: acc ::: List(shead))
case (Nil, thead :: trest) => computePrefix(Nil, trest, ".." :: acc)
case (shead :: srest, Nil) => computePrefix(srest, Nil, acc ::: List(shead))
case (Nil, Nil) => acc
}
def flatResultList(result: Any): ArrayBuffer[String] = result match {
case a: Some[Any] => flatResultList(a.get)
case a: ParseResult[Any] => flatResultList(a.get)
case a: ~[Any, Any] => flatResultList(a._1) ++ flatResultList(a._2)
case a :: rest => flatResultList(a) ++ flatResultList(rest)
case a: String => ArrayBuffer(a)
case None => ArrayBuffer()
case List() => ArrayBuffer()
/* Put any rewrite rule here, and annotate the above tokens with ^^ to do it. */
case url: URL => ArrayBuffer(url.rewrite(this.prefix))
case needsSpace: NeedsSpace => flatResultList(needsSpace.token) ++ ArrayBuffer(" ")
}
}
https://github.com/axiak/scala-css-parser/blob/master/src/testurl.sh
#!/bin/bash
curl "$1" | java -cp /opt/scala/lib/scala-library.jar:. Main

Take a look at Hyperscala (http://www.hyperscala.org). It has an incredibly powerful CSS parser built-in (StyleSheet.parse) that can parse multiple selectors.

Related

How to use gstreamer to output rtsp stream for multiple clients to access

I used the solution of< https://stackoverflow.com/questions/47396372/write-opencv-frames-into-gstreamer-rtsp-server-pipeline/60580247#60580247>, but when I use multiple clients to access the rtsp stream , The error of Assertion fctx->async_lock failed at libavcodec/pthread_frame.c:155 appeared.
I am new to gstreamer, Here is the complete Python code (copied from WisdomPill's answer):
import cv2
import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')
from gi.repository import Gst, GstRtspServer, GObject, GLib
class SensorFactory(GstRtspServer.RTSPMediaFactory):
def __init__(self, **properties):
super(SensorFactory, self).__init__(**properties)
self.cap = cv2.VideoCapture("http://192.168.2.153/video/mjpg.cgi")
self.number_frames = 0
self.fps = 10
self.duration = 1 / self.fps * Gst.SECOND # duration of a frame in nanoseconds
self.launch_string = 'appsrc name=source is-live=true block=true format=GST_FORMAT_TIME ' \
'caps=video/x-raw,format=BGR,width=640,height=480,framerate={}/1 ' \
'! videoconvert ! video/x-raw,format=I420 ' \
'! x264enc speed-preset=ultrafast tune=zerolatency ' \
'! rtph264pay config-interval=1 name=pay0 pt=96'.format(self.fps)
def on_need_data(self, src, lenght):
if self.cap.isOpened():
ret, frame = self.cap.read()
if ret:
data = frame.tobytes()
buf = Gst.Buffer.new_allocate(None, len(data), None)
buf.fill(0, data)
buf.duration = self.duration
timestamp = self.number_frames * self.duration
buf.pts = buf.dts = int(timestamp)
buf.offset = timestamp
self.number_frames += 1
retval = src.emit('push-buffer', buf)
print ('pushed buffer, frame {}, duration {} ns, durations {} s'.format(self.number_frames, self.duration, self.duration / Gst.SECOND))
if retval != Gst.FlowReturn.OK:
print(retval)
def do_create_element(self, url):
return Gst.parse_launch(self.launch_string)
def do_configure(self, rtsp_media):
self.number_frames = 0
appsrc = rtsp_media.get_element().get_child_by_name('source')
appsrc.connect('need-data', self.on_need_data)
class GstServer(GstRtspServer.RTSPServer):
def __init__(self, **properties):
super(GstServer, self).__init__(**properties)
self.factory = SensorFactory()
self.factory.set_shared(True)
self.get_mount_points().add_factory("/test", self.factory)
self.set_service('9999')
self.attach(None)
Gst.init(None)
server = GstServer()
loop = GLib.MainLoop()
loop.run()
if you have any suggestions, I would appreciate it

How to output entry from NCBI database into a table in R

thanks for your read and help.
I have download a genebank flat file from NCBI, which contains many entries. I would like to extract three entries from each gene and make them into a table. How to realize it? Thank you much. the file from NCBI---->The table I hope to get
my friend writes it for me with python:
================================================================================
import os
import pandas as pd
from tqdm import tqdm
import sys
def search_line(gene_dict,gene_name,target,info,mode,l):
if '/{}='.format(target) in l:
if len(l.split('"')) == 3:
gene_dict[gene_name][mode].append('{} = '.format(target) + l.split('"')[1].strip('\n'))
keep_read = 0
info = []
else:
info = [l.split('"')[1].strip('\n')]
keep_read = target_list.index(target)
else:
if '"' in l:
info.append(l.strip().strip('"\n'))
if '{} = '.format(target) + ' '.join(info) not in gene_dict[gene_name][mode]:
gene_dict[gene_name][mode].append('{} = '.format(target) + ' '.join(info))
keep_read = 0
info = []
else:
info.append(l.strip())
keep_read = target_list.index(target)
return gene_dict,info,keep_read
def init_frame_dict(gene_dict,ids,mode):
frame_dict = {'gene': gene_dict[ids]['gene'], 'source': mode}
for target in target_list[1:]:
frame_dict[target] = ''
return frame_dict
def gen_frame(gene_dict,flat):
frame = []
for ids in gene_dict.keys():
for mode in gene_dict[ids].keys():
if mode not in extract_list:
continue
# print(mode)
data = gene_dict[ids][mode]
frame_dict = init_frame_dict(gene_dict, ids, mode)
for target_data in data:
for target in target_list[1:]:
if '{} = '.format(target) in target_data:
if frame_dict[target] != '':
frame.append(frame_dict)
# print(frame_dict)
frame_dict = init_frame_dict(gene_dict, ids, mode)
frame_dict[target] = target_data.split('{} = '.format(target))[1]
frame.append(frame_dict)
pd.DataFrame(frame).to_csv('{}.csv'.format(flat[:-5]))
def main():
for flat in os.listdir(path_root):
gene_dict = {}
if flat[-4:] != 'flat':
continue
with open (os.path.join(path_root,flat)) as f:
lines = f.read()
genes = lines.split('/gene=')
skip = False
for gene in tqdm(genes[1:]):
if skip:
break
lines = gene.split('\n')
gene_name = lines[0].split('"')[1]
#init paras
mode = 'init'
target = 'none'
read_mode = 0
info = []
#init dict
if gene_name not in gene_dict:
gene_dict[gene_name] = {'gene':gene_name,'mRNA':[],'ncRNA':[],'CDS':[],'misc_RNA':[],'exon':[],}
#proc lines
for l in lines:
if 'ORIGIN' in l:
skip = True
break
if ' mRNA' in l:
mode = 'mRNA'
elif ' ncRNA' in l:
mode = 'ncRNA'
elif ' CDS' in l:
mode = 'CDS'
elif ' misc_RNA' in l:
mode = 'misc_RNA'
elif ' exon' in l:
mode = 'exon'
# search_line(gene_dict, gene_name, target, info, mode, l)
if '/product=' in l and mode != 'init' or (target == 'product' and read_mode == target_list.index('product')):
target = 'product'
gene_dict,info,read_mode = search_line(gene_dict, gene_name, target, info, mode, l)
if '/protein_id=' in l and mode != 'init' or (target == 'protein_id' and read_mode == target_list.index('protein_id')):
target = 'protein_id'
gene_dict,info,read_mode = search_line(gene_dict, gene_name, target, info, mode, l)
if '/note=' in l and mode != 'init' or (target == 'note' and read_mode == target_list.index('note')):
target = 'note'
gene_dict,info,read_mode = search_line(gene_dict, gene_name, target, info, mode, l)
if '/transcript_id=' in l and mode != 'init' or (target == 'note' and read_mode == target_list.index('transcript_id')):
target = 'transcript_id'
gene_dict,info,read_mode = search_line(gene_dict, gene_name, target, info, mode, l)
gen_frame(gene_dict,flat)
if __name__ == '__main__':
target_list = ['none', 'product', 'transcript_id','protein_id','note']
extract_list = ['mRNA']
path_root = 'flats'
if not os.path.exists(path_root):
print('Please put your flat files in flats/ directory !')
sys.exit()
if len(os.listdir(path_root)) == 0:
print('No files found in flats/ directory.')
sys.exit()
main()

Set.contains() in jvm doesn't work with Character class

HashMap<Character, Character> charMap = new HashMap<Character ,Character>();
charMap['{'] = '}';
charMap['['] = ']';
charMap['('] = ')';
//println charMap.keySet()
Character ch = '{'
println charMap.keySet().contains(ch)
It simply prints false while it is obvious that my set contains '{", any idea why would this happen? (I am using groovy for testing...)
Thanks.
'{' is a String and not a Character in groovy. To prove that, just print this in last line:
println charMap.keySet()*.class
It has to be explicitly type-casted to character:
HashMap<Character, Character> charMap = new HashMap<Character ,Character>();
charMap['{' as char] = '}';
charMap['['] = ']';
charMap['('] = ')';
//println charMap.keySet()
Character ch = '{'
println charMap.keySet().contains(ch)
This is how it works:
//String
assert 'a'.class == String
//Character
def c1= 'a' as char, c2= (char)'b'
def c3= new Character(c2), c4= c2.charValue()
[c1, c2, c3, c4].each { assert it.class == Character }
assert c2 == c3 && c1 != c2
assert c1 < c2 && c1.compareTo(c2) == -1
assert c2.toString().class == String

JavaCC: encountered... Was expecting one of... error

I'm getting this error when trying to run a parser written in JavaCC on a sample (syntactically valid) file:
Exception in thread "main" ParseException: Encountered "8;" at line 13, column 17.
Was expecting one of:
<INT_CONST> ...
"<" ...at jimpleParser.generateParseException(jimpleParser.java:2421)
at jimpleParser.jj_consume_token(jimpleParser.java:2292)
at jimpleParser.expr(jimpleParser.java:1038)
(shortened for conciseness)
I cannot work out why it is throwing an error. "8" should be a valid token. Here is the function in question:
String expr():
{
Token t1 = null, t2 = null;
String f1 = null, f2 = null, f3 = null;
}
{
(LOOKAHEAD(3)
f1 = imm() {System.out.println(f1);}
| f1 = new_expr()
| t1 = <LBR> f2 = nonvoid_type() t2 = <RBR> f3 = imm()
{f1 = t1.image.concat(f2.concat(t2.image.concat(f3)));}
| LOOKAHEAD(2)
f2 = imm() t1 = <INSTANCEOF> f3 = nonvoid_type()
{f1 = f2.concat(t1.image.concat(f3));}
| f1 = invoke_expr()
| LOOKAHEAD(2)
f1 = reference()
| LOOKAHEAD(2)
f1 = binop_expr()
| f1 = unop_expr())
{return f1;}
}
which should in turn call imm shown here:
String imm():
{
String f1 = null;
}
{
(f1 = constant()
| f1 = local_name())
{return f1;}
}
which should in turn call constant shown here:
String constant():
{
Token t1 = null, t2 = null;
String f1 = null;
}
{
(t1 = <INT_CONST> {f1 = t1.image; System.out.println(f1);}
| t1 = <FLOAT_CONST> {f1 = t1.image;}
| t1 = <MIN_INT_CONST> {f1 = t1.image;}
| t1 = <MIN_FLOAT_CONST> {f1 = t1.image;}
| t1 = <STRING_CONST> {f1 = t1.image;}
| t1 = <CLASS> t2 = <STRING_CONST> {f1 = t1.image.concat(t2.image);}
| t1 = <NULL> {f1 = t1.image;})
{return f1;}
}
8 should be an INT_CONST. Relevant token specifications are shown here:
<INT_CONST: ((<OCT_CONST> | <DEC_CONST> | <HEX_CONST>) ("L")?)>
<DEC_CONST: (<DEC_DIGIT>)+>
<DEC_DIGIT: ["0"-"9"]>
Any help would be much appreciated. Thanks
Note that it is not "8" that is causing the problem, but "8;". Although "8" is an INT_CONST, "8;" is not. So the longest match rule is kicking in and some other token production that does match "8;" is winning. See the FAQ http://www.engr.mun.ca/~theo/JavaCC-FAQ/ question 3.3. Without seeing all of your .jj file, I can't tell you which token it is, but if you put a break point on the code that constructs the error message you can easily see what the .kind field of the unexpected token holds.
Try this in your token section, it would definitely solve your problem:
TOKEN:
{
<DEC_DIGIT: (["0"-"9"])>
|
<INT_CONST: ((<OCT_CONST> | <DEC_CONST> | <HEX_CONST>) ("L")?)>
|
<DEC_CONST: (<DEC_DIGIT>)+>
// The rest of your tokens....
}

undefined vector.New

From this question
How do I use a (generic) vector in go?
I tried to create a new vector but the compiler says it is undefined:
$ 6g -V
6g version release.r60.3 9516
$ cat > vectest.go <<.
> package main
>
> import vector "container/vector"
> import "fmt"
>
> func main() {
> vec := vector.New(0);
> buf := make([]byte,10);
> vec.Push(buf);
>
> for i := 0; i < vec.Len(); i++ {
> el := vec.At(i).([]byte);
> fmt.Print(el,"\n");
> }
> }
> .
$ 6g vectest.go
vectest.go:7: undefined: vector.New
What might be wrong ?
weekly.2011-10-18
The container/vector package has been deleted. Slices are better.
SliceTricks: How to do vector-esque things with slices.
I revised your convertToLCD code to have better performance: 5,745 ns/op versus 19,003 ns/op.
package main
import (
"fmt"
"strconv"
)
const (
lcdNumerals = `
_ _ _ _ _ _ _ _
| | | _| _||_||_ |_ ||_||_|
|_| ||_ _| | _||_| ||_| _|
`
lcdWidth = 3
lcdHeight = 3
lcdLineLen = (len(lcdNumerals) - 1) / lcdWidth
)
func convertToLCD(n int) string {
digits := strconv.Itoa(n)
displayLineLen := len(digits)*lcdWidth + 1
display := make([]byte, displayLineLen*lcdHeight)
for i, digit := range digits {
iPos := i * lcdWidth
digitPos := int(digit-'0') * lcdWidth
for line := 0; line < lcdHeight; line++ {
numeralPos := 1 + lcdLineLen*line + digitPos
numeralLine := lcdNumerals[numeralPos : numeralPos+lcdWidth]
displayPos := displayLineLen*line + iPos
displayLine := display[displayPos : displayPos+lcdWidth]
copy(displayLine, string(numeralLine))
if i == len(digits)-1 {
display[displayLineLen*(line+1)-1] = '\n'
}
}
}
return string(display)
}
func main() {
fmt.Printf("%s\n", convertToLCD(1234567890))
}
Output:
_ _ _ _ _ _ _ _
| _| _||_||_ |_ ||_||_|| |
||_ _| | _||_| ||_| _||_|
It's true there is no vector.New in r60.3, but rather than patch up this code, you should learn the new append function. It made the vector package unnecessary, and in fact the package was removed some time ago from the weekly releases.

Resources