Foros del Web » Creando para Internet » CSS »

[SOLUCIONADO] reemplazar estilos CSS en dependencia de sus propiedades

Estas en el tema de reemplazar estilos CSS en dependencia de sus propiedades en el foro de CSS en Foros del Web. Hola amigos! Me gustaría hacer un script que tuviese en cuenta varias propiedades de CSS, y en dependencia de 3 de sus valores las modifique ...
  #1 (permalink)  
Antiguo 25/04/2016, 07:30
 
Fecha de Ingreso: abril-2016
Mensajes: 4
Antigüedad: 8 años
Puntos: 0
Información reemplazar estilos CSS en dependencia de sus propiedades

Hola amigos!
Me gustaría hacer un script que tuviese en cuenta varias propiedades de CSS, y en dependencia de 3 de sus valores las modifique de una manera u otra. Alguna pista por favor, es muy complicado.

Es decir:

MI ACTUAL CSS:

Código CSS:
Ver original
  1. .Estilo1 {
  2. ...
  3. ...
  4. text-indent: 10px;
  5. margin-left: 10px;
  6. margin-right: 10px;
  7. ...
  8. ...
  9. }
  10.  
  11. .Estilo2 {
  12. ...
  13. ...
  14. text-indent: -10px;
  15. margin-left: 10px;
  16. margin-right: 10px;
  17. ...
  18. ...
  19. }
  20.  
  21. .Estilo3 {
  22. ...
  23. ...
  24. text-indent: -10px;
  25. margin-left: 10px;
  26. margin-right: 0;
  27. ...
  28. ...
  29. }
  30.  
  31. .Estilo4 {
  32. ...
  33. ...
  34. text-indent: 10px;
  35. margin-left: 0;
  36. margin-right: 0;
  37. ...
  38. ...
  39. }


MI CSS DESPUÉS DE EJECUTAR EL SCRIPT:

Código CSS:
Ver original
  1. .Estilo1 {
  2. ...
  3. ...
  4. text-indent: 1em;
  5. margin-left: 1em;
  6. margin-right: 1em;
  7. ...
  8. ...
  9. }
  10.  
  11. .Estilo2 {
  12. ...
  13. ...
  14. text-indent: -1em;
  15. margin-left: 1em;
  16. margin-right: 1em;
  17. ...
  18. ...
  19. }
  20.  
  21. .Estilo3 {
  22. ...
  23. ...
  24. text-indent: -1em;
  25. margin-left: 1em;
  26. margin-right: 0;
  27. ...
  28. ...
  29. }
  30.  
  31. .Estilo4 {
  32. ...
  33. ...
  34. text-indent: 1em;
  35. margin-left: 0;
  36. margin-right: 0;
  37. ...
  38. ...
  39. }

gracias de antemano!
  #2 (permalink)  
Antiguo 29/04/2016, 07:58
Avatar de g3kdigital  
Fecha de Ingreso: noviembre-2013
Ubicación: En mi apartamento en bogotá
Mensajes: 208
Antigüedad: 10 años, 5 meses
Puntos: 39
Respuesta: reemplazar estilos CSS en dependencia de sus propiedades

Esto es más labor de javascript que de css, no?

En segundo, puede usar multiclases si tienes las mismas propiedades en distintos elementos y solo varian en unas cuantas propiedades, así:

Código HTML:
Ver original
  1. <div class="row col-xs-6 estilo-similar estilo1">
  2. <div class="row col-xs-6 estilo-similar estilo2">
  3. <div class="row col-xs-6 estilo-similar estilo3">

Código CSS:
Ver original
  1. .estilo-similar
  2. {
  3.  /*estilos similares*/
  4. }
  5.  
  6. .estilo1
  7. {
  8. margin: X;
  9. }
  10.  
  11. .estilo2
  12. {
  13. margin: Y;
  14. }
  15.  
  16. .estilo3
  17. {
  18. margin: z;
  19. }

Igual puede ser algo que ya sepas.
__________________
Puedes ser el tipo de persona que se amarga por lo inevitable o aceptar el reto de superarse siempre e ir a la par con el progreso.

WEB: G3K.co | codepen.io/g3kdigital
  #3 (permalink)  
Antiguo 20/08/2016, 12:29
 
Fecha de Ingreso: abril-2016
Mensajes: 4
Antigüedad: 8 años
Puntos: 0
Respuesta: reemplazar estilos CSS en dependencia de sus propiedades

Gracias g3kdigital, pero tengo que hacerlo con un Bash, y ya casi lo he conseguido. Pero aún tengo un error.

Mi archivo CSS y su ruta son: ./ePub/OEBPS/css/idGeneratedStyles.css

Con el siguiente Bash ejecutándolo en el Terminal de Mac o Linux, logro que cada estilo del CSS se me divida en un archivo independiente cada vez que encuentra caracter del final de estilo, es decir }


El script lo he llamado script.sh y es el siguiente codigo:

Código BASH:
Ver original
  1. cat ./ePub/OEBPS/css/idGeneratedStyles.css | (
  2. I=0;
  3. echo "">./ePub/OEBPS/css/idGeneratedStyles0;
  4. while read line;
  5. do
  6. echo $line >> ./ePub/OEBPS/css/idGeneratedStyles$I;
  7. if [ "$line" == '}' ];
  8. then I=$[I+1];
  9. echo "" > ./ePub/OEBPS/css/idGeneratedStyles$I;
  10. fi;
  11. done;
  12. )

Hasta aqui todo funciona correctamente, y después de ejecutarlo podríamos ver en nuestra carpeta, los siguientes archivos:
idGeneratedStyles.css
idGeneratedStyles0
idGeneratedStyles1
idGeneratedStyles2
idGeneratedStyles3



Pero ahora os enseño el script con el código para que al encontrar el estilo llamado "Estilo1" haga una modificación, y con el "Estilo2" otra diferente, etc...

Pero no funciona, creo que el error esta en la linea del "grep" que no reconoce el numero final del nombre del archivo y no hay manera de que funcione.
Una ayuda por favor:

Código BASH:
Ver original
  1. cat ./ePub/OEBPS/css/idGeneratedStyles.css | (
  2. I=0;
  3. echo "">./ePub/OEBPS/css/idGeneratedStyles0;
  4. while read line;
  5. do
  6. echo $line >> ./ePub/OEBPS/css/idGeneratedStyles$I;
  7. if [ "$line" == '}' ];
  8. then I=$[I+1];
  9. echo "" > ./ePub/OEBPS/css/idGeneratedStyles$I;
  10. fi;
  11.  
  12.  
  13. ############
  14. if grep -qr Estilo1 "./ePub/OEBPS/css/idGeneratedStyles$I"; then
  15. #delete the bad bleeding
  16. perl -i -ne'/\margin-left/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
  17. perl -i -ne'/\margin-right/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
  18. perl -i -ne'/\text-indent/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
  19.  
  20. #add the right bledding
  21. perl -pi -w -e 's#}#margin-left:1em;margin-right:1em;text-indent:1em;}#g;' ./ePub/OEBPS/css/idGeneratedStyles$I;
  22.  
  23. fi;
  24. done;
  25. )
  26.  
  27. ############
  28. if grep -qr Estilo2 "./ePub/OEBPS/css/idGeneratedStyles$I"; then
  29. #delete the bad bleeding
  30. perl -i -ne'/\margin-left/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
  31. perl -i -ne'/\margin-right/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
  32. perl -i -ne'/\text-indent/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
  33.  
  34. #add the right bledding
  35. perl -pi -w -e 's#}#margin-left:-1em;margin-right:1em;text-indent:1em;}#g;' ./ePub/OEBPS/css/idGeneratedStyles$I;
  36.  
  37. fi;
  38. done;
  39. )
  40.  
  41. ############
  42. if grep -qr Estilo3 "./ePub/OEBPS/css/idGeneratedStyles$I"; then
  43. #delete the bad bleeding
  44. perl -i -ne'/\margin-left/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
  45. perl -i -ne'/\margin-right/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
  46. perl -i -ne'/\text-indent/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
  47.  
  48. #add the right bledding
  49. perl -pi -w -e 's#}#margin-left:-1em;margin-right:1em;text-indent:0;}#g;' ./ePub/OEBPS/css/idGeneratedStyles$I;
  50.  
  51. fi;
  52. done;
  53. )
  54.  
  55. ############
  56. if grep -qr Estilo4 "./ePub/OEBPS/css/idGeneratedStyles$I"; then
  57. #delete the bad bleeding
  58. perl -i -ne'/\margin-left/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
  59. perl -i -ne'/\margin-right/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
  60. perl -i -ne'/\text-indent/ or print' ./ePub/OEBPS/css/idGeneratedStyles$I;
  61.  
  62. #add the right bledding
  63. perl -pi -w -e 's#}#margin-left:1em;margin-right:0;text-indent:0;}#g;' ./ePub/OEBPS/css/idGeneratedStyles$I;
  64.  
  65. fi;
  66. done;
  67. )



PD: Por favor administrador del forosdelweb, si lo considera oportuno, traslade este post a Linux pues será más fácil que alguien me ayude. Muchas gracias.
  #4 (permalink)  
Antiguo 26/08/2016, 03:26
 
Fecha de Ingreso: abril-2016
Mensajes: 4
Antigüedad: 8 años
Puntos: 0
Respuesta: reemplazar estilos CSS en dependencia de sus propiedades

Solucionado!
concretamente este script puedes usarlo cuando exportes desde indesign, pues Indesign tras la exportación los eBooks no se ven correctamente.
Ten encuenta:

1º lo he testeado desde el terminal de Mac
2º el ebook cuando lo descomprimes tiene la siguiente roota:
./ePub/OEBPS/css/idGeneratedStyles.css

3º el archivo css original tras exportarlo de indesign se llama:
idGeneratedStyles.css

4º Tu simplemente tendrías que sustituir los nombres de tus stylos por los que pone abajo, por ejemplo:
en lugar de:
if grep -qr L0-R0-I4 $f; then

pondrías:
if grep -qr MI_ESTILO_CSS $f; then


5º en este script se modifica solo el margin-left, margin-right y text-indent, pero tu puedes escoger las propiedades que desees.

6º el script simplemente se encargará de:
- dividir todo tu CSS en archivos,
- eliminar las propiedades margin-left, margin-right y text-indent
- reemplazar el signo } por tus propiedades correctas.

7º Después si lo deseas también puedes eliminar todos los archivos CSS que se han creado y juntarlos todos en 1, que se debe llamar idGeneratedStyles.css.
Haciendo esto:

Código BASH:
Ver original
  1. ############
  2. # Delete the original CSS
  3. rm ./ePub/OEBPS/css/idGeneratedStyles.css;
  4. # merge the css files
  5. cat ./ePub/OEBPS/css/GeneratedStyles* > ./ePub/OEBPS/css/idGeneratedStyles.css;
  6. # If you're using bash (the default shell), the extglob shell option allows you to use an extended pattern matching syntax.
  7. shopt -s extglob
  8. #remove all files except the idGeneratedStyles.css
  9. rm -r ./ePub/OEBPS/css/!(idGeneratedStyles.css)
  10.  
  11. sudo chown -Rv root ./ePub
  12. chmod -R 777 ./ePub


8º También puedes eliminar lineas concretas de CSS:
Código BASH:
Ver original
  1. # DELETE CSS LINES. It´s optional. Delete specific lines creating a temporally file, from your CSS for show the pictures with a right size on devices
  2. for x in ./ePub/OEBPS/css/idGeneratedStyles.css;
  3. do
  4. sed '2257,2258d' <"$x" >"$x.tmp"
  5. mv "$x.tmp" "$x"
  6. done
  7. for x in ./ePub/OEBPS/css/idGeneratedStyles.css;
  8. do
  9. sed '2271,2272d' <"$x" >"$x.tmp"
  10. mv "$x.tmp" "$x"
  11. done




Bueno tras esta descripción aqui, esta el código:


Código BASH:
Ver original
  1. #!/bin/bash
  2.  
  3. #####################################################################################
  4. ###START### MODIFY ALL BLEEDING STYLES FOR NICE APPEARANCE ON EPUB AND MOBI #########
  5. #####################################################################################
  6. # Split all the CSS styles from the Adobe Indesign into a separate files
  7. cat ./ePub/OEBPS/css/idGeneratedStyles.css | (
  8. I=0;
  9. echo "">./ePub/OEBPS/css/GeneratedStyles0;
  10. while read line;
  11. do
  12. echo $line >> ./ePub/OEBPS/css/GeneratedStyles$I;
  13. if [ "$line" == '}' ];
  14. then I=$[I+1];
  15. echo "" > ./ePub/OEBPS/css/GeneratedStyles$I;
  16. fi
  17. done;
  18. )
  19.  
  20. sudo chown -Rv root ./ePub/OEBPS/css/*
  21. chmod -R 777 ./ePub/OEBPS/css/*
  22. for f in `ls ./ePub/OEBPS/css/GeneratedStyles*` ; do
  23. ############
  24. if grep -qr L0-R0-I4 $f; then
  25.        
  26. perl -i -ne'/margin-left/ or print' $f;
  27. perl -i -ne'/margin-right/ or print' $f;
  28. perl -i -ne'/text-indent/ or print' $f;
  29.  
  30. #add the right bledding
  31. perl -pi -w -e 's#}#margin-left:0;margin-right:0;text-indent:1em;\}#g;' $f
  32. echo "working on css file $I %";
  33. fi
  34.  
  35. ############
  36. if grep -qr L0-R0-I5 $f; then
  37. #delete the bad bleeding
  38. perl -i -ne'/margin-left/ or print' $f;
  39. perl -i -ne'/margin-right/ or print' $f;
  40. perl -i -ne'/text-indent/ or print' $f;
  41.  
  42. #add the right bledding
  43. perl -pi -w -e 's/}/margin-left:0;margin-right:0;text-indent:1em;\}/g' $f
  44. echo "working on css file $f %";
  45. fi;
  46.  
  47. ############
  48. if grep -qr L0-R0-I3 $f; then
  49. #delete the bad bleeding
  50. perl -i -ne'/margin-left/ or print' $f;
  51. perl -i -ne'/margin-right/ or print' $f;
  52. perl -i -ne'/text-indent/ or print' $f;
  53.  
  54. #add the right bledding
  55. perl -pi -w -e 's/}/margin-left:0;margin-right:0;text-indent:1em;\}/g;' $f
  56. echo "working on css file $f %";
  57. fi;
  58. ############
  59. if grep -qr L0-R0-I5 $f; then
  60. #delete the bad bleeding
  61. perl -i -ne'/margin-left/ or print' $f;
  62. perl -i -ne'/margin-right/ or print' $f;
  63. perl -i -ne'/text-indent/ or print' $f;
  64.  
  65. #add the right bledding
  66. perl -pi -w -e 's/}/margin-left:0;margin-right:0;text-indent:1em;\}/g' $f
  67. echo "working on css file $f %";
  68. fi;
  69.  
  70. ############
  71. if grep -qr L6-R0-I-4 $f; then
  72. #delete the bad bleeding
  73. perl -i -ne'/margin-left/ or print' $f;
  74. perl -i -ne'/margin-right/ or print' $f;
  75. perl -i -ne'/text-indent/ or print' $f;
  76.  
  77. #add the right bledding
  78. perl -pi -w -e 's/}/margin-left:0;margin-right:0;text-indent:1em;\}/g;' $f
  79. echo "working on css file $f %";
  80. fi;
  81. ############
  82. if grep -qr L6-R6-I0 $f; then
  83. #delete the bad bleeding
  84. perl -i -ne'/margin-left/ or print' $f;
  85. perl -i -ne'/margin-right/ or print' $f;
  86. perl -i -ne'/text-indent/ or print' $f;
  87.  
  88. #add the right bledding
  89. perl -pi -w -e 's/}/margin-left:0;margin-right:0;text-indent:1em;\}/g' $f
  90. echo "working on css file $f %";
  91. fi;
  92.  
  93. ############
  94. if grep -qr L6-R6-I3 $f; then
  95. #delete the bad bleeding
  96. perl -i -ne'/margin-left/ or print' $f;
  97. perl -i -ne'/margin-right/ or print' $f;
  98. perl -i -ne'/text-indent/ or print' $f;
  99.  
  100. #add the right bledding
  101. perl -pi -w -e 's/}/margin-left:0;margin-right:0;text-indent:1em;\}/g;' $f
  102. echo "working on css file $f %";
  103. fi;
  104. ############
  105. if grep -qr L14-R4-I-5 $f; then
  106. #delete the bad bleeding
  107. perl -i -ne'/margin-left/ or print' $f;
  108. perl -i -ne'/margin-right/ or print' $f;
  109. perl -i -ne'/text-indent/ or print' $f;
  110.  
  111. #add the right bledding
  112. perl -pi -w -e 's/}/margin-left:0;margin-right:0;text-indent:1em;\}/g;' $f
  113. echo "working on css file $f %";
  114. fi;
  115. ############
  116. if grep -qr CHAPTER_eBook_03-Biblio-entry---2-lines-List-1-to-9---EBOOK $f; then
  117. #delete the bad bleeding
  118. perl -i -ne'/margin-left/ or print' $f;
  119. perl -i -ne'/margin-right/ or print' $f;
  120. perl -i -ne'/text-indent/ or print' $f;
  121.  
  122. #add the right bledding
  123. perl -pi -w -e 's/}/margin-left:0;margin-right:0;text-indent:1em;\}/g' $f
  124. echo "working on css file $f %";
  125. fi;
  126.  
  127. ############
  128. if grep -qr CHAPTER_eBook_03-Biblio-entry---subtitle-List-1-to-9---EBOOK $f; then
  129. #delete the bad bleeding
  130. perl -i -ne'/margin-left/ or print' $f;
  131. perl -i -ne'/margin-right/ or print' $f;
  132. perl -i -ne'/text-indent/ or print' $f;
  133.  
  134. #add the right bledding
  135. perl -pi -w -e 's/}/margin-left:0;margin-right:0;text-indent:1em;\}/g;' $f
  136. echo "working on css file $f %";
  137. fi;
  138. done;
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145. ###END### MODIFY ALL BLEEDING STYLES FOR NICE APPEARANCE ON EPUB AND MOBI #########
  146. #####################################################################################

Última edición por OdooPlay; 26/08/2016 a las 03:35 Razón: añado código extra muy util para el interesado

Etiquetas: estilos, propiedades, reemplazar, unix-linux
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 16:59.