Permalink
Browse files

moved to git

  • Loading branch information...
0 parents commit 3887e8b8e5c598afb884aa8c7b31bbb27b3fbb87 @duncantl duncantl committed Dec 6, 2011
@@ -0,0 +1,17 @@
+Package: RCSS
+Version: 0.2-0
+Title: Facilities for reading and working with CSS files in R
+Description: This is an interface to libcroco for reading CSS
+ files in R. This allows us to be able to understand the contents of a
+ CSS document. We can also explicitly match styles to XML/HTML nodes
+ to find which CSS elements will be applied to which XML/HTML nodes.
+Note: libcroco is used in Inkscape and development is active there.
+ The changes there will hopefully be merged back into libcroco.
+ So libcroco is a reasonable library to use for CSS parsing, but it does
+ have some bugs (case insensitive matching, handling of import nodes, page nodes).
+Author: Duncan Temple Lang
+Imports: methods
+System: libcroco
+Maintainer: Duncan Temple Lang <duncan@r-project.org>
+License: GPL
+
@@ -0,0 +1,53 @@
+useDynLib(RCSS)
+
+import(methods)
+
+if(FALSE)
+exportClasses(
+CSSAttributeSelector,
+CSSClassSelector,
+CSSComplexSelector,
+CSSDeclaration,
+CSSIDSelector,
+CSSPseudoSelector,
+CSSQualifiedSelector,
+CSSRGB,
+CSSSelectionEngine
+)
+
+#CSSRuleset,
+
+exportClass(CSSStatement)
+
+exportClass(CSSStyleSheet)
+#CSSSimpleSelector,
+
+
+exportClasses(
+AT_RULE,
+FONT_FACE_RULE,
+IMPORT_RULE,
+MEDIA_RULE,
+PAGE_RULE,
+RULESET,
+CHARSET_RULE)
+
+#ExternalRef
+# AttributeMatchWay,
+
+export(getCSSRules)
+export(readCSS)
+
+export(asCSSObject)
+exportMethods(asCSSObject)
+
+exportMethods("names")
+
+exportMethods(sapply)
+exportMethods(lapply)
+
+
+export(getPos)
+
+export(getDeclaration)
+exportMethods(getDeclaration)
@@ -0,0 +1,91 @@
+setClass("ExternalRef", representation(ref = "externalptr"))
+
+setClass("CSSSelectionEngine", contains = "ExternalRef")
+
+setClass("CSSStyleSheet", representation(docName = "character"), contains = "ExternalRef",
+ prototype = list(docName = as.character(NA)))
+
+setClass("CSSStatement", contains = "ExternalRef")
+setClass("AT_RULE", contains = "CSSStatement")
+setClass("RULESET", contains = "CSSStatement")
+setClass("IMPORT_RULE", contains = "CSSStatement")
+setClass("MEDIA_RULE", contains = "CSSStatement")
+setClass("PAGE_RULE", contains = "CSSStatement")
+setClass("CHARSET_RULE", contains = "CSSStatement")
+setClass("FONT_FACE_RULE", contains = "CSSStatement")
+
+# R-level objects
+setClass("CSSElement") # generic abstract class
+
+setClass("CSSRuleset", representation(declarations = "list",
+ selectors = "list" ),
+ contains = "CSSElement")
+
+
+setClass("CSSImportRule", contains = "CSSElement",
+ representation(url = "character",
+ media = "character",
+ sheet = "CSSStyleSheet"))
+
+
+setClass("CSSMediaRule", contains = "CSSElement",
+ representation(media = "character",
+ rulesets = "list"))
+
+setClass("CSSPageRule", representation(declarations = "list",
+ name = "character",
+ pseudo = "character"),
+ contains = "CSSElement")
+
+
+setClass("CSSCharsetRule",
+ representation(charset = "character"),
+ contains = "CSSElement")
+
+setClass("CSSFontFaceRule",
+ representation(declarations = "list"),
+ contains = "CSSElement")
+
+
+##################################
+
+setClass("CSSDeclaration",
+ representation(important = "logical",
+ property = "character",
+ value = "ANY")) # XXX for now. Could restrict to set.
+
+setClass("CSSRGB",
+ representation(values = "numeric",
+ is_percentage = "logical",
+ is_transparent = "logical",
+ inherit = "logical"),
+ prototype = list(values = numeric(3), is_percentage = FALSE, is_transparent = FALSE, inherit = FALSE))
+
+setClass("CSSSimpleSelector",
+ representation(is_case_sensitive = "logical",
+ name = "character",
+ specificity = "integer"))
+
+setClass("CSSComplexSelector",
+ contains = "CSSSimpleSelector",
+ representation(selectors = "list"))
+
+setClass("CSSQualifiedSelector")
+setClass("CSSIDSelector", representation(name = "character"), contains = "CSSQualifiedSelector")
+setClass("CSSClassSelector", representation(name = "character"), contains = "CSSQualifiedSelector")
+setClass("CSSPseudoSelector", representation(name = "character", extra = "character", type = "integer"), contains = "CSSQualifiedSelector")
+
+setClass("AttributeMatchWay", contains = "integer", prototype = structure(0L, names = "NO_MATCH"))
+setValidity("AttributeMatchWay", function(object) {
+ if(object < 0 || object > 4)
+ "value out of range (0 through 4)"
+ else
+ TRUE
+ })
+
+
+ # There can be a list of these within the CRAdditionalSelectorContent
+setClass("CSSAttributeSelector",
+ representation(name = "character",
+ value = "character",
+ match_way = "AttributeMatchWay"))
@@ -0,0 +1,12 @@
+
+getCSSRules =
+function(nodes, sheet, eng = NULL)
+{
+ if(length(nodes) > 1)
+ return(lapply(nodes,
+ function(x) {
+ getCSSRules(x, sheet, eng)
+ }))
+
+ .Call("R_getMatchedRulesets", eng, sheet, nodes)
+}
197 R/parse.R
@@ -0,0 +1,197 @@
+
+readCSS =
+function(src, asText = inherits(src, "AsIs"))
+{
+ if(!asText) {
+ src = path.expand(src)
+
+ if(!file.exists(src))
+ stop("no such CSS file. If this is CSS text, use asText = TRUE")
+ }
+
+ ans = .Call("R_parseCSS", src, !asText)
+ if(is.character(src) && !asText)
+ ans@docName = src
+
+ ans
+}
+
+setMethod("length", "CSSStyleSheet",
+ function(x) {
+ .Call("R_getStylesheetLength", x)
+ })
+
+setMethod("[[", c("CSSStyleSheet", "character"),
+ function(x, i, j, ...) {
+ k = pmatch(i, names(x))
+ if(is.na(k))
+ return(NULL)
+
+ x[[k]]
+ })
+
+
+setMethod("[[", c("CSSStyleSheet", "numeric"),
+ function(x, i, j, ...) {
+ if(i == 0)
+ return(NULL)
+ if(i < 1 || i > length(x))
+ stop("index out of range")
+
+ .Call("R_getStylesheetItem", x, as.integer(i - 1))
+ })
+
+setMethod("[", "CSSStyleSheet",
+ function(x, i, j, ..., drop = NA) {
+ if(missing(i) && missing(j)) {
+ if(length(x) == 0)
+ return(list())
+ return(lapply(seq(length = length(x)), function(i) x[[i]]))
+ }
+
+ if(!missing(j))
+ i = c(i, j)
+
+ lapply(i, function(i) x[[i]])
+ })
+
+
+setMethod("[", c("CSSStyleSheet", "character"),
+ function(x, i, j, ..., drop = NA) {
+ if(!missing(j))
+ i = c(i, j, unlist(list(...)))
+
+ k = pmatch(i, names(x))
+ if(all(is.na(k)))
+ return(NULL)
+ structure(x[k], names = names(x)[k])
+ })
+
+
+IMPORT_RULE_ELEMENTS = c("url", "sheet", "media_list")
+
+setMethod("$", "IMPORT_RULE",
+ function(x, name) {
+ i = pmatch(name, IMPORT_RULE_ELEMENTS)
+ if(is.na(i))
+ stop("No such field in ", class(x))
+
+ switch(i,
+ .Call("R_importRuleGetUrl", x),
+ .Call("R_importRuleGetSheet", x),
+ .Call("R_importRuleGetMediaList", x))
+ })
+
+setMethod("names", "CSSStyleSheet",
+ function(x){
+ if(length(x) == 0)
+ return(NULL)
+
+ els = lapply(x[], asCSSObject)
+ sapply(els, asCSSName)
+ })
+
+setGeneric("asCSSName", function(x) standardGeneric("asCSSName"))
+setMethod("asCSSName", "CSSCharsetRule", function(x) "charset")
+
+setMethod("asCSSName", "CSSQualifiedSelector", function(x) x@name)
+
+setMethod("asCSSName", "CSSElement", function(x) tolower(gsub("(CSS|Statement|Rule)", "", class(x))))
+
+setMethod("asCSSName", "CSSRuleset",
+ function(x) paste(sapply(x@selectors, asCSSName), collapse = ", "))
+
+setMethod("asCSSName", "CSSSimpleSelector",
+ function(x) x@name)
+setMethod("asCSSName", "CSSPseudoSelector",
+ function(x) { # paste(x@name, paste(x@extra, collapse = " "), sep = ":")
+ x@name
+ })
+
+setMethod("asCSSName", "CSSComplexSelector",
+ function(x) {
+ ops = attr(x@selectors, "ops")
+ if(length(ops) == 0)
+ ops = rep(".", length(x@selectors))
+
+ isPseudo = sapply(x@selectors, is, "CSSPseudoSelector")
+ ids = sapply(x@selectors, asCSSName)
+ if(any(isPseudo))
+ ids[isPseudo] = paste(":", ids[isPseudo], sep = "")
+ paste(x@name, paste(ops, ids, collapse = " "))
+ })
+
+
+setMethod("lapply", "CSSStyleSheet",
+ function(X, FUN, ...) {
+ lapply(X[], FUN, ...)
+ })
+
+setMethod("sapply", "CSSStyleSheet",
+ function(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE) {
+ sapply(X[], FUN, ..., simplify = simplify, USE.NAMES = USE.NAMES)
+ })
+
+##############
+
+setGeneric("asCSSObject",
+ function(x, ...) {
+ standardGeneric("asCSSObject")
+ })
+
+setMethod("asCSSObject", "CSSStatement",
+ function(x, ...) {
+ .Call("R_convertStatement", x)
+ })
+
+
+###################
+
+setGeneric("getPos", function(x, ...) standardGeneric("getPos"))
+
+setMethod('getPos', 'CSSStatement',
+ function(x, ...) {
+ .Call("R_getParseLocation", x)
+ })
+
+
+
+############################
+
+setGeneric("getDeclaration",
+ function(x, what, default = NA, simplify = TRUE)
+ standardGeneric("getDeclaration"))
+
+setMethod("getDeclaration",
+ "RULESET",
+ function(x, what, default = NA, simplify = TRUE) {
+ getDeclaration(asCSSObject(x), what, default, simplify)
+ })
+
+setMethod("getDeclaration",
+ "IMPORT_RULE",
+ function(x, what, default = NA, simplify = TRUE) {
+ default
+ })
+
+setMethod("getDeclaration",
+ "CSSRuleset",
+ function(x, what, default = NA, simplify = TRUE) {
+
+ val = x@declarations[[what]]
+ if(is.null(val))
+ default
+ else {
+ val = val@value
+ if(length(val) == 1)
+ val[[1]]
+ else
+ val
+ }
+ })
+
+setMethod("getDeclaration",
+ "CSSStyleSheet",
+ function(x, what, default = NA, simplify = TRUE) {
+ sapply(x, getDeclaration, what, default, simplify)
+ })
@@ -0,0 +1,6 @@
+CC=gcc
+CFLAGS=-c
+XTRA_CFLAGS=$(shell pkg-config --cflags libcroco-0.6)
+
+%.tu: %.c
+ $(CC) -fdump-translation-unit $(CFLAGS) $(XTRA_CFLAGS) $< -o /dev/null
@@ -0,0 +1,2 @@
+#include <libcroco/cr-om-parser.h>
+
Oops, something went wrong.

0 comments on commit 3887e8b

Please sign in to comment.